Java错误消息“无法解析为变量”?
发布时间:2020-12-15 08:45:23 所属栏目:Java 来源:网络整理
导读:好吧所以我对 java有点新意,我正在尝试创建一个类,它可以要求用户输入一个12位长的upc代码,检查以确保它是一个有效的代码,然后显示它是否是或不.我当前的程序有很多错误,我似乎无法弄明白.这是我到目前为止的代码: public class Upc { private long upc; pu
好吧所以我对
java有点新意,我正在尝试创建一个类,它可以要求用户输入一个12位长的upc代码,检查以确保它是一个有效的代码,然后显示它是否是或不.我当前的程序有很多错误,我似乎无法弄明白.这是我到目前为止的代码:
public class Upc { private long upc; public Upc(long upcs) { upc = upcs; } public long getUpc() { int m = (n2 + n4 + n6 + n8 + n10); long n = (n1 + n3 + n5 + n7 + n9 + n11); long r = (10 - (m + 3 * n) % 10); long n12 = (int) (upc % 10); upc /= 10; long n11 = (int) (upc % 10); upc /= 10; long n10 = (int) (upc % 10); upc /= 10; long n9 = (int) (upc % 10); upc /= 10; long n8 = (int) (upc % 10); upc /= 10; long n7 = (int) (upc % 10); upc /= 10; long n6 = (int) (upc % 10); upc /= 10; long n5 = (int) (upc % 10); upc /= 10; long n4 = (int) (upc % 10); upc /= 10; long n3 = (int) (upc % 10); upc /= 10; long n2 = (int) (upc % 10); upc /= 10; long n1 = (int) (upc % 10); if (r == n12) { return (upc + " is a feasible UPC code"); } else { return (upc + " is an invalid UPC code"); } } } 我的错误如下: 13 errors found: File: C:UsersAndrewDownloadsUpc.java [line: 10] Error: n2 cannot be resolved to a variable File: C:UsersAndrewDownloadsUpc.java [line: 10] Error: n4 cannot be resolved to a variable File: C:UsersAndrewDownloadsUpc.java [line: 10] Error: n6 cannot be resolved to a variable File: C:UsersAndrewDownloadsUpc.java [line: 10] Error: n8 cannot be resolved to a variable File: C:UsersAndrewDownloadsUpc.java [line: 10] Error: n10 cannot be resolved to a variable File: C:UsersAndrewDownloadsUpc.java [line: 11] Error: n1 cannot be resolved to a variable File: C:UsersAndrewDownloadsUpc.java [line: 11] Error: n3 cannot be resolved to a variable File: C:UsersAndrewDownloadsUpc.java [line: 11] Error: n5 cannot be resolved to a variable File: C:UsersAndrewDownloadsUpc.java [line: 11] Error: n7 cannot be resolved to a variable File: C:UsersAndrewDownloadsUpc.java [line: 11] Error: n9 cannot be resolved to a variable File: C:UsersAndrewDownloadsUpc.java [line: 11] Error: n11 cannot be resolved to a variable File: C:UsersAndrewDownloadsUpc.java [line: 39] Error: Type mismatch: cannot convert from java.lang.String to long File: C:UsersAndrewDownloadsUpc.java [line: 42] Error: Type mismatch: cannot convert from java.lang.String to long 我觉得修一两件事会消除很多这个,有人能帮帮我吗? 解决方法
这个变量定义在之后
long n12 = (int) (upc%10); upc /= 10; long n11 = (int) (upc%10); upc /= 10; long n10 = (int) (upc%10); upc /= 10; long n9 = (int) (upc%10); upc /= 10; long n8 = (int) (upc%10); upc /= 10; long n7 = (int) (upc%10); upc /= 10; long n6 = (int) (upc%10); upc /= 10; long n5 = (int) (upc%10); upc /= 10; long n4 = (int) (upc%10); upc /= 10; long n3 = (int) (upc%10); upc /= 10; long n2 = (int) (upc%10); upc /= 10; long n1 = (int) (upc%10); int m = (n2 + n4 + n6 + n8 + n10); long n = (n1 + n3 + n5 + n7 + n9 + n11); long r = (10-(m+3*n)%10); 你使用那个尚未定义的变量,并在使用后定义变量 编辑:对于if 当您使用返回值定义方法时,如果您返回String值,请参阅返回值 return (upc + " is a feasible UPC code"); 你需要在方法或返回时更改返回类型,就像你想要返回它一样,那么你的方法签名就像这样 public long getUpc(){ // and return will work return (upc + " is a feasible UPC code"); } 但是如果你只想要数值,那么不要改变它的方法签名只需像这样返回upc return upc; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |