我在验证数据方面需要帮助。出于某种原因,我不断收到不兼容的错误。我检查了几次,因为我的类型正确。有什么不对,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 :(得分:0)
这是错误的一行:
name = student.setName( input);
setName
对象上的student
方法返回void
;也就是说,它不会返回值。您无法将void方法的结果分配给任何内容。
删除作业,这将清除您的一个错误。