Java:变量已在方法中定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23148347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Variable is already defined in method
提问by user3547815
I am trying to make a program which makes a random number between 1000-9999 with no repeating numbers. The rules are I have to label the random number as a String, and then use a while loop to figure out if any of the numbers repeat.
我正在尝试制作一个程序,该程序在 1000-9999 之间生成一个没有重复数字的随机数。规则是我必须将随机数标记为字符串,然后使用 while 循环来确定是否有任何数字重复。
//generating random number
double answer1 = (Math.random() * 9000)+1000;
//converting answer
int answer2 = (int)answer1;
String answer = answer2 + " ";
//Labeling arrays
answer.toCharArray();
//**NEW** checking random number
while (answer.charAt(0) == answer.charAt(0)
|| answer.charAt(0) == answer.charAt(1)
|| answer.charAt(0) == answer.charAt(2)
|| answer.charAt(0) == answer.charAt(3)
|| answer.charAt(1) == answer.charAt(1)
|| answer.charAt(1) == answer.charAt(2)
|| answer.charAt(1) == answer.charAt(3)
|| answer.charAt(2) == answer.charAt(2)
|| answer.charAt(2) == answer.charAt(3)
|| answer.charAt(3) == answer.charAt(3)); //you have my apologies
{
////generating random number
double answer1 = (Math.random() * 9000)+1000;
//converting answer
int answer2 = (int)answer1;
String answer = answer2 + "
}
I know the while parameters are extremely long, but it was necessary to make sure none of the numbers repeated. I assumed that I could regenerate a new random number and test it again in the while loop, but I get the error that variables answer1, answer, and answer2 are already defined in my main method. Does anyone have a tip as to what I can do to generate a new random number until none of the numbers repeat?
我知道 while 参数非常长,但有必要确保没有重复的数字。我假设我可以重新生成一个新的随机数并在 while 循环中再次测试它,但是我收到错误,变量 answer1、answer 和 answer2 已经在我的主要方法中定义。有没有人知道我可以做些什么来生成一个新的随机数,直到没有一个数字重复?
回答by PressingOnAlways
I am not sure if this is the main issue (you may have more than one), but a ; should not terminate a while statement. It should be while (..) { }
, not while (..); { }
.
我不确定这是否是主要问题(您可能有多个),但是 ; 不应终止 while 语句。应该是while (..) { }
,不是while (..); { }
。
As for your error about redefinition, you cannot define the variable again that was already defined in a larger scope. For this instance, line 2, int answer2 = (int) answer1
already defined answer2. In the while loop, you just have to use the variable again and you do not need to define it. So it should be:
至于你关于重定义的错误,你不能再次定义已经在更大范围内定义的变量。对于这个例子,第 2 行,int answer2 = (int) answer1
已经定义了 answer2。在 while 循环中,您只需要再次使用该变量,而无需定义它。所以应该是:
while (...)
{
////generating random number
answer1 = (Math.random() * 9000)+1000;
//converting answer
answer2 = (int)answer1;
answer = answer2 + " ";
}
回答by nablex
There is so much wrong with this question that it boggles the mind.
这个问题有很多错误,令人难以置信。
However specifically: you are redefining variables in the same scope which is not allowed.
但具体来说:您在同一范围内重新定义变量,这是不允许的。
回答by Ankit Lamba
You've already define your variable answer2
in line 2
, and in your while loop you are trying to redefine the variable answer2
, i.e:
你已经定义了你的变量answer2
中line 2
,并在你的while循环,你正在试图重新定义变量answer2
,即:
int answer2 = (int)answer1;
You can do it like :
你可以这样做:
answer2 = (int)answer1;
回答by Rishi Dwivedi
answer.toCharArray(); // here you use answer variable
and than your loop closed
而不是你的循环关闭
while(conditon);
than again you define answer variable inside { }
再一次在 { } 中定义 answer 变量
String answer = answer2 + " "; // duplicate
you should use if for the condition and terminate the ; from while like
您应该使用 if 作为条件并终止 ; 从喜欢
while(condition){
}
and use
并使用
answer = answer2 + " ";
instead of
代替
String answer = answer2 + " ";