【问题标题】:reading input with Scanner; output is printed twice [duplicate]使用扫描仪读取输入;输出打印两次[重复]
【发布时间】:2014-08-27 11:04:17
【问题描述】:

我有以下课程

import java.util.Scanner;

public class Album{

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        System.out.println("How many songs do your CD contain?");
        int songs = sc.nextInt();

        String[] songNames = new String[songs];

        for (int i = 0; i < songs; i++) {
            System.out.println("Please enter song nr " + (i+1) + ": ");
            songNames[i] = sc.nextLine();
            // What is wrong here? (See result for this line of code)
            // It is working when I use "sc.next();"
            // but then I can't type a song with 2 or more words.
            // Takes every word for a new song name. 
        }

        System.out.println();
        System.out.println("Your CD contains:");
        System.out.println("=================");
        System.out.println();

        for (int i = 0; i < songNames.length; i++) {
            System.out.println("Song nr " + (i+1) + ": " + songNames[i]);
        }
    }
}

我无法输入歌曲名称 nr 1,因为它总是同时显示前两个。

如果我输入 3,就像这样:

How many songs do your CD contain?
3
Please enter song nr 1:
Please enter song nr 2: 

【问题讨论】:

  • 我编辑了你的问题,给它一个更好的标题。当然,你的旧标题给人们一个挑战,让人们做一些代码检测工作来发现你的代码中的问题,它很好地达到了它的目的,有 5 个答案,但是前面的 我的代码有什么问题?检查我的评论行可能适合任意多个问题。

标签: java string java.util.scanner


【解决方案1】:

改变

int songs = sc.nextInt();

到:

int songs = Integer.parseInt(sc.nextLine().trim());

它会正常工作的。

您不应将nextIntnextLine 混合使用。

【讨论】:

    【解决方案2】:

    int songs = sc.nextInt();之后添加sc.nextLine();

    一旦您输入一个数字并使用扫描仪(作为数字)使用sc.nextInt(); 读取它,换行符 字符将出现在输入流中,当您执行@987654324 时将读取该字符@。所以,要跳过它,你需要在sc.nextInt();之后调用sc.nextLine()

    【讨论】:

    • 感谢您的解释!
    【解决方案3】:

    sc.nextInt() 之后添加sc.nextLine(),您的代码就可以正常工作了。

    原因是输入歌曲的编号后行尾。

    【讨论】:

      【解决方案4】:

      要么使用nextIntnextLine,我会选择nextLine

      import java.util.Scanner;
      
      public class Album{
      
        public static void main(String[] args){
      
          Scanner sc = new Scanner(System.in);
          System.out.println("How many songs do your CD contain?");
          int songs = Integer.parseInt(sc.nextLine()); // instead of nextInt()
      
          String[] songNames = new String[songs];
      
          for (int i = 0; i < songs; i++) {
              System.out.println("Please enter song nr " + (i+1) + ": ");
              songNames[i] = sc.nextLine();
              // What is wrong here? (See result for this line of code)
              // It is working when I use "sc.next();"
              // but then I can't type a song with 2 or more words.
              // Takes every word for a new song name. 
          }
      
          System.out.println();
          System.out.println("Your CD contains:");
          System.out.println("=================");
          System.out.println();
      
          for (int i = 0; i < songNames.length; i++) {
              System.out.println("Song nr " + (i+1) + ": " + songNames[i]);
          }
        }
      }
      

      【讨论】:

        【解决方案5】:

        放一个 sc.nextLine(); int 歌曲之后 = sc.nextInt();

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-07
          • 1970-01-01
          • 1970-01-01
          • 2018-05-30
          • 2016-06-12
          相关资源
          最近更新 更多