简体   繁体   中英

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(); 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(); 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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