有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当我输入不属于我的选项的输入时,java My while循环将永远运行

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.

    }
}

我不知道为什么,当我输入石英,羊皮纸或剪切程序运行良好。但是,当我输入任何其他内容时,程序会弹出“无法识别输入,请重试!”永远非常感谢您的帮助


共 (1) 个答案

  1. # 1 楼答案

    当您的输入既不是石英羊皮纸剪切时,将执行开关盒的默认设置。因此,这里将fasle分配给value(value=false)。所以,当编译器再次检查该条件时,会发现该条件为true,并再次执行默认情况,这将继续进行。这导致了无限循环