Java 当再次运行程序时,如何使扫描仪暂停输入?

Java 当再次运行程序时,如何使扫描仪暂停输入?,java,user-input,Java,User Input,因此,为了测试到目前为止我对Java所学的知识,我决定制作一个程序来应用来自HeartStick的webcomicSol Captor的字符的书写技巧,以满足那些对用户使用switch语句输入感到好奇的人。到目前为止,程序运行良好,怪癖应用良好,但是,当我让程序询问用户是否希望再次运行程序时,当用户说是时,程序将再次运行,只是它没有暂停以允许用户键入另一条语句来应用怪癖。我尝试将所有声明(除了charrestart)放入while循环中,并添加行sol.Next;在System.out.prin

因此,为了测试到目前为止我对Java所学的知识,我决定制作一个程序来应用来自HeartStick的webcomicSol Captor的字符的书写技巧,以满足那些对用户使用switch语句输入感到好奇的人。到目前为止,程序运行良好,怪癖应用良好,但是,当我让程序询问用户是否希望再次运行程序时,当用户说是时,程序将再次运行,只是它没有暂停以允许用户键入另一条语句来应用怪癖。我尝试将所有声明(除了charrestart)放入while循环中,并添加行sol.Next;在System.out.printlnormal;之后;。这两种方法都不起作用,我在任何地方都找不到关于如何解决这个问题的任何信息。以下是我的代码供参考:

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);
            }
        }
    }
}

先谢谢你。

首先,我不认为使用扫描仪进行混合系统读取是个好主意。这似乎造成了一些问题,谁在使用输入的字符

说,替换这部分代码

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

解决了这个问题


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

我见过的最漂亮的缩进!您是如何编辑和编译代码的?如果您使用的是NetBeans或Eclipse之类的IDE,那么应该使用调试器逐步检查代码以了解发生了什么。或者,您可以将System.out.println调用添加到代码中,以便跟踪变量的执行和查看值。@DekDekku中断操作!至少代码是格式化的,不像这里90%的问题。但我喜欢它。它基本上是一个有限状态机。有点
// remove the last \n 
sol.nextLine();
// get the user response and consume the whole line
restart = sol.nextLine().charAt(0);