2016-09-15 164 views
-1

我有這樣一個任務:Java輸入,掃描儀和if語句

寫一個完整的程序稱爲ExamResult與行爲如下所示。 閱讀用戶輸入內容,瞭解學生的內部考試分數和期末考試分數。

內部測試分數低於20會導致學生失敗; 40以下的最終考試成績也會導致學生失敗。

否則學生已通過。

學期考試成績!

你的內部測試分數是多少? 21

你的期末考試分數是多少? 69

你已經通過了!

到目前爲止,我已經發現使用if語句和掃描器進行輸入是合適的。 現在這是我的代碼(我有課,進口的Java UTIL及以上主梅索德。我選擇不包括在這裏)

System.out.println("Semester Examination Results!"); 

    System.out.println("What is your internal test score?"); 

    //input 
    //calculate input 
    //does input pass the if test? 
    //output passed/failed 

    int score1 = scanner.nextLine(); 
    int score2 = scanner.nextLine(); 

    if (int score1 < 20; || score2 < 40;) 
    { 
    System.out.println("You have failed"); 
    } 
    else 
    { 
    System.out.println("You have passed"); 
    } 

我不確定什麼是如何使用輸入計算?

+2

'if(int score1 <20; || score2 <40;)'不會很好地結束。刪除';'和'int'。 – Bathsheba

+0

我強烈建議閱讀初學者的java教程,然後在Stack Overflow上發佈問題。提問「經過深入研究」的基本問題是可以的:) – TheLostMind

+0

我一遍又一遍地閱讀我的書,並試圖研究,它只是因爲它太新了,所以我很容易發作:P –

回答

2

使用scanner.nextInt()而不是nextLine()。你也不需要if語句中的分號。

Edit1

您不需要在if語句中重新初始化score1和2。它ahould這個樣子

Scanner scanner = new Scanner(System.in); 
//init and read in values here 
if(score1 < 20 || score2 < 40){ 
    //do stuff 
} 
else{ 
    //do other stuff 
} 
+0

錯誤:找不到符號 int score2 = scanner.nextInt(); 找不到符號掃描儀,我該怎麼辦? –

+0

@TinaDitte - 你還沒有聲明/初始化一個'Scanner',在'int score1'之前添加'Scanner sc = new Scanner(System.in)'' – TheLostMind

+0

已更新我的回答以反映這一點。 –

1
  • 刪除;從條件,這些都不是任何聲明。也刪除int

    if (int score1 < 20; || score2 < 40;) 
    

    應該是:

    if (score1 < 20 || score2 < 40) 
    
  • 此外,使用nextInt()Integer.parseInt(scanner.nextLine())用於取整數輸入。