2014-12-06 328 views
-5
public class StudentGrades { 

    String studentID; 
    Integer numericGrade; 

    Scanner input = new Scanner(System.in); 

public void loadStudentGrades(){ 
    do{ 
     System.out.println("Please enter a Student ID, or enter 'end' to exit: "); 
     studentID = input.next(); 
     System.out.println("Please enter numeric grade for the ID above: "); 
     numericGrade = input.nextInt(); 
     map.put(studentID, numericGrade); 
     } 
    while (studentID !String.equals("end")); //this is throwing an error. How is it possible to get this to work? 
    } 
} 

我工作的這個類和我發現很難讓我的同時,部分的Java布爾表達式do-while循環上班的路上,我就期待。我想說的是,studentID不等於「結束」以通過循環。在do-while循環的String

+4

你認爲'studentID!String.equals(「end」)應該做什麼?爲什麼? – 2014-12-06 22:18:40

+0

http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – 2014-12-06 22:20:48

+0

我認爲它應該比較變量studentID「結束」,如果studentID等於「結束」,然後結束循環。我是Java新手,爲什麼我認爲它應該這樣做?那麼,我還沒有介紹如何比較布爾格式的字符串,以及我在網上看到的,我認爲我應該工作,因爲我知道.equals()用於比較字符串,而不是!=對於整數。下面給出的例子也不起作用,即使我爲studentID輸入「end」,循環也會繼續。 – javan 2014-12-06 22:34:40

回答

2

如下您while狀況應該改變:

while (!studentID.equals("end")) 

您正在調用equals方法不正確,因爲方法不知道的方式,你正試圖比較studentID到「結束」;所有你給它的是「結束」。

1

你應該寫

while(!studentID.contentEquals("end"));