尝试修复我的printf语句时出现问题

时间:2016-02-12 02:49:48

标签: java printf setter getter

我需要在我的学生班中修复addQuiz()。然后,通过该课程,我将所有信息拉入我的prog2主程序。除了两件事,我的一切都在运作。我需要为我的addQuiz()修改公式,以便总计输入的点数,并在我的主要文件中修复while语句,以便我可以输入一个单词告诉程序我已完成输入我的测验。

这是我的主要文件。

import java.util.Scanner;

public class Prog2 {

 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  Student student = new Student();
  //creates array for quizzes
  double[] grades = new double[99];
  //counter for total number of quizzes
  int num = 0;

  //requests user to enter students name
  System.out.print("Enter name of student: ");
  String name = in .nextLine();

  //requests user to enter students quizzes
  System.out.print("Enter students quiz grades: ");
  int quiz = in .nextInt();

  while (quiz >= 1) {

   System.out.print("Enter students quiz grades: ");
   quiz = in .nextInt();
   grades[num] = quiz;
   num++;
  }
  //prints the name, total, and average of students grades
  System.out.println();
  System.out.println(name);
  System.out.printf("\nTotal: ", student.addQuiz(grades, num));
  System.out.printf("\nAverage: %1.2f", student.Average(grades, num));
 }
}

这是我的学生档案:

    public class Student {
private String name;
private int total;
private int quiz;
static int num;

public Student() {
    super();    
}

public String getName(){
    return name;
}
public void setName(String name){
    this.name = name;
}
public int getTotal() {
    return total;
}
public void setTotal(int total) {
    this.total = total;
}
public int getQuiz() {
    return quiz;
}

public void setQuiz(int quiz) {
    this.quiz = quiz;
}

public static double addQuiz( double[] grades, int num){
    int totalQuiz = 0;
    for( int x = 0; x < num; x++){
        totalQuiz += grades[x];
    }
            return totalQuiz;
    }
public static double Average( double[] grades, int num){
    double sum = 0;
    for( int x = 0; x < num; x++){
        sum += grades [x];
    }
    return (double) sum / num;
}
}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

您的要求不明确,但我想它应该是这样的。

Prog2课程:

    public static void main(String[] args) {

    // requests user to enter students name
    System.out.print("Enter name of student: ");
    Scanner in = new Scanner(System.in);
    String name = in.nextLine();

    Student student = new Student(name);

    System.out.print("Enter number of quiz: ");
    int count = in.nextInt();

    for (int i = 0; i < count; i++) {
        // requests user to enter students quizzes
        System.out.print("Enter students quiz grades: ");
        int quiz = in.nextInt();
        student.addGrade(quiz);
    }

    // prints the name, total, and average of students grades
    System.out.println(name);
    System.out.println("Total: " + student.getTotal());
    System.out.println("Average: " + student.getAverage());
}

学生班:

    private String name;
private List<Double> grades = new ArrayList<Double>();

public Student(String name) {
    super();
    this.name= name;
}

public String getName() {
    return name;
}

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

public void addGrade(double grade) {
    this.grades.add(grade);
}

public double getTotal() {
    double total = 0;
    for (double grade : grades) {
        total += grade;
    }
    return total;
}

public double getAverage() {
    return (double) getTotal() / grades.size();
}

如果有帮助,请接受答案。

答案 1 :(得分:0)

对于停止条件,您可以尝试这样的事情

String stopFrase="stop";
String userInput="";
int quiz;
//Other code
for(;;) //This basically means loop until I stop you
{
  System.out.print("Enter students quiz grades type 'stop' to finish:");
  userInput=in.nextLine();
  if(userInput.equals(stopFrace))
  {
    break;//Stop the loop
  }
  quiz= Integer.parseInt(userInput);
  //The rest of your code
}

您的addQuiz()方法似乎没问题,如果您没有得到所需的结果,请检查您的参数,特别是确保该数字与输入的测验数相符。