【问题标题】:How do you get the Scanner to pause for input when running the program again?再次运行程序时如何让扫描仪暂停输入?
【发布时间】:2013-05-12 07:42:47
【问题描述】:

因此,作为对自己的一个小挑战,以测试我迄今为止对 Java 的了解,我决定编写一个程序,将网络漫画中一个角色的写作怪癖(对于那些好奇的人,来自 Heartstuck 的 Sol Captor)应用到使用 switch 语句的用户输入。到目前为止,该程序运行良好,并且应用正常,但是,当我让程序询问用户是否要再次运行该程序时,当说是时,该程序将再次运行,除非它没有t 暂停以允许用户键入另一个语句以应用该怪癖。我已经尝试将所有声明(除了 char restart 之外)放在 while 循环中并添加行“sol.Next();”在 "System.out.println("Normal");." 之后这些都没有奏效,我在任何地方都找不到有关如何解决此问题的任何信息。这是我的参考代码:

import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SolQuirk {

    public static void main(String[] args) {
        char restart = 'y';
        Scanner sol = new Scanner(System.in).useDelimiter("");
        String input = "";
        while(restart == 'y' | restart == 'n'){
            System.out.println("Normal:");
            while(!sol.hasNext("\n")){
                char ch = sol.next().charAt(0);
                switch(ch){
                    case 's': case 'S':
                        input += '2';
                        break;
                    case ' ':
                        input += ch;
                        ch = sol.next().charAt(0);
                        if(ch == 't' || ch == 'T'){
                            ch = sol.next().charAt(0);
                            if(ch == 'o' || ch == 'O'){
                                ch = sol.next().charAt(0);
                                if(ch == 'o' || ch == 'O'){
                                    ch = sol.next().charAt(0);
                                    if(ch == ' ' || ch == '.'){
                                        input += "two";
                                        input += ch;
                                        break;
                                    }
                                    else{
                                        input += "too";
                                        input += ch;
                                        break;
                                    }
                                }
                                else if(ch == ' ' || ch == '.'){
                                    input += "two";
                                    input += ch;
                                    break;
                                }
                                else{
                                    input += "to";
                                    input += ch;
                                    break;
                                }
                            }
                            else{
                                input += "t";
                                input += ch;
                                break;
                            }
                        }
                    default:
                        input += ch;
                        break;
                }
            }
            String solQuirk = input.toLowerCase();
            System.out.println("Sol:");
            System.out.println(solQuirk);
            System.out.println("Would you like to go again? y/n");
            try {
                restart = (char) System.in.read();
            } catch (IOException ex) {
                Logger.getLogger(SolQuirk.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

提前谢谢你。

【问题讨论】:

  • 我见过最漂亮的压痕!
  • 你是如何编辑和编译你的代码的?如果您使用的是诸如 NetBeans 或 Eclipse 之类的 IDE,您应该使用调试器来单步调试您的代码以找出正在发生的事情。或者,您可以在代码中添加System.out.println() 调用,以便跟踪变量的执行和查看值。
  • @DekDekku 让 OP 休息一下!至少代码格式的,不像这里90%的问题。
  • 但我喜欢它。它基本上是一个有限状态机。种。

标签: java user-input


【解决方案1】:

首先,我不认为将System.in.readScanner 混合是一个好主意。这似乎导致了谁在使用正在输入的字符方面的某种问题。

说了,替换你的这部分代码

try {
     restart = (char) System.in.read();
} catch (IOException ex) {
     Logger.getLogger(SolQuirk.class.getName()).log(Level.SEVERE, null, ex);
}

// remove the last \n 
sol.nextLine();
// get the user response and consume the whole line
restart = sol.nextLine().charAt(0);

解决问题。

我不确定你的代码错误在哪里,但它与 \n 没有被消耗有关,因此没有进入你的古怪循环。

【讨论】:

    猜你喜欢
    • 2015-03-22
    • 1970-01-01
    • 2015-08-27
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    相关资源
    最近更新 更多