2014-09-30 57 views
-5


我需要在驗證數據領域的幫助。出於某種原因,我不斷收到不兼容的錯誤。我現在檢查了幾次,我有正確的類型。有什麼不對,2班。錯誤是int驅動程序類繼續在「name = student.setName(input);」中引入不兼容tyoes。請解釋爲什麼?Java:沒理由的錯誤

/* 
* Joel Gordon 
* per.4 
*/ 
import java.util.Scanner; 
public class P5A 
    { 
    public static void main (String args[]) 
    { 
    Scanner reader = new Scanner(System.in); 

    student student = new student(); 

    String name, validate, valtests; 
    int tests, score, count = 100; 
    String input = reader.nextLine(); 

    System.out.println("Please enter the students Name: " + input); 
    name = student.setName(input); 
    validate = student.validateData(name); 

    System.out.print("Please enter Test " + count + "(As number 1-3 for test number, then test   score): "); 
    tests = student.getScore(reader.nextInt(), reader.nextInt()); 
    valtests = student.validateTests(tests); 

     System.out.println(stu.toString()); 



    }//end of main mthod 
    }//end of main class/Driver 

學生類驅動程序結束。

public class student 
{ 
private String name, result; 
private int test1, test2, test3; 

public student() 
{ 
    name = ""; 
    test1 = 0; 
    test2 = 0; 
    test3 = 0; 
    result = ""; 
}//constructor 

public void setName (String nm) 
{ 
    name = nm; 
}//setting the name in the program 

public String getName() 
{ 
    return name; 
}//getting the name 

public int getScore (int i, int score) 
{ 
    if (i == 1) test1 = score; 
    else if(i == 2)test2 = score; 
    else test3 = score; 

    if (i == 1)return test1; 
    else if (i == 2) return test2; 
    else return test3; 
}//getting score of tests 

public int getAverage() 
{ 
    int average; 
    average = (int) Math.round((test1 + test2 + test3)/ 3.0); 
    return average; 
}//getting a average of all the scores 

public int getHighScore() 
{ 
    int highscore; 
    highscore = test1; 
    if (test2 > highscore) highscore = test2; 
    if (test3 > highscore)highscore = test3; 
    return highscore; 
}//getting the highscores of all three 

public String validateData(String name) 
{ 

    if (name.equals("")) result = "You have entered an invalid name, Please restart." ; 

    return result; 
} 

public String validateTests (int tests) 
{ 
    if (test1 >= 0 && test1 <= 100 || test2 >= 0 && test2 <= 100 || test3 >= 0 && test3 <= 100)  result = " You have entered an invalid number, between 1-100. " + 
    "Please restart!" ; 
    return result; 
}//getting the test scores and tesing each one against method 

public String toString() 
{ 
    String str; 
    str = "Name: " + name + 
      "\nTest1: " + test1 + 
      "\nTest2: " + test2 + 
      "\nTest3: " + test3 + 
      "\nAverage: " + getAverage() + 
      "\nHighscore: " + getHighScore(); 
    return str; 
}//putting all the tests together to view in termainal 


} 
+0

你應該在這裏閱讀'if-then-else':http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html – 2014-09-30 03:17:01

回答

1

您正在努力控制流量。

正在發生的事情是,當你打電話給你teacher構造,無論是什麼,下面的代碼行始終執行:

System.out.println ("I don't have a teacher in that room."); 
    System.out.println("Always show"); 

有幾種方法來解決這個問題:

第一,您可以在if聲明中輸入return;

或者您可以使用if然後else if然後else if然後else作爲您的每個條件的最後一個。您可以使用switch聲明。

0

你應該讓老師代表老師。然後,在教師課堂外創建教師對象的實例。

public class Teacher{ 
    private String name; 
    private String catchPhrase; 
    private int roomNumber; 

    public(String name, String catchPhrase, int roomNumber){ 
     this.name = name; 
     this.catchPhrase = catchPhrase; 
     this.roomNumber = roomNumber; 
    } 

    //put setters and getters here... 


    public String toString(){ 
    return "Name: "+this.getName()+"\nCatch Phrase: "+this.getCatchPhrase() + "\nRoom Number: 
      "+this.getRoomNumber(); 
    } 

} 

然後,您可以創建教師對象。例如,你可以把它們放在TeacherDriver類中。

public class TeacherDriver{ 
    public static void main (String[] args){ 
    //create array of teachers (you can use list and other data structures if you want) 
    Teacher[] myTeachers = new Teacher[]{ 
           new Teacher("Mr. Clark", "Just do it", 225), 
           new Teacher("Mr. Harris", "Do the essays and you will pass.", 123) 
          }; 

    //ask for room number input here... 

    //mark this truE if teacher is found 
    boolean found = false; 
    //search the array of teachers for a match 
    for(Teacher chosenTeacher : myTeachers) 
     //if the room number of the teacher matches your target room number 
     //then display teacher info and stop reading the array 
     //note: replace <your input room number> with your input variable name 
     if(chosenTeacher.getRoomNumber() == <your input room number>){ 
      found = true; 
      System.out.println(chosenTeacher.toString()); 
      break; //stop the loop 

     } 
    } 

    if(!found){ 
     System.out.println ("I don't have a teacher in that room."); 
    } 
} 

一個更好的辦法來做到這一點,雖然是創建一個擁有教師對象的集合/數組的類。

例子:

public class TeacherList{ 
    List<Teacher> teachers; 

    public TeacherList(){ 
      teachers = new ArrayList<>(); 

      //you can put initial entries like: 
      teacher.add(new Teacher("Mr. Clark", "Just do it", 225)); 
      teacher.add(new Teacher("Mr. Harris", "Do the essays and you will pass.", 123)); 
    } 

    public void add(Teacher e){ 
     teachers.add(e); 
    } 

    public void findTeacher(int roomNumber){ 
      //mark this tru if teacher is found 
      boolean found = false; 
      //search 
      for(Teacher chosenTeacher : teachers) 
     //if the room number of the teacher matches your target room number 
     //then display teacher info and stop reading the array 
     //note: replace <your input room number> with your input variable name 
      if(chosenTeacher.getRoomNumber() == <your input room number>){ 
       found = true; 
       System.out.println(chosenTeacher.toString()); 
       break; //stop the loop 

      } 
      } 

     if(!found){ 
      System.out.println ("I don't have a teacher in that room."); 
     } 

     } 

} 

然後,在你的主:

//create Teacher list 
TeacherList myTeachers = new TeacherList(); 

//input room number here... 

//replace <room number? with input variable name 
myTeachers.findTeacher(<room number>); 

如果你想接受多個房間號碼,然後你可以把代碼的最後兩行以上的循環中。