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

Java 如何使用用户输入开始和结束循环?

Java 如何使用用户输入开始和结束循环?,java,loops,Java,Loops,我不知道如何提示一个问题,允许用户输入一个数字或字符来开始或结束循环 下面是代码 import java.util.Scanner; public class diceRoller{ public static void main(String[] args){ Scanner keyboard = new Scanner(System.in); int score; int sum=0; int roll1

我不知道如何提示一个问题,允许用户输入一个数字或字符来开始或结束循环

下面是代码

import java.util.Scanner;

public class diceRoller{
    public static void main(String[] args){     

        Scanner keyboard = new Scanner(System.in);

        int score;
        int sum=0;
        int roll1 = genRanInRange(1, 6);
        int roll2 = genRanInRange(1, 6);
        int roll3 = genRanInRange(1, 6);
        System.out.println("Your rolls were: " + " " + roll1 + " " + roll2 + " " + roll3);      
            if (roll1 == roll2 || roll2 == roll3 || roll1 == roll3){ // start of first loop

                System.out.println("You had 2 matching rolls! You gain 50 points.");
                score = 50;
                sum += score;
                if (roll1 == roll2 && roll2 == roll3 && roll1 == roll3){ // nested loop

                int rollSum = roll1 + roll2 + roll3;
                    if (rollSum == 18){ // nested loop 2

                    System.out.println("You had 3 matching 6's! You gain 500 points.");
                    score = 500;                        
                    sum += score;   
                    } // end nested loop 2

                } // end nested loop

            } // end of first loop

            else { 
                System.out.println("Sorry, you had 0 matching die.");
                score = 1;
                sum -= score;
            }
                System.out.println("Your score was: " + sum);   

            if (sum > 0){
                System.out.println("Would you like to continue?");
                }

            else{

                System.out.println("You are out of points. Would you like to restart?");
                }

    } // end Main

    public static int genRanInRange(int start, int end)
    {
        return (int)(Math.random()*(end-start+1.0)) + start;
    } // end genRanInRange

} // end Dice Roller

这应该能奏效

System.out.print("Would you like to continue? Y/N: ");
String UserResp = scanner.nextLine();
将其余代码放入while循环中。
对于第一个循环,在开始时有一个布尔值定义为true,然后将UserResp与Y或N进行比较以设置布尔值。

下面是可以使用的代码:

import java.util.Scanner;

public class DiceRoller{
    public static void main(String[] args){     

        Scanner keyboard = new Scanner(System.in);

        int score;
        int sum=0;
        char ans='y'; 
        while(ans=='Y'||ans=='y'){ //loop while user says Y 
            int roll1 = genRanInRange(1, 6);
            int roll2 = genRanInRange(1, 6);
            int roll3 = genRanInRange(1, 6);
            System.out.println("Your rolls were: " + " " + roll1 + " " + roll2 + " " + roll3);      
            if (roll1 == roll2 || roll2 == roll3 || roll1 == roll3){ // start of first loop
                System.out.println("You had 2 matching rolls! You gain 50 points.");
                score = 50;
                sum += score;
                if (roll1 == roll2 && roll2 == roll3 && roll1 == roll3){ // nested loop
                int rollSum = roll1 + roll2 + roll3;
                    if (rollSum == 18){ // nested loop 2

                    System.out.println("You had 3 matching 6's! You gain 500 points.");
                    score = 500;                        
                    sum += score;   
                    } // end nested loop 2

                } // end nested loop

            } // end of first loop

            else { 
                System.out.println("Sorry, you had 0 matching die.");
                score = 1;
                sum -= score;
            }
                System.out.println("Your score was: " + sum);   

            if (sum > 0){
                System.out.println("Would you like to continue?(Y/N) ");
                    ans=keyboard.next().charAt(0);
                }
            else{
                System.out.println("You are out of points. Would you like to restart?(Y/N) ");
                ans=keyboard.next().charAt(0);
            }

        }
        keyboard.close(); //Don't forget to close sccaner. 
    } // end Main

    public static int genRanInRange(int start, int end)
    {
        return (int)(Math.random()*(end-start+1.0)) + start;
    } // end genRanInRange

} // end Dice Roller
我将你的类的大小写从掷骰子改为掷骰子。

类名的首字母应始终大写

还有一件事:

别忘了关闭扫描仪


如果条件不是循环,则为
。这是一个分支。事实上,您的代码根本不包含任何循环。您希望用户从键盘输入什么?