Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java使用for循环_Java_For Loop - Fatal编程技术网

java使用for循环

java使用for循环,java,for-loop,Java,For Loop,请帮助我使用for循环,因为我对Java相当陌生。当我运行我的程序时,编译器抛出一个错误,说我的for循环不是一个语句,它暗示了变量guess,所以我把String放在guess前面,因为我将它声明为String。在我编译了这个新编辑的程序之后,它抛出了另一个错误,这次说变量guess已经在for循环之外声明了。有没有办法在for循环中使用在for循环外部声明的变量?代码如下: import java.util.Scanner; import java.util.Random; public

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

        }


    }
}
}
import java.util.Scanner;
导入java.util.Random;
公共类彩票{
公共静态void main(字符串参数[]){
//创建扫描仪对象
扫描仪输入;
in=新扫描仪(System.in);
//创建计数器变量
int计数器=0;
//提示用户在000和999之间进行选择,包括000和999
System.out.print(“请选择一个介于000和999之间的数字:”;
//初始化猜测
字符串guess=in.next();
//得到猜测的前两位数
String firstTwoDigitGuess=guess.substring(0,2);
//得到猜测的最后两位数
String lastTwoDigitGuess=guess.substring(1);
//创建随机数
随机数=新随机数();
//初始化范围
整数范围=数字。下一个整数(1000);
//将整型范围转换为字符串
字符串rangeString=Integer.toString(范围);
//rangeString前两位的firstTwoDigits声明和初始化
String firstTwoDigitsString=rangeString.substring(0,2);
//rangeString最后两位的secondTwoDigits声明和初始化
String lastTwoDigitsString=rangeString.substring(1);
//firstTwoDitigsRangeTrailingZero声明和初始化
字符串firstTwoDigitsRangeTrailingZero=“”;
//secondTwoDigitRangeTrailingZero声明和初始化
字符串lastTwoDigitRangeTrailingZero=“”;
//while语句
for(guess;!guess.equals(rangeString);){
//用于尾随零的if-else-if语句
如果(范围<100){
//创建零变量
零焦;
零='0';
//创建一个尾随零的数字
int-oneTrailingZero;
oneTrailingZero=零+范围;
//将int-oneTrailingZero转换为字符串
字符串oneTrailingZeroString=Integer.toString(oneTrailingZero);
//创建一个尾随零的前两位数
String firstOneRailingZeroString=OneRailingZeroString.substring(0,1);
//创建一个尾随零的最后两位数
字符串LastOneRailingZeroString=OneRailingZeroString.substring(1);
////嵌套的if-else-if语句
//所有数字都匹配
if(guess.equals(oneTrailingZeroString)){
//通知用户已获胜
System.out.println(“获胜者!\n随机数:“+范围”);
//通知用户他们获胜的原因
System.out.println(“完美猜测,所有数字匹配!”);
//前两位数匹配
}else if(firstTwoDigitGuess.equals(firstOneRailingZeroString)){
//通知用户已获胜
System.out.println(“获胜者!\n随机数:“+范围”);
//通知用户他们获胜的原因
System.out.println(“前两位数字匹配!”);
//最后两位数匹配
}else if(lastTwoDigitGuess.equals(lastOneRailingZeroString)){
//通知用户已获胜
System.out.println(“获胜者!\n随机数:“+范围”);
//通知用户他们获胜的原因
System.out.println(“最后两位数字匹配!”);
}否则{
//无所事事
}
}否则如果(范围<10){
//创建双零变量
字符串doubleZero=“00”;
//创建带有双尾随零的数字
字符串双拖零;
doubleTrailingZero=doubleZero+量程;
//创建双尾随零的前两位数
String firstDoubleTrailingZeroString=doubleTrailingZero.substring(0,1);
//创建双尾随零的最后两位数
字符串lastDoubleTrailingZeroString=doubleTrailingZero.substring(1);
////嵌套的if-else-if语句
//所有数字都匹配
if(猜测等于(双跟踪零)){
//通知用户已获胜
System.out.println(“获胜者!\n随机数:“+范围”);
//通知用户他们获胜的原因
System.out.println(“完美猜测,所有数字匹配!”);
//前两位数匹配
}else if(firstTwoDigitGuess.equals(firstDoubleTrailingZeroString)){
//通知用户已获胜
System.out.println(“获胜者!\n随机数:“+范围”);
//通知用户他们获胜的原因
System.out.println(“第一个两位数匹配!”);
//最后两位数匹配
}else if(lastTwoDigitGuess.equals(lastDoubleTrailingZeroString)){
//通知用户已获胜
System.out.println(“获胜者!\n随机数:“+范围”);
//通知用户他们获胜的原因
System.out.println(“最后两位数字匹配!”);
}否则{
//无所事事
}
//猜错了
}否则{
//通知用户猜测错误
System.out.println(“您选择的猜测是错误的。您没有得到完美的猜测。您的前两位或后两位数字与随机数也不匹配。随机数为:“+范围”);
}
}
}
}

循环语句的格式如下:

for(int i = 0; i < 10; i++){
//    start^   end^     ^increment
}
for(int i=0;i<10;i++){
//开始^结束^增量
}

您不需要在for循环中使用
guess
,因为它不在for循环中
int i = 0;
for(i; i < 10; ++i) {...}
for(int i = 0; i < 10; ++i) {...}
int i = 0;
while(i < 4){
    //do stuff
    i++;
}
for(int i = 0; i < 4; i++){
    //do stuff
}