请帮助我使用 for 循环,因为我对 Java 还很陌生。当我运行我的程序时,编译器抛出一个错误,说我的 for 循环不是语句,它暗示了变量guess,所以我把String放在guess前面,因为我将它声明为String。在我编译了这个新编辑的程序后,它又抛出了另一个错误,这一次说变量guess 已经在for循环之外声明了。有什么方法可以在 for 循环中使用在 for 循环之外声明的变量?这是代码:
import java.util.Scanner;
import java.util.Random;
public class Lottery{
public static void main(String args[]){
// create Scanner object
Scanner in;
in = new Scanner(System.in);
// create counter variable
int counter = 0;
// prompt user to choose between and including 000 and 999
System.out.print("Please pick a number between and including 000 and 999: ");
// initialize guess
String guess = in.next();
// get first two digits of guess
String firstTwoDigitsGuess = guess.substring(0, 2);
// get last two digits of guess
String lastTwoDigitsGuess = guess.substring(1);
// create Random number
Random number = new Random();
// initialize range
int range = number.nextInt(1000);
// convert int range to String
String rangeString = Integer.toString(range);
// firstTwoDigits declaration and initialization for first two digits of rangeString
String firstTwoDigitsString = rangeString.substring(0, 2);
// secondTwoDigits declaration and initialization for last two digits of rangeString
String lastTwoDigitsString = rangeString.substring(1);
// firstTwoDitigsRangeTrailingZero declaration and initialization
String firstTwoDigitsRangeTrailingZero = "";
// secondTwoDigitRangeTrailingZero declaration and initialization
String lastTwoDigitRangeTrailingZero = "";
// while statement
for(guess; !guess.equals(rangeString); ){
// if-else-if statement for trailing zeroes
if(range < 100){
// create zero variable
char zero;
zero = '0';
// create number with one trailing zero
int oneTrailingZero;
oneTrailingZero = zero + range;
// convert int oneTrailingZero to String
String oneTrailingZeroString = Integer.toString(oneTrailingZero);
// create first two digits of one trailing zero
String firstOneTrailingZeroString = oneTrailingZeroString.substring(0, 1);
// create last two digits of one trailing zero
String lastOneTrailingZeroString = oneTrailingZeroString.substring(1);
// // nested if-else-if statements
// all digits match
if(guess.equals(oneTrailingZeroString)){
// notify user has won
System.out.println("Winner!\nRandom number: " + range);
// notify user why they won
System.out.println("Perfect guess, all digits match!");
// first two digits match
}else if(firstTwoDigitsGuess.equals( firstOneTrailingZeroString)){
// notify user has won
System.out.println("Winner!\nRandom number: " + range);
// notify user why they won
System.out.println("The first two digits match!");
// last two digits match
}else if(lastTwoDigitsGuess.equals( lastOneTrailingZeroString)){
// notify user has won
System.out.println("Winner!\nRandom number: " + range);
// notify user why they won
System.out.println("The last two digits match!");
}else{
// do nothing
}
}else if(range < 10){
// create doubleZero variable
String doubleZero = "00";
// create number with double trailing zeroes
String doubleTrailingZero;
doubleTrailingZero = doubleZero + range;
// create first two digits of double trailing zeroes
String firstDoubleTrailingZeroString = doubleTrailingZero.substring(0, 1);
// create last two digits of double trailing zeroes
String lastDoubleTrailingZeroString = doubleTrailingZero.substring(1);
// // nested if-else-if statements
// all digits match
if(guess.equals(doubleTrailingZero)){
// notify user has won
System.out.println("Winner!\nRandom number: " + range);
// notify user why they won
System.out.println("Perfect guess, all digits match!");
// first two digits match
}else if(firstTwoDigitsGuess.equals( firstDoubleTrailingZeroString)){
// notify user has won
System.out.println("Winner!\nRandom number: " + range);
// notify user why they won
System.out.println("Firt two digits match!");
// last two digits match
}else if(lastTwoDigitsGuess.equals( lastDoubleTrailingZeroString)){
// notify user has won
System.out.println("Winner!\nRandom number: " + range);
// notify user why they won
System.out.println("Last two digits match!");
}else{
// do nothing
}
// wrong guess
}else{
// notify user's guess is wrong
System.out.println("The guess you chose was wrong. You did not get a perfect guess. Neither did your first or last two digits match the random number. The random number was: " + range);
}
}
}
}