2011-11-18 330 views
3

好吧,所以我有点新来的Java和我试图做一个类,这将能够要求用户输入一个12位长的upc代码,检查以确保其有效的代码,然后显示如果它是否。我目前的程序有很多错误,而且我似乎无法弄清楚。这是我到目前为止的代码:Java错误消息“无法解析为变量”?

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 

我觉得像固定一两件事情将消除这种很多,谁能帮助我?

+1

如果我可能会建议某人从Java开始......如果你还没有,尝试用适当的IDE进行开发(我推荐[Eclipse](www.eclipse.org),但还有其他人良好的免费选择)。这将有助于各种各样的事情,从提供修复错误的建议到自动格式化代码以自动编译等。我还建议有意义地指定变量/方法/类(好吧,当它们是数学时很难,但仍然)并且学习在可能的情况下重构重复的代码(一次且只有一次规则)。祝你好运,并保持在它! :-) –

回答

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); 

找你定义和使用后,可以定义变量

编辑:因为如果

在定义你使用返回值的方法很长时间,并返回字符串值,请参阅返回值

return (upc + " is a feasible UPC code"); 

您需要更改返回类型无论是在方法或回程时间一样,如果你想回到这点,你的方法签名看起来像这样

public long getUpc(){ 
    // and return will work 
    return (upc + " is a feasible UPC code"); 
} 

,但如果你只想数值则没有变化它的方法签名只是像这样返回upc

return upc; 
+0

哦哇大声笑谢谢。好吧,我这样做了,现在我只有两个与最后的if语句有关的错误。我认为我以某种方式做了错误陈述。有任何想法吗? – Andrew

+0

你能解释你得到哪种类型的错误吗? – Pratik

+0

我在if语句末尾显示的错误显示“File:C:\ Users \ Andrew \ Downloads \ Upc.java [line:39] 错误:类型不匹配:无法从java.lang.String转换为long 文件:C:\ Users \ Andrew \ Downloads \ Upc.java [line:42] 错误:类型不匹配:无法从java.lang.String转换为long“ – Andrew

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); 
2

这是因为您在告诉编译器它们是什么之前使用了变量。首先,你应该宣布他们,然后使用它们。

相关问题