(使用BlueJ)在终端中,当我的代码运行到某个点时,用户可以连续输入单词但是什么也做不了

(Using BlueJ) In the terminal, when my code runs to a certain point, the user can continuously type in words but nothing will be done with it

我提前为我的初级代码道歉 -- 我几个月前开始编码。

我正在尝试编写一个基于文本的烘焙游戏,其中 combos/recipes (16) 个数量有限,并且用户必须尝试解锁所有蛋糕组合才能完成游戏。当我尝试 运行 代码时,当询问用户想要的顶部时,无论我输入什么输入,代码都不会 运行 通过这部分。预期的结果是将口味和浇头都加在一起成为新的蛋糕串。

[所描述问题的屏幕截图][1] [1]: https://i.stack.imgur.com/bphyO.png

我遇到的另一个问题是,当代码 运行转到检查蛋糕组合是否已经找到的部分,在终端内它无限地打印出用户首先找到的组合。

非常感谢任何帮助,非常感谢。

代码:

import java.util.Arrays; import java.util.Scanner; import java.util.ArrayList;

public class Bakery

  public ArrayList<String> aList = new ArrayList();
    public static int achievements = 0;

    static ArrayList<String> foundCakes = new <String>ArrayList();
   
    public static String[] f = {"chocolate", "vanilla", "strawberry", "banana"};
    public static String[] t = {"sprinkles", "fruit", "frosting", "nothing"};
    public static void main (String[]args) throws InterruptedException {
        Scanner sc = new Scanner(System.in); 
        System.out.println("(To quit the game, type in 'quit')");

        delay("Hi, what's your name?", 60L);
        String playerName = sc.nextLine(); 
        delay("Your name is: " + playerName, 60L); 
        delay("Welcome to this Bakery!", 40L);
        delay("This Bakery has been without an owner for so long...",40L);
        delay("Most of it's recipies have been lost.", 40L);
        delay("It's up to you to collect all of the lost recipies!", 40L);
        delay("These are the ingredients provided: ", 60L);
        delay("Base flavors: " + Arrays.toString(f), 60L);
        delay("Toppings: " + Arrays.toString(t), 60L);
        
        while (achievements != 16){
            System.out.println("Pick a flavor");
            String flavor = sc.nextLine();

            if (flavor.equalsIgnoreCase("quit")){
                delay("Thanks for playing!", 40L);
                System.exit(0);
            }

            
            String cuFlavor = flavor.toLowerCase();
          
            boolean oo = false;
            while (oo){
                if(Arrays.asList(f).contains(cuFlavor)){
                    oo = true;
                }
                
            }
            if (Arrays.asList(f).contains(cuFlavor) == false){
                delay("Not an option, please pick again.", 40L);
                flavor = sc.nextLine();
            }

            System.out.println("Pick a topping");
            String topping = sc.nextLine();
            if (topping.equalsIgnoreCase("quit")){
                delay("Thanks for playing!", 40L);
                System.exit(0);
            }
            String cuTopping = topping.toLowerCase();
            boolean tt = false;
            while (tt==false){
                if(Arrays.asList(t).contains(cuTopping) == true){
                    tt = true;
                }
            }

            if (Arrays.asList(t).contains(cuTopping) == false){
                delay("Not an option, please pick again.", 40L);
                topping = sc.nextLine();
            }

            String cake = cuFlavor+cuTopping;
            
            boolean bb = false;
            while (bb == false){
                if(foundCakes.contains(cake)){
                    delay("Previously found recipe!", 40L);
                    delay(getRandomResponse(), 40L);
                    bb = true;
                }
            }

            boolean nc = true;
            while(nc == true){
                if(foundCakes.contains(cake) == false){
                    delay("You found a new cake!", 40L);
                    delay("Unlocked: "+cake, 40L);
                    foundCakes.add(cake);
                    achievements++;
                    delay("Number of recipes found: " + achievements, 40L);
                    nc = false;
                }
            }
            
        }
        System.exit(0);
    }

    public int getAchievements(){
        return achievements;
    }

    private static String getRandomResponse()
    {
        final int NUMBER_OF_RESPONSES = 4;
        double r = Math.random();
        int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
        String response = "";

        if (whichResponse == 0)
        {
            response = "Don't worry! Still delicious.";
        }
        else if (whichResponse == 1)
        {
            response = "What a classic cake!";
        }
        else if (whichResponse == 2)
        {
            response = "Yummy :)";
        }
        else if (whichResponse == 3)
        {
            response = "Smells nice!";
        }

        return response;
    }

    public String toString(){
        return "Flavors: "+Arrays.toString(f)+" Topping: "+Arrays.toString(t);
    }

    public static void delay(String s, long delay) throws InterruptedException {
        for ( int i= 0; i < s.length(); i++) { 
            // for loop delays individual String characters
            System.out.print(s.charAt(i));
            Thread.sleep(delay); //time is in milliseconds
        }
        System.out.println(""); // this is the space in between lines
    }
}

看看你的 while 循环。第一:

boolean oo = false;
while (oo){
    if (Arrays.asList(f).contains(cuFlavor)) {
        oo = true;
    }    
}

oo == false 以来 从未 进入此循环。

下一个:

boolean tt = false;
while (tt == false) {
    if (Arrays.asList(t).contains(cuTopping) == true) {
        tt = true;
    }
}

这个循环确实执行了,但是如果t不包含cuTopping会发生什么?在那种情况下,tt 永远不会设置为 true 并且循环会一直持续下去。

接下来的两个循环有同样的问题。

您需要确保循环会在某个时刻结束。示例:

while (tt == false) {
    if (Arrays.asList(t).contains(cuTopping) == true) {
        tt = true;
    }
    else {
        // Do something to change cuTopping
        System.out.println("Pick a topping");
        cuTopping = sc.nextLine();
        // etc....
    }
}

您可以将循环与输入的收集相结合:

String cuTopping = null;
do {
    if (cuTopping != null) { // Only true after first iteration
        System.out.println("That topping is not in the list!");
    }
    System.out.println("Pick a topping");
    cuTopping = sc.nextLine().toLowerCase();
    if (cuTopping.equalsIgnoreCase("quit")) {
        delay("Thanks for playing!", 40L);
        System.exit(0);
    }
} while (!Arrays.asList(t).contains(cuTopping));