当我输入一个不是我的选项之一的输入时,我的while循环会永远运行

时间:2016-09-20 19:59:20

标签: java loops while-loop

import java.util.*;

public class Game {

public static void main(String[] args) {

    System.out.println("Hello, and welcome to my game of Quartz Parchment and Shears"
                        + " (AKA Rock Paper Scizzors)!"
                        + "\n\nType quartz, parchment or shears!\n");

    //The description and instructions for the game.

    boolean valid = false;

    Scanner userInput = new Scanner(System.in);
    String s = userInput.next();

    //This code asks for the user input.

    while (!valid) {

    switch (s) {

        case "Quartz":
            System.out.println("\nYou have picked quartz!");
                valid = true;
                break;
        case "Parchment":
            System.out.println("\nYou have picked parchment!");
                valid = true;
                break;
        case "Shears":
            System.out.println("\nYou have picked shears!");
                valid = true;
                break;
        default:
            System.out.println("\nInput not recognised, please try again!");
                valid = false;
                break;
                }
                    }

    userInput.close();

    //This code ensures the user selects a valid option.

    ArrayList<String> response = new ArrayList<String>();
        response.add("Quartz");
        response.add("Parchment");
        response.add("Shears");

        Random random = new Random();
        int value = random.nextInt(3);

        String ai = response.get(value);

    switch (ai) {

        case "Quartz":
            System.out.println("\nYour opponent has picked quartz!");
                break;
        case "Parchment":
            System.out.println("\nYour opponent has picked parchment!");
                break;
        case "Shears":
            System.out.println("\nYour opponent has picked shears!");
                break;

                    }

    //This code randomly selects the program's choice.

    if (ai == s) {
        System.out.println("Draw!");
    }
    else if (s.equals("Quartz") && ai.equals("Shears")) {

            System.out.println("\nYou win!");
    }
    else if (s.equals("Quartz") && ai.equals("Parchment")) {

            System.out.println("\nYou lose!");
    }
    else if (s.equals("Parchment") && ai.equals("Shears")) {

        System.out.println("\nYou lose!");
    }
    else if (s.equals("Parchment") && ai.equals("Quartz")) {

        System.out.println("\nYou win!");
    }
    else if (s.equals("Shears") && ai.equals("Quartz")) {

    System.out.println("\nYou lose!");
    }
    else if (s.equals("Shears") && ai.equals("Parchment")) {

    System.out.println("\nYou win!");
    }
    else {
        System.out.println("\nYou draw!");
        }   

    //This code decides and declares the winner.

    }
}

我不知道为什么,当我输入Quartz,Parchment或Shears时,程序运行正常。但是,当我输入任何其他内容时,程序会弹出&#34;输入无法识别,请再试一次!&#34;永远。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果您的输入既不是 Quartz 羊皮纸剪切,则会执行切换案例的默认设置。因此,您将分配fasle值( value = false )。所以当编译器再次检查条件时,发现条件为真,并且再次执行默认情况并且这继续进行。这导致无限循环