使用单个扫描仪对象扫描多行

新手上路,请多包涵

我是 Java 的新手,所以如果这对您来说听起来绝对愚蠢,请不要打分

好的,我如何使用单个扫描仪对象输入它

5个

你好,你怎么样

欢迎来到我的世界

6 7

对于那些建议的人

scannerobj.nextInt->nextLine->nextLine->nextInt->nextInt,,,

查了一下,没用!!!

谢谢

原文由 Creative_Cimmons 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 358
2 个回答
public static void main(String[] args) {
    Scanner  in    = new Scanner(System.in);

    System.out.printf("Please specify how many lines you want to enter: ");
    String[] input = new String[in.nextInt()];
    in.nextLine(); //consuming the <enter> from input above

    for (int i = 0; i < input.length; i++) {
        input[i] = in.nextLine();
    }

    System.out.printf("\nYour input:\n");
    for (String s : input) {
        System.out.println(s);
    }
}

样本执行:

 Please specify how many lines you want to enter: 3
Line1
Line2
Line3

Your input:
Line1
Line2
Line3

原文由 ifloop 发布,翻译遵循 CC BY-SA 3.0 许可协议

public class Sol{

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

       while(sc.hasNextLine()){

           System.out.println(sc.nextLine());
       }

    }
}

原文由 Raghuveer Reddy 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题