-2

我有以下作业问题:

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");
}
4

2 回答 2

2

您需要为NafterDo 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);
    }

编辑

如果您想在答案为 时打印“Game Stop” ,请N使用Thread.sleep(timeInMilliseconds);beforeSystem.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);
}
于 2015-02-12T17:03:37.147 回答
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
}
于 2015-02-12T17:02:18.277 回答