2
我一直在調試,從小時以下程序但我找不到爲什麼是塞納沒有采取輸入字段名掃描儀跳過輸入?
import java.util.Scanner;
public class Student
{
int rollNo;
String name;
double percentageMarks;
static Scanner input = new Scanner(System.in);
public void accept()
{
System.out.print("Enter roll no: ");
rollNo = input.nextInt();
System.out.print("Enter Name: ");
name = input.nextLine();
System.out.print("Enter percentageMarks: ");
percentageMarks = input.nextDouble();
}
public void display()
{
System.out.println("Name: " +name);
System.out.println("roll no: " +rollNo);
System.out.println("Percentage marks: " +percentageMarks);
}
public static void main(String[] args)
{
Student s1 = new Student(), s2 = new Student();
s1.accept();
s2.accept();
if(s1.percentageMarks>s2.percentageMarks)
s1.display();
else if (s2.percentageMarks>s1.percentageMarks)
s2.display();
else
System.out.println("Both students has same marks");
}
}
它產生出這樣的:
進入卷號:1 輸入名稱:輸入百分比標記:
如上所示,如果沒有允許輸入學生姓名,它的提示輸入學生百分比標記。
你可以閱讀這篇文章,我寫了澄清你對這個問題的懷疑:[在nextLine()']之前使用'nextInt()'(https://christprogramming.wordpress.com/2014/01/30/java -common-errors-1 /) – Christian
可能是掃描器將換行符(按Enter鍵)作爲輸入並跳到下一個輸入行。 –