简体   繁体   English

为什么输入不打印

[英]Why does the input not print out

I just want it to display my name. 我只希望它显示我的名字。 It worked in a different code but I cut out the parts that were supposed to say the name. 它以不同的代码工作,但我删去了应该说出名称的部分。

import java.util.Scanner;
public class ComputePay
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      String name;

         System.out.print("Please enter your First and Last name >> ");
         input.nextLine();
         name = input.nextLine();

            System.out.println("Thank you, " + name);
   }
}

When you do your input.nextLine(); 当您input.nextLine(); for the first time, you don't save the result to any variable. 第一次,您没有将结果保存到任何变量。 So you are loosing the value that the user entered before. 因此,您失去了用户之前输入的值。

If you just remove that line then name = input.nextLine(); 如果只删除该行,那么name = input.nextLine(); will successfully read the value and store it in the variable name . 将成功读取值并将其存储在变量名称中

If you want to read in multiple values just repeat that process: 如果要读取多个值,请重复该过程:

System.out.print("Please enter your first name >> ");
firstName = input.nextLine();
System.out.print("Please enter your last name >> ");
lastName = input.nextLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM