Java:无原因的错误

Java:无原因的错误,java,Java,嗨, 我需要验证数据方面的帮助。由于某种原因,我一直收到一个不兼容的错误。我检查了几次,现在我有正确的类型。怎么了,2节课。错误为int。驱动程序类在name=student.setName input;中不断引入不兼容的tyoes;。请解释一下原因 /* * Joel Gordon * per.4 */ import java.util.Scanner; public class P5A { public static void main (String args[])

嗨, 我需要验证数据方面的帮助。由于某种原因,我一直收到一个不兼容的错误。我检查了几次,现在我有正确的类型。怎么了,2节课。错误为int。驱动程序类在name=student.setName input;中不断引入不兼容的tyoes;。请解释一下原因

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


}

您正在与控制流进行斗争

发生的情况是,当您调用教师构造函数时,无论发生什么,始终会执行以下代码行:

    System.out.println ( "I don't have a teacher in that room." );
    System.out.println("Always show");
有几种方法可以解决此问题:

首先,你可以回来;在你的if语句中

或者您可以使用if-then-else-if-then-else作为每个条件的最后一个


或者您可以使用switch语句。

您应该将教师设置为代表教师的类。然后,在教师类之外创建教师对象的实例

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>);
如果您想要接受多个房间号码,那么您可以将上面的最后两行代码放入一个循环中

您应该在此处阅读关于if-then-else的内容: