-3

请帮助我使用 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);

        }


    }
}
}
4

3 回答 3

0

例如,for循环的工作方式与循环非常相似while

int i = 0;
while(i < 4){
    //do stuff
    i++;
}

是相同的

for(int i = 0; i < 4; i++){
    //do stuff
}

因此,在第一部分中,您定义了一个变量,在第二部分中,您说“只要这是真的就继续”,最后您说“在每个循环结束时执行此操作”

所以试试 for( ;!guess.equals(rangeString); ){//your code}

但是,你最好用这个替换你的 for 循环: while(!guess.equals(rangeString)){//your code}

于 2013-10-31T23:37:41.543 回答
0

您不需要guess在 for 循环中,因为它没有实例化它。而是尝试...

for( ; !guess.equals(rangeString); ) {...}

我很确定这就是它暗示它的原因。通常,至少在 Java 中,如果没有在 for 中创建变量,那么如果你按照你的方式做,它就会抱怨。比如这个...

int i = 0;
for(i; i < 10; ++i) {...}

抱怨这...

for(int i = 0; i < 10; ++i) {...}

才不是。

于 2013-10-31T23:28:20.990 回答
-2

循环语句是这样形成的:

for(int i = 0; i < 10; i++){
//    start^   end^     ^increment
}
于 2013-10-31T23:24:47.633 回答