当我点击n或N时,程序不会退出?

时间:2015-02-12 16:45:21

标签: java loops while-loop sentinel

我有以下作业问题:

  

Q1。使用嵌套for循环语句绘制任何字符的空框(来自用户的输入)。这些框具有相同的行数和列数(来自用户的输入;有效范围:5到21)。测试输入错误(包括类型)

     

示例输出:

Do you want to start(Y/N): y
How many chars/last row? n
Not an integer! Try again! How many chars/last row? fgfgfg
Not an integer! Try again! How many chars/last row? 7.6
Not an integer! Try again! How many chars/last row? 34
ERROR! Valid range 5 - 21. How many chars/last row? 7
What character? k

Do you want to continue(Y/N): y

我已经编写了以下代码,但是当我点击“&n”时,它并没有退出。或者' N'我不知道为什么。我该如何解决这个问题?

public static void main(String[] args) {
    Scanner input = new Scanner(System. in );
    char answer = 'n';
    int row = 0;
    char output = 'k';

    do {
        System.out.println("DO YOU WANT TO START Y OR N?");
        answer = input.next().charAt(0);
        System.out.println("enter the number of rows");

        while (!input.hasNextInt()) {
            System.out.println("Not an integer,try again ");
            input.next();
        }

        row = input.nextInt();


        while (row < 5 || row > 21) {
            System.out.println("ERROR! Valid range 5 - 21. How many chars/last row?");
            row = input.nextInt();
        }
        System.out.println("WHAT CHARACTER?");
        output = input.next().charAt(0);

        for (int i = 0; i < row; i++) { //nested for loop to create the box
            System.out.print(output);
        }
        System.out.println();

        for (int i = 0; i < row - 2; i++) {
            System.out.print(output);
            for (int j = 0; j < row - 2; j++) {
                System.out.print(" ");
            }
            System.out.print(output);
            System.out.println();
        }
        for (int i = 0; i < row; i++) {
            System.out.print(output);
        }
        System.out.println();
        System.out.println();
        System.out.println("DO YOU WANT TO CONTINUE ? Y OR N");
        answer = input.next().charAt(0);

    } while (answer == 'Y' || answer == 'y');

    input.close();
    System.out.println("game stop");
}

2 个答案:

答案 0 :(得分:2)

您需要在NDo you want to start(Y/N):之后添加Do you want to continue(Y/N):的条件

System.exit(0)用于终止程序。

把这段代码

System.out.println("DO YOU WANT TO START Y OR N?");
    answer = input.next().charAt(0);
    if(answer == n || answer == N){
        System.exit(0);
    }

这适用于Do you want to continue(Y/N):

System.out.println("DO YOU WANT TO CONTINUE ? Y OR N");
    answer = input.next().charAt(0);
    if(answer == n || answer == N){
        System.exit(0);
    }

修改

如果您想要回答N,请打印“游戏停止”,然后在Thread.sleep(timeInMilliseconds);之前使用System.exit(0)

if(answer == n || answer == N){
    Thread.sleep(5000); //This will make console wait for 5 seconds before exiting.
    System.out.println("Game Stop."); //game stop will be printed for 5 seconds
    System.exit(0);
}

答案 1 :(得分:0)

最简单的方法是:

检查输入“N”是否在循环中,并且打开while循环,可能是这样的:

if ((answer == 'n') || (answer == 'N')) {
    break;
}

此外,您正在此程序中检查y / n输入2次。写一个更好的方法是使用普通的while循环而不是do-while循环;很明显,在问题中,如果你在开头输入N,那么根本不应该运行该程序。 Do-While循环对于确保程序至少运行一次非常有用(这不是此处应该发生的;程序只应在输入有效时运行,例如:“y”)。虽然使用DW循环是“好的”,但是while循环可以更好地发挥作用。

你的循环可以这样编写:

// you need this line to initially print the y/n question.
System.out.println("DO YOU WANT TO START Y OR N?");
// get an input and check if it is not n
while (Character.toUpperCase(input.next().charAt(0)) != 'N') {    
    // do stuff here 
    System.out.println("DO YOU WANT TO CONTINUE ? Y OR N"); // ask for next input
}