<small id='lQ5xR'></small><noframes id='lQ5xR'>

    <legend id='lQ5xR'><style id='lQ5xR'><dir id='lQ5xR'><q id='lQ5xR'></q></dir></style></legend>
      <bdo id='lQ5xR'></bdo><ul id='lQ5xR'></ul>
    <tfoot id='lQ5xR'></tfoot>

        <i id='lQ5xR'><tr id='lQ5xR'><dt id='lQ5xR'><q id='lQ5xR'><span id='lQ5xR'><b id='lQ5xR'><form id='lQ5xR'><ins id='lQ5xR'></ins><ul id='lQ5xR'></ul><sub id='lQ5xR'></sub></form><legend id='lQ5xR'></legend><bdo id='lQ5xR'><pre id='lQ5xR'><center id='lQ5xR'></center></pre></bdo></b><th id='lQ5xR'></th></span></q></dt></tr></i><div id='lQ5xR'><tfoot id='lQ5xR'></tfoot><dl id='lQ5xR'><fieldset id='lQ5xR'></fieldset></dl></div>
      1. 使用 nextInt 和 nextLine() 时遇到问题

        时间:2023-05-28

          • <bdo id='e8cll'></bdo><ul id='e8cll'></ul>

              <small id='e8cll'></small><noframes id='e8cll'>

              <tfoot id='e8cll'></tfoot>

                <i id='e8cll'><tr id='e8cll'><dt id='e8cll'><q id='e8cll'><span id='e8cll'><b id='e8cll'><form id='e8cll'><ins id='e8cll'></ins><ul id='e8cll'></ul><sub id='e8cll'></sub></form><legend id='e8cll'></legend><bdo id='e8cll'><pre id='e8cll'><center id='e8cll'></center></pre></bdo></b><th id='e8cll'></th></span></q></dt></tr></i><div id='e8cll'><tfoot id='e8cll'></tfoot><dl id='e8cll'><fieldset id='e8cll'></fieldset></dl></div>
                • <legend id='e8cll'><style id='e8cll'><dir id='e8cll'><q id='e8cll'></q></dir></style></legend>
                    <tbody id='e8cll'></tbody>
                • 本文介绍了使用 nextInt 和 nextLine() 时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这不是让我输入我的名字,但它确实可以正常工作.我知道我可以更改语句的顺序,但还有其他方法可以做到吗?

                  It's not letting me put my name in but it does the age works fine. I know i can change the order of the statements but is there another way I could do it?

                  import java.util.Scanner;
                  
                  public class ScannerErr2
                  {
                    public static void main(String [] args)
                    {
                      Scanner keyboard= new Scanner(System.in);
                      String name;
                      int    age;
                  
                      System.out.print("Enter your age : ");
                      age= keyboard.nextInt();
                      System.out.print("Enter your name: ");
                      name= keyboard.nextLine();
                  
                      System.out.println("Age : "+age);
                      System.out.println("Name: "+name);
                    }
                  }
                  

                  推荐答案

                  你的问题是下一个 int 没有考虑你名字部分的输入中的换行符.因此名称返回为空白.

                  You problem is that the next int doesn't consider the new line character which goes in the input for your name part. Hence name is returned as blank.

                  您可以通过 2 种方式更改代码:

                  You can change your code in 2 ways:

                  System.out.print("Enter your age : ");
                  age = keyboard.nextInt();
                  keyboard.nextLine();
                  System.out.print("Enter your name: ");
                  name = keyboard.nextLine();
                  

                  System.out.print("Enter your age : ");
                  age = Integer.parseInt(keyboard.nextLine().trim());
                  System.out.print("Enter your name: ");
                  name = keyboard.nextLine();
                  

                  我个人喜欢第二种方式.

                  I personally like the second way.

                  这篇关于使用 nextInt 和 nextLine() 时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在java中将两个整数除以一个double 下一篇:Java int 部门让我感到困惑

                  相关文章

                  <small id='yBWRL'></small><noframes id='yBWRL'>

                • <i id='yBWRL'><tr id='yBWRL'><dt id='yBWRL'><q id='yBWRL'><span id='yBWRL'><b id='yBWRL'><form id='yBWRL'><ins id='yBWRL'></ins><ul id='yBWRL'></ul><sub id='yBWRL'></sub></form><legend id='yBWRL'></legend><bdo id='yBWRL'><pre id='yBWRL'><center id='yBWRL'></center></pre></bdo></b><th id='yBWRL'></th></span></q></dt></tr></i><div id='yBWRL'><tfoot id='yBWRL'></tfoot><dl id='yBWRL'><fieldset id='yBWRL'></fieldset></dl></div>
                • <tfoot id='yBWRL'></tfoot>
                • <legend id='yBWRL'><style id='yBWRL'><dir id='yBWRL'><q id='yBWRL'></q></dir></style></legend>
                    • <bdo id='yBWRL'></bdo><ul id='yBWRL'></ul>