2014-10-11 75 views
0

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

import java.util.Scanner; 
public class P5A 
{ 
public static void main (String args[]) 
{ 
    System.out.println("Always Show"); 

    Scanner reader = new Scanner(System.in); 

    student student = new student(); 

    String name, validate, valtests; 
    int tests, score; 


    System.out.print("Please enter the students Name: "); 
    String input = reader.nextLine(); 
    student.getName(input); 
    student.validateData(); 
     System.out.print("Please enter Test (As number 1-3 for test number, then test score): "); 
     tests = student.getScore(reader.nextInt(), reader.nextInt()); 
     student.setTests(tests); 

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

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

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



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

這裏是它已經被編輯因爲這個問題被張貼第二類,

import java.util.*; 
import java.lang.*; 
public class student 
{ 
private String name, result, testchange; 
private int test1, test2, test3; 

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

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

public String validateData() 
{ 
    if (name == null)name = "Error! Must enter at least one character"; 
    return name; 
}//end validation method 

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 String validateTests() 
{ 
    String testschange; 
    if (test1 < 0 || test1 > 100) { 
     testschange = " You have entered an invalid number, between 1-100. \nPlease restart!"; 
     testschange = Integer.toString(test1) ; 
    } 
    else if (test2 < 0 || test2 > 100) { 
     testschange = " You have entered an invalid number, between 1-100. " + 
     "\nPlease restart!"; 
     testschange = Integer.toString(test2); 
    } 
    else if (test3 < 0 || test3 > 100) { 
     testschange = " You have entered an invalid number, between 1-100. " + 
     "\nPlease restart!"; 
     testschange = Integer.toString(test3); 
    } 
    else String.toInteger(testchange) = test1 || test2 || test3; 
    return testchange; 
}//validating the test scores and tesing each one against method 

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 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

字符串相等的第一條規則:你不要用'=='比較字符串。其次,你的問題陳述非常廣泛。通過縮小具體出錯地點和地點,可以爲您提供最好的服務。 – Makoto 2014-10-11 23:04:43

+0

我會加入Makoto評論,你必須再次研究如何正確編程。仔細檢查你的代碼,並嘗試認爲如果邏輯正確。例如:public String getName(String nm),是錯誤的getName只應該返回「name」,而不是將其設置爲nm。使用void setter函數來解決這個問題。 – 2014-10-11 23:11:13

+0

請向我解釋爲什麼你需要有2種方法從驅動程序獲取某些東西並將變量設置爲某種東西? – jdog1218 2014-10-12 23:13:39

回答

0

這是錯誤的行:

name = student.setName(input); 

setNamestudent對象的方法返回void;也就是說,它不會返回一個值。你不能將void方法的結果賦值給任何東西。

刪除作業並清除其中一個錯誤。