简体   繁体   English

虽然条件得到满足,但循环不循环

[英]while loop not looping despite condition being met

Here's my code so far (well, the while loop): 这是我到目前为止的代码(好吧,while循环):

public class Lab10d
{
public static void main(String args[])
{
    Scanner keyboard = new Scanner(System.in);
    char response = 0;


    //add in a do while loop after you get the basics up and running

        String player = "";

        out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");

        //read in the player value
        player = keyboard.next();

        RockPaperScissors game = new RockPaperScissors(player);
        game.setPlayers(player);
        out.println(game);
    while(response == ('y'))
    {
        out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
        player = keyboard.next();
        game.setPlayers(player);
        //game.determineWinner();
        out.println(game);
        out.println();

        //



    }
    out.println("would you like to play again? (y/n):: ");
        String resp =  keyboard.next();
        response = resp.charAt(0);
}
}

it's supposed to run the code additional times until an n is inputted 它应该运行代码额外的时间,直到输入n

When I input ay, it is supposed to re-run the code but doesn't 当我输入ay时,它应该重新运行代码但不会

Your while loop ends before you ask if they want to play again. 你的while循环在你询问他们是否想要再次播放之前结束。

Change the loop to: 将循环更改为:

while(response == ('y'))
    {
        out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
        player = keyboard.next();
        game.setPlayers(player);
        //game.determineWinner();
        out.println(game);
        out.println();
        out.println("would you like to play again? (y/n):: ");
        String resp =  keyboard.next();
        response = resp.charAt(0);
    }

There is another problem: response is not set to 'y' before the loop is started. 还有一个问题:在循环开始之前, response未设置为“y”。 It will not do anything in the loop at all. 它根本不会在循环中做任何事情。 Use a do { ... } while (response == 'y') loop instead. 请改用do { ... } while (response == 'y')循环。

    do
    {
        out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
        player = keyboard.next();
        game.setPlayers(player);
        //game.determineWinner();
        out.println(game);
        out.println();
        out.println("would you like to play again? (y/n):: ");
        String resp =  keyboard.next();
        response = resp.charAt(0);
    } while (response == 'y');

A do-while will execute the code once , then check the condition and keep executing if it is true . 一个做,而将执行一次代码, 然后检查条件,继续执行,如果它是true A while loop will just check the condition, and keep executing while it's true . while循环只会检查条件,并在其为true继续执行。

EDIT: I put together some code for you: 编辑:我为你整理了一些代码:

import java.util.Scanner;

public class Troubleshoot {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        char response = ' ';
        do {
            System.out.println("Stuff");
            System.out.print("Again? (y/n): ");
            response = s.next().charAt(0);
        } while (response == 'y');
    }

}

Output: 输出:

Stuff
Again? (y/n): y
Stuff
Again? (y/n): y
Stuff
Again? (y/n): n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM