无法正确运行Java阈值

时间:2015-12-03 14:12:21

标签: java oop

Main.java

import java.util.*;

public class Main{
    static Student[] students = new Student[10];//creates an array of 10 students to be entered

    public static int inputThreshold(){
        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter the Threshold: \n");
        int threshold = scan.nextInt();

        return  Threshold();
    }

    public static Student inputStudent(){
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter students surname:  \n");//instructs the user to enter a surname
        String name = scan.nextLine();//allows the user to input sdtudents surname
        System.out.println("Enter student score between 0 and 30:  \n");
        int score = scan.nextInt();//allows the user to input students score
        return new Student(name, score);
    }

    public static void printStudent(Student student){
        int percentage = student.getScore()*10/3;//retrieves the percentage of the score submitted out of 30

        System.out.println("Surname:  " + student.getName() + "   Score:  " + student.getScore() + "   Percentage:  " + percentage + "%");
        //prints out the surname, score and percentage achieved by the student
    }      

    public static void printThreshold(int threshold){
        int percentage = student.getScore()*10/3;//retrieves the percentage of the score submitted out of 30


        if (percentage < threshold){
            System.out.println("Surname:  " + student.getName() + "   Score:  " + student.getScore() + "   Percentage:  " + percentage + "%");
            //prints out the surname, score and percentage achieved by the student
                }
    }     

    public static Student getWinner(Student[] student){
        Student x = student[0];
        for(int i = 1; i < 10; i++){
            if(student[i].getScore() > x.getScore()){
                x = student[i];
            }
        }
        return x;
    }

    public static void main(String[] args){
        for (int i = 0; i = 1; i++){
            threshold = inputThreshold;
        }

        for (int i = 0; i < 10; i++){
            students[i] = inputStudent();
        }

        for(int i = 0; i < 10; i++){
            printStudent(students[i]);
        }


        for(int i= 0; i < 1; i++){
            printThreshold(students[i]);
        }

        Student winrar = getWinner(students);//retrieves the winner from getWinner into a variable
        System.out.println("AND WE HAVE OUR WINNER! \n" + "Name:  " + winrar.getName() + "   Score:  " + winrar.getScore());
        //prints out the highest scoring students surname, with their score
    }
}

Student.java

public class Student{
   private String name;//sets name to characters
   private int score;//sets score to numbers
   private int threshold;//sets the threshold of the score
   private int max = 30;//sets max score to 30


   public Student(String name, int score){
       this.name = name;
       if (score <= max && score >= 0) //if the score is equal to 30 or less than 0
           this.score = score;//the score can be stored
       else{
           System.out.println("This number is too big ");//if it is larger than 30 it cannot be stored and shows errors message
           System.exit(1);//this will end the program
       }
   }

   public String getName(){
       return name;
   }

   public int getScore(){
       return score;
   }

   public int getThreshold(){
       return threshold;
   }

   public void setScore(int s){
       this.score = s;
   }

   public void setName(String n){
       this.name = n;
   }

   public void setThreshold(int t){
       this.threshold = t;
   }
}

返回无法查找符号 - 方法阈值()

我不确定要引用什么或如何调用方法以使其正确运行。简介:10位用户,10位分数。每个参赛者都要达到一个门槛。该计划输出他们的名字,达到的分数和他们的分数百分比。但它也应该宣布总冠军。

不确定

3 个答案:

答案 0 :(得分:0)

return  Threshold(); 

应该是

return threshold;

答案 1 :(得分:0)

Threshold()更改为threshold

我强烈建议您在编写另一个程序之前阅读本文

Objects, Instance Methods, and Instance Variables

答案 2 :(得分:0)

 public static int inputThreshold(){
        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter the Threshold: \n");
        int threshold = scan.nextInt();

        return  Threshold();
    }   

 public static void main(String[] args){
             for (int i = 0; i = 1; i++){
                 threshold = inputThreshold;
             }
         ...rest of code in main
   }

thresholdinputStudent()中定义的局部变量,您无法在main()中访问它。另外return Threshold();,没有Threshold()您提供的代码中定义的方法