【问题标题】:Switch statement in loop with unknown number of repetitions循环中的 Switch 语句,重复次数未知
【发布时间】:2020-07-18 01:38:24
【问题描述】:

您好,我正在做一个小任务,我需要构建一个重复次数未知的循环。我的代码有问题,当用户输入字符时,输出会无限显示。我该如何解决这个问题?

String[] action = {"x", "u", "d", "l", "r", "s", "h", "e"}; 

        System.out.println("Please enter an action");

    String input = sc.next();

    char selection;

        do{
            selection = input.charAt(0);

        switch(selection){

            case 'x': System.out.print("Bye!"); 
                break;
            case 'u' : System.out.print("You go one square up.");
                break;
            case 'd': System.out.print("You go one square down.");
                break;
            case 'l': System.out.print("You go one square left.");
                break;  
            case 'r': System.out.print("You go one square right.");
                break;      
            case 's': System.out.print("You search the square for treasure. You find nothing.");
                break;  
            case 'h': System.out.print("You hide, waiting for enemies to come by. It gets boring after about an hour and a half, so you give up.");
                break;  
            case 'e': System.out.print("You eat some food. You regain 0 hit points");
                break;
            case 'z': 
                break;
            default: 
                System.out.println("I dont understand");
                break;
        }
        }
            while (selection != 'z');
        ```

【问题讨论】:

    标签: java arrays loops switch-statement do-while


    【解决方案1】:

    sc.next() 暂停执行,直到用户进行输入。
    但是,由于它在循环之外,它不会停止读取新输入。
    因此,您需要将next() 调用移动到循环中。

    do{
      String input = sc.next();
      selection = input.charAt(0);
    
      switch(selection){
      // rest of the code
    
    }
    

    【讨论】:

      【解决方案2】:

      您还需要将用户输入放入 do-while 循环中。所以它在每次迭代后都要求新的输入。我目前正在使用手机,因此无法显示。但它很容易解决。

      【讨论】:

        【解决方案3】:

        首先,您需要逐个字符地读取输入。由于您已经输入了一个单词,因此您需要从索引 0 开始并继续移动到 do-while 循环中的下一个字符。为此:

        String[] action = {"x", "u", "d", "l", "r", "s", "h", "e"}; 
                System.out.println("Please enter an action:");
                Scanner sc = new Scanner(System.in);
            String input = sc.next();
            char selection;
            int i = 0;
                do{
                    selection = input.charAt(i++);
        
                switch(selection){
        
                    case 'x': System.out.print("Bye!"); 
                        break;
                    case 'u' : System.out.print("You go one square up.");
                        break;
                    case 'd': System.out.print("You go one square down.");
                        break;
                    case 'l': System.out.print("You go one square left.");
                        break;  
                    case 'r': System.out.print("You go one square right.");
                        break;      
                    case 's': System.out.print("You search the square for treasure. You find nothing.");
                        break;  
                    case 'h': System.out.print("You hide, waiting for enemies to come by. It gets boring after about an hour and a half, so you give up.");
                        break;  
                    case 'e': System.out.print("You eat some food. You regain 0 hit points");
                        break;
                    case 'z': 
                        break;
                    default: 
                        System.out.println("I dont understand");
                        break;
              }
            } while (i < input.length() && selection != 'z');
        

        您需要在while() 中添加另一个条件,以避免超出输入字符串的大小。另外,我不明白你为什么需要一开始创建的字符串数组action

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-17
          • 2013-04-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多