Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Java_Loops_Do While - Fatal编程技术网

边做边循环Java

边做边循环Java,java,loops,do-while,Java,Loops,Do While,因此,我对编码几乎是一个新手,我正忙着玩“边做边循环”,我的代码可能很糟糕,而且很可能有1000种更好的方法来做我想做的事情,但无论如何 我尝试使用main方法,然后使用另外两种方法(出于练习目的)让用户选择一个介于1和10之间的数字,如果他们想再试一次 我让代码运行,直到它要求用户输入yes/no,但随后代码停止运行,我不知道为什么 public static void main(String[] args) { chooseNum(); } public static boole

因此,我对编码几乎是一个新手,我正忙着玩“边做边循环”,我的代码可能很糟糕,而且很可能有1000种更好的方法来做我想做的事情,但无论如何

我尝试使用main方法,然后使用另外两种方法(出于练习目的)让用户选择一个介于1和10之间的数字,如果他们想再试一次

我让代码运行,直到它要求用户输入yes/no,但随后代码停止运行,我不知道为什么

public static void main(String[] args) {
    chooseNum();

}

public static boolean chooseNum() {
    int number = 4;
    do {
        System.out.println("Enter a number 1 to 10");
        int userNum = in.nextInt();
        if (number == userNum) {
            System.out.println("You chose the correct number!");
            return tryAgain();
        } else if (number != userNum) {
            System.out.println("You chose the incorrect number!");
            return tryAgain();
        } else
            System.out.println("invalid Response");
    }
    while (false);
    return false;
}

public static boolean tryAgain(){

        System.out.println("Try again? yes/no");
        String response = in.nextLine();
        if (response.equalsIgnoreCase("yes")){
            return chooseNum();
        }else if (response.equalsIgnoreCase("no")){
            return false;
    }else
        return false;
    }
}
我的输出如下:

Enter a number 1 to 10
1
You chose the incorrect number!
Try again? yes/no

Process finished with exit code 0
为了输入是或否,我似乎无法进一步获取它

我知道这是基本的,我很可能看起来很可怜,但任何帮助都会被感激的

也许另一个新手可以得到帮助别人的机会:)

非常感谢

啊,“下一行”明白了

输入1时,STDIN包含
1\n\r
。调用nextInt()时,它会弹出STDIN流中的1。现在,当它调用nextLine()获取下一个输入时,它将读取下一个
\n\r
,并返回一个空字符串,因为
\n\r
仍然会污染流中的内容。如果改用next(),它将忽略
\n\r
(以及任何其他空格),并从STDIN中为您提供下一个单词。(您也可以只调用nextLine()两次,从nextInt调用中清除
\n\r

这是使用do while的正确方法

public static void main(String[] args) {
    chooseNum();
}

public static void chooseNum() {
    int number = 4;
    do {
        System.out.println("Enter a number 1 to 10");
        int userNum = in.nextInt();
        if (number == userNum) {
            System.out.println("You chose the correct number!");
        } else if (number != userNum) {
            System.out.println("You chose the incorrect number!");
        } else
            System.out.println("invalid Response");
    }
    while (tryAgain());
}

public static boolean tryAgain(){
    do {
        System.out.println("Try again? yes/no");
        String response = in.nextLine();
        if (response.equalsIgnoreCase("yes")){
            return true;
        }else if (response.equalsIgnoreCase("no")){
            return false;
        }else
            System.out.println("Invalid response.");
        }
    } while(true);
}

只需在方法中创建一个新的scanner实例,就可以避免nextLine错误。我在chooseNum和tryAgain中声明了一个新的扫描程序,从而对您的代码进行了编辑,这将避免缓冲输入引起的问题。我希望这有帮助

import java.util.Scanner;

public class dowhile {
    public static void main(String[] args) {
        chooseNum();
    }

    public static boolean chooseNum() {
        Scanner in = new Scanner(System.in);
        int number = 4;
        do {
            System.out.println("Enter a number 1 to 10");
            int userNum = in.nextInt();
            if (number == userNum) {
                System.out.println("You chose the correct number!");
                return tryAgain();
            } else if (number != userNum) {
                System.out.println("You chose the incorrect number!");
                return tryAgain();
            } else
                System.out.println("invalid Response");
        }
        while (false);
        return false;
    }

    public static boolean tryAgain(){
        Scanner in = new Scanner(System.in);
        System.out.println("Try again? yes/no");
        String response = in.nextLine();
        if (response.equalsIgnoreCase("yes")){
            return chooseNum();
        }else if (response.equalsIgnoreCase("no")){
            return false;
        }else
            return false;
    }
}

当您执行行
return tryAgain()时
chooseNum()
方法结束。这是返回语句的结果。

使用
next()
而不是
nextLine()
,因为在读取int并添加
\n
以提交值时。扫描仪正在将此作为输入
next()
应忽略先前输入的
\n
并按预期接收输入。

预期的输出是什么?尝试
执行{…}while(true)
而不是
…while(false)
“while(false)”实际上是告诉循环停止循环。实际上,while(false)在这里是正确的,因为它是递归循环,而不是while循环。(请注意,不要将递归用作while循环!)您应该将while循环更改为
while(tryAgain())
,并且不要在tryAgain中调用chooseNum