您好下,
我需要在验证数据方面提供帮助。由于某种原因,我不断收到不兼容的错误。我检查了几次,因为我的类型正确。有什么不对,2班。错误是int在驱动程序类中继续在“name = student.setName(input);”中引入不兼容的tyoe。请解释原因?
/*
* 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 :(得分:1)
你正在努力控制流程。
当您调用teacher
构造函数时,无论如何,总是会执行以下代码行:
System.out.println ( "I don't have a teacher in that room." );
System.out.println("Always show");
有几种方法可以解决这个问题:
首先,您可以在return;
声明中if
。
或者您可以使用if
然后else if
然后使用else if
然后使用else
作为每个条件的最后一个。
或者您可以使用switch
声明。
答案 1 :(得分: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." );
}
}
更好的方法是创建另一个包含Teacher对象的集合/数组的类。
示例:
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>);
如果您想接受多个房间号码,那么您可以将最后两行代码放在循环中。