java - 为什么java中不能连续输入两个字符串?

标签 java

我的程序在输入一个字符串后终止。它不需要字符串 2 输入。

这是代码片段:

    public static void main (String[] args) {
        Scanner scn= new Scanner(System.in);
        int T ;
        T=scn.nextInt();
        while(T!=0){
            String s1 = scn.nextLine(); // after i give input for the first string ,program terminates!
            String s2 = scn.nextLine(); // program terminates before asking for s2 input
            System.out.println(isMetaString(s1,s2));
            T--;
        }
    }

最佳答案

public static void main (String[] args) {
    Scanner scn= new Scanner(System.in);
    int T ;
    T=scn.nextInt();

    // scn.nextLine();  // for enter.
    // here after entering int value, 
    // you must be pressing enter,
    // that 'enter' is getting stored in s1,
    // and then when you give first string it is getting stored in s2 
    // and that is why your program is terminating.

    while(T!=0){
        String s1 = scn.nextLine(); // after i give input for the first string ,program terminates!
        String s2 = scn.nextLine(); // program terminates before asking for s2 input
        System.out.println(isMetaString(s1,s2));
        T--;
    }
}

解决方案:在 sca.nextInt() 之后添加 sca.nextLine() 即可。将解决问题

关于java - 为什么java中不能连续输入两个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48475000/

相关文章:

java - 使用正则表达式检查字符串是否包含表情符号

java仅使用逻辑,没有日历,日期或类似的两个日期之间的差异

java - 在 Matlab 中将 java.util.HashMap 键转换为 vector

java - 当连续按下 `System.in` 键两次时如何终止 "Enter"键盘流?

java - IntelliJ 与 spring-boot-jetty-jsp

java - 如何让Java使用多线程线程?

java - 为 Math.Random 创建武器数组以供选择

java - 以格式 HH :MM:SS 连续减少 java 中的时间

java - 如何在 Ubuntu 上设置 JAVA_HOME 路径?

java - 如何在 JLabel 中设置填充