打破我的 while 循环

Breaking my while loop

我已经创建了程序的基本结构,但无论我尝试什么,我似乎都无法退出循环。

我还想让它显示用户输入的信息;退出循环后,从键盘转到控制台,但我不确定该怎么做。

import java.util.Scanner;

public class Requirement1 {

    public static void main(String[] args) {

        Scanner scan = new Scanner (System.in);
        String name, game, time, points;
        int i = 0;

        System.out.println("Please Enter Your Name");
        name = scan.nextLine();
        System.out.println("Players Name: " + name);
        while (i < 100)
        {

        Scanner scan2 = new Scanner(System.in);
        System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
        game = scan.nextLine();
        System.out.println("Game: " + game);

        Scanner scan3 = new Scanner (System.in);
        System.out.println("Please Enter Your Score");
        points = scan.nextLine();
        System.out.println("Your Score: " + points);

        Scanner scan4= new Scanner (System.in);
        System.out.println("Please Enter the amount of time Spent Playing in Minutes");
        time = scan.nextLine();
        System.out.println("Time played: " + time);
        }



    }

在循环中包含一个 i++

为了跳出 while 循环,您需要使 i < 100 为假,或者添加一个显式语句来停止循环 (break, return, throwSystem.exit,具体取决于您的要求)。

如果你想在用户输入 "quit" 时打破循环并执行循环后面的代码:

System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
game = scan.nextLine();
if (game.equals("quit")) {
  break;
}

目前,i 永远不会从 0 改变,所以 i < 100 始终为真。此外,它不会以其他方式读取。除非出于其他目的需要它,否则可以删除 i 并将 while 循环声明更改为:

while (true) {
  // ...
}

这个条件显然永远不会为假,因此您需要在循环中明确中断。

另一种方法是使用 for 循环,更适合您的需要:

for (int i = 0; i < 100; i++) {
    //your code
}

你的循环是一个简单的计数器,你可能想用 for-loop 替换它,因为它更好地表明循环不会因为循环内发生的某些操作而中断:

for(int i = 0 ; i < 100 ; i++) {
    // block running 100 times
}

一般来说,当我知道循环的确切次数 运行 时,我喜欢使用 for 循环。如果我不知道循环会运行多少次,那么我喜欢用while循环。

一个for循环:

import java.util.Scanner;

public class Requirement1 {

    public static void main(String[] args) {

        Scanner scan = new Scanner (System.in);
        String name, game, time, points;

        System.out.println("Please Enter Your Name");
        name = scan.nextLine();
        System.out.println("Players Name: " + name);

        for (int i = 0; i < 100; i++) {

            Scanner scan2 = new Scanner(System.in);
            System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
            game = scan.nextLine();

            if (game.equals("quit")) {
                break;
            }
            System.out.println("Game: " + game);

            Scanner scan3 = new Scanner (System.in);
            System.out.println("Please Enter Your Score");
            points = scan.nextLine();
            System.out.println("Your Score: " + points);

            Scanner scan4= new Scanner (System.in);
            System.out.println("Please Enter the amount of time Spent Playing in Minutes");
            time = scan.nextLine();
            System.out.println("Time played: " + time);
        }
    }

一个while循环:

import java.util.Scanner;

public class Requirement1 {

    public static void main(String[] args) {

        Scanner scan = new Scanner (System.in);
        String name, game, time, points;

        System.out.println("Please Enter Your Name");
        name = scan.nextLine();
        System.out.println("Players Name: " + name);

        while (true) {

            Scanner scan2 = new Scanner(System.in);
            System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
            game = scan.nextLine();

            if (game.equals("quit")) {
                break;
            }
            System.out.println("Game: " + game);

            Scanner scan3 = new Scanner (System.in);
            System.out.println("Please Enter Your Score");
            points = scan.nextLine();
            System.out.println("Your Score: " + points);

            Scanner scan4= new Scanner (System.in);
            System.out.println("Please Enter the amount of time Spent Playing in Minutes");
            time = scan.nextLine();
            System.out.println("Time played: " + time);
        }
    }

如果可能,避免使用 do / while 以支持 for 循环。如果游戏循环应该在用户键入退出时结束,请添加 break 语句。

for (i = 0; i < 100; i++) {

    // get user's game choice from console

    if (game.equals("quit")){
        break;
    }

    // if they didn't type quit ...
}