我是java的新手,对使用循环很新,到目前为止,我一直在为我的班级制作一个程序,检查用户是否输入随机生成的正确数字的变体,但是它说一旦循环开始,就无法找到lotteryNumberString。关于如何修复它的任何提示?
import java.util.Scanner;
public class Lottery
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
for(int i=0; i < 3; i++)
{
double lotto = Math.random();
int lotteryNumberDigit = (int)(lotto*10);
String lotteryNumberString = Integer.toString(lotteryNumberDigit);
}
String firstNumber = lotteryNumberString.substring(0,0);
String secondNumber = lotteryNumberString.substring(1,1);
String thirdNumber = lotteryNumberString.substring(2,2);
String firstTwoWinner = firstNumber + secondNumber;
String lastTwoWinner = secondNumber + thirdNumber;
String allNumbersWinner = firstNumber + secondNumber + thirdNumber;
System.out.println("Please enter your three numbers (e.g. 123): ");
String userInput = input.next();
if(userInput.substring(0,2).equals(firstTwoWinner))
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Congratulations, the front pair matched.");
}
else if (userInput.substring(1,3).equals(lastTwoWinner))
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Congratulations, the end pair matched.");
}
else if (userInput.equals(allNumbersWinner))
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Congratulations, both pairs matched.");
}
else
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Sorry, no matches. You only had one chance out of 100 to win anyway.");
}
}
}
答案 0 :(得分:1)
看一下这篇文章:http://www.java-made-easy.com/variable-scope.html。您面临的问题与lotteryNumberString变量的范围(生命周期)有关。如果在循环内部声明,它不存在于循环外部。要解决此问题,请在for循环
之前声明lotteryNumberString答案 1 :(得分:0)
问题是编译问题..因为你的lotterNumberString只在for循环中作用域,所以它在外面不可见。 你可能需要做这样的事情。
import java.util.Scanner;
public class Lottery
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String lotteryNumberString = null;
for(int i=0; i < 3; i++)
{
double lotto = Math.random();
int lotteryNumberDigit = (int)(lotto*10);
lotteryNumberString = Integer.toString(lotteryNumberDigit);
}
String firstNumber = lotteryNumberString.substring(0,0);
String secondNumber = lotteryNumberString.substring(1,1);
String thirdNumber = lotteryNumberString.substring(2,2);
String firstTwoWinner = firstNumber + secondNumber;
String lastTwoWinner = secondNumber + thirdNumber;
String allNumbersWinner = firstNumber + secondNumber + thirdNumber;
System.out.println("Please enter your three numbers (e.g. 123): ");
String userInput = input.next();
if(userInput.substring(0,2).equals(firstTwoWinner))
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Congratulations, the front pair matched.");
}
else if (userInput.substring(1,3).equals(lastTwoWinner))
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Congratulations, the end pair matched.");
}
else if (userInput.equals(allNumbersWinner))
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Congratulations, both pairs matched.");
}
else
{
System.out.println("Winner: " + allNumbersWinner );
System.out.println("Sorry, no matches. You only had one chance out of 100 to win anyway.");
}
}