2017-04-17 125 views
-3

我還沒有完成這個,但如果我已經有錯誤沒有進展。這裏是我的代碼..2錯誤:表達式的非法開始[Java]

public static void main(String[] args) 
{ 
    Scanner inputDevice = new Scanner(System.in); 
    System.out.print("Please enter the name of student ==> "); 
    studentName = inputDevice.nextInt(); 
    inputDevice.nextLine(); 
    System.out.printIn("Enter the mark for student "+ studentName " out of 65 ==> "); 
    studentMark = inputDevice.nextInt(); 
    inputDevice.nextLine(); 
} 

1號錯誤: ')' 預期

System.out.printIn("Enter the mark for student "+ studentName " out of 65 ==> ");

第二個錯誤:表達非法啓動

System.out.printIn("Enter the mark for student "+ studentName " out of 65 ==> ");

我非常新的哈哈。我不明白爲什麼我的第二打印是給我一個錯誤

+0

'studentName'應該是'字符串studentName','studentMark'應該是'INT studentMark',不是嗎?此外,您永遠不會將'nextLine()'的返回值賦值給任何東西... – domsson

+0

哇,7行中有5行有錯誤或不需要。我給你寫了一份所有問題的清單,以便你能夠理解和解決它們,更重要的是,在未來阻止這些問題。然而,下一次請給自己一杯咖啡,仔細閱讀你的代碼(以及錯誤信息),以便在這裏詢問之前自己發現一些這些問題。 – domsson

回答

1

基本上,幾乎所有是錯在這裏。

問題#1 - 3號線

studentName = inputDevice.nextInt(); 

你沒有正確申報studentName。當你想要一個名字(String)時,你也試圖獲得int。相反,該行應爲:

String studentName = inputDevice.nextLine(); 

問題#2 - 4行

inputDevice.nextLine(); 

這是什麼線在這裏做什麼?你沒有把輸入分配給任何東西。只需完全刪除此行。

問題3 - 5行

System.out.printIn("Enter the mark for student "+ studentName " out of 65 ==> "); 

你缺少一個+。此行應爲:

System.out.printIn("Enter the mark for student "+ studentName +" out of 65 ==> "); 

問題#4 - 6號線

studentMark = inputDevice.nextInt(); 

同樣,你沒有正確聲明變量。它應該是:

int studentMark = inputDevice.nextInt(); 

問題#5 - 7行

inputDevice.nextLine(); 

就像第2期,此行實現了什麼。去掉它。

摘要

您的代碼應該(可能)這樣寫的,而不是:

Scanner inputDevice = new Scanner(System.in); 
    System.out.print("Please enter the name of student ==> "); 
    String studentName = inputDevice.nextLine(); 
    System.out.println("Enter the mark for student "+ studentName + " out of 65 ==> "); 
    int studentMark = inputDevice.nextInt(); 
+0

非常感謝您的幫助! – pnkrtn

-1

替換此

System.out.printIn("Enter the mark for student "+ studentName " out of 65 ==> "); 

System.out.printIn("Enter the mark for student "+ studentName +" out of 65 ==> "); 
+0

他們的代碼有很多錯誤。 – domsson

-2

我想,你應該你不聲明變量。 掃描儀輸入=新掃描儀(System.in);

String studentName = inputDevice.nextLine(); int studentMark = inputDevice.nextInt();