2014-12-06 154 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"));