<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() 时遇到问题

        Trouble using nextInt and nextLine()(使用 nextInt 和 nextLine() 时遇到问题)

          • <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() 时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 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>
                      <tbody id='yBWRL'></tbody>

                    <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>