Java 爪哇:nextLine Dilema

Java 爪哇:nextLine Dilema,java,Java,我已经看到了一些类似于这个问题的线索,但是很难找到我特定问题的解决方案 我写的代码是让老师输入学生的姓名和成绩,并接收最终字母成绩的输出 我遇到的问题是,姓氏输入与名字输入组合在一起,而不是单独提问。如何提示这些问题以单独问题的形式出现 import java.util.*; public class AssignmentTest { public static void main (String [] args) { Scanner console = new Scan

我已经看到了一些类似于这个问题的线索,但是很难找到我特定问题的解决方案

我写的代码是让老师输入学生的姓名和成绩,并接收最终字母成绩的输出

我遇到的问题是,姓氏输入与名字输入组合在一起,而不是单独提问。如何提示这些问题以单独问题的形式出现

import java.util.*;

public class AssignmentTest {

   public static void main (String [] args) {

      Scanner console = new Scanner (System.in);


      int iStudentID = 0; //StudentID
      String sLastName = ""; //Last Name
      String sFirstName = ""; //First Name
      int iAssignmentsScore = 0; //Assignment Input
      int iQuizzesScore = 0; //Quizzes Input
      int iMidtermsScore = 0; //Midterm Input
      int iFinalScore = 0;; //Final Input




      //USER INPUT

      //StudentID Input
      System.out.println("Please enter the StudentID: ");
      iStudentID = console.nextInt() ;


      //Last Name Input
      System.out.println ("Please enter the student's last name: ");
      sLastName = console.nextLine() ;

      //First Name Input
      System.out.println ("Please enter the student's first name: ");
      sFirstName = console.nextLine() ;

      //Assignment Input
      System.out.println ("Please enter the student's assignment score: ");
      iAssignmentsScore = console.nextInt() ;

      //Quiz Input
      System.out.println ("Please enter the student's quiz score: ");
      iQuizzesScore = console.nextInt ();

      //Midterm Input
      System.out.println ("Please enter the student's midterm score: ");
      iMidtermsScore = console.nextInt ();

      //Final Input
      System.out.println ("Please enter the student's final score: ");
      iFinalScore = console.nextInt ();

   }

}
试试这个

System.out.println ("Please enter the student's last name: ");
sLastName = console.nextLine() ;
console.nextLine();
试试这个

System.out.println ("Please enter the student's last name: ");
sLastName = console.nextLine() ;
console.nextLine();

我认为这可能与添加另一个新行字符一样简单\n。比如说

System.out.println ("\nPlease enter the student's first name: ");

我认为这可能与添加另一个新行字符一样简单\n。比如说

System.out.println ("\nPlease enter the student's first name: ");
由于sFirstName和lFirstName都由一个您应该使用的单词组成

下一个

查找并返回此扫描程序中的下一个完整令牌

而不是

下一行

使扫描器前进超过当前行并返回 被跳过了

@程序员有更好的解释:

事实上,问题是在iStudentID=console.nextInt;返回, 缓冲区中仍有一个新行字符,因此 sLastName=console.nextLine;被称为,它跳过了这个 自动返回空字符串-这是一个非常常见的错误 误会

由于sFirstName和lFirstName都由一个您应该使用的单词组成

下一个

查找并返回此扫描程序中的下一个完整令牌

而不是

下一行

使扫描器前进超过当前行并返回 被跳过了

@程序员有更好的解释:

事实上,问题是在iStudentID=console.nextInt;返回, 缓冲区中仍有一个新行字符,因此 sLastName=console.nextLine;被称为,它跳过了这个 自动返回空字符串-这是一个非常常见的错误 误会


您应该使用next而不是nextLine

您应该使用next而不是nextLine

为什么不使用next,因为它将识别令牌?为什么不使用next,因为它将识别令牌?实际上,问题是在iStudentID=console.nextInt;返回时,缓冲区中仍有新行字符,因此当sLastName=console.nextLine;调用时,它会跳过此操作,自动返回空字符串-这是一个非常常见的错误mistake@SyoSyo请看一看madprogrammer的解释:实际上,问题是在iStudentID=console.nextInt之后;返回时,缓冲区中仍有新行字符,因此当sLastName=console.nextLine;调用时,它会跳过此操作,自动返回空字符串-这是一个非常常见的错误mistake@SyoSyo请同时看一看madprogrammer的解释: