嗨,
我需要在驗證數據領域的幫助。出於某種原因,我不斷收到不兼容的錯誤。我現在檢查了幾次,我有正確的類型。有什麼不對,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
}
你應該在這裏閱讀'if-then-else':http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html – 2014-09-30 03:17:01