好吧所以我对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:\Users\Andrew\Downloads\Upc.java [line: 10]
Error: n2 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java [line: 10]
Error: n4 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java [line: 10]
Error: n6 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java [line: 10]
Error: n8 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java [line: 10]
Error: n10 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java [line: 11]
Error: n1 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java [line: 11]
Error: n3 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java [line: 11]
Error: n5 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java [line: 11]
Error: n7 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java [line: 11]
Error: n9 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java [line: 11]
Error: n11 cannot be resolved to a variable
File: C:\Users\Andrew\Downloads\Upc.java [line: 39]
Error: Type mismatch: cannot convert from java.lang.String to long
File: C:\Users\Andrew\Downloads\Upc.java [line: 42]
Error: Type mismatch: cannot convert from java.lang.String to long
我觉得修一两件事会消除很多这个,有人能帮助我吗?
答案 0 :(得分:3)
这是因为在这些行中使用它们之前没有声明这些变量:
int m = (n2 + n4 + n6 + n8 + n10);
long n = (n1 + n3 + n5 + n7 + n9 + n11);
您在 ...
之后声明了基于我认为你要做的事情,你需要将这三行移到 之后 大块的除法/模数代码:
int m = (n2 + n4 + n6 + n8 + n10);
long n = (n1 + n3 + n5 + n7 + n9 + n11);
long r = (10-(m+3*n)%10);
答案 1 :(得分:2)
此变量在
之后定义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);
你使用那个尚未定义的变量,并在使用之后定义变量
编辑:for 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;
答案 2 :(得分:2)
这是因为您在告诉编译器它们之前使用变量。首先你应该声明它们然后使用它们。
答案 3 :(得分:0)
正如其他人所说,对于前 11 个错误,您必须首先声明所有变量,然后才能使用它们。
对于最后两个错误:您将 getUpc() 方法声明为 LONG 方法,因此期望返回 LONG 类型的返回值;相反,您正在返回一个字符串。 更改方法声明或更改返回类型,它应该可以工作。