java成绩报告家庭作业

时间:2010-10-25 14:46:34

标签: java

我应该:

  1. 创建两个构造函数。一个。查询学生的姓名和三个分数。湾拿四个论点
  2. 编写计算和设置平均值
  3. 的方法calculateAvg
  4. 写一个方法calculateGrade,根据90 + = A计算,80 + = B,70 + = C,60 + = D,< 60 = F
  5. 编写一个方法toString,显示带有a的gradeReport。名字b。一条线上的三个分数c。平均和字母等级
  6. 使用第一个构造函数时,确保分数在0-100之内。如果不再提示并解释原因。
  7. 将输出格式化为正好两位小数
  8. 格式化输出,以便用标签分隔分数。
  9. 我并不是要求所有这一切都完成,但是如果你看看我的代码,你可以给我任何关于我出错的地方以及我可能需要添加的内容吗?

    import java.text.DecimalFormat;
    import java.util.Scanner;
    public class GradeReport 
    {
        String name, name1, name2;
        int score1, score2, score3;
        double average;
        char grade;
        public GradeReport()  //creates the first constructor
        {
            Scanner sc = new Scanner (System.in);
    
            System.out.println ("Enter student's first name: ");
            name1 = sc.nextLine();
    
            System.out.println ("Enter the student's last name: ");
            name2 = sc.nextLine();
    
            System.out.println ("Enter first grade: ");
            score1 = sc.nextInt();
    
            System.out.println ("Enter second grade: ");
            score2 = sc.nextInt();
    
            System.out.println ("Enter third grade: ");
            score3 = sc.nextInt();
        }
        public GradeReport (String name, int score1, int score2, int score3)
        {
        }
        public void calculateAverage()
        {
            average = ((score1 + score2 + score3) / 3);
    
            DecimalFormat fmt = new DecimalFormat ("0.###"); //to format average to 2 decimal places
        }
        public void calculateGrade()
        {
            if (average >= 90)
                System.out.println("A");
            else if (average >= 80)
                System.out.println("B");
            else if (average >= 70)
                System.out.println("C");
            else if (average >= 60)
                System.out.println("D");
            else
                System.out.println("F");
        }
        public String toString()
        {
            //System.out.println (name1, name2);
            String gradeReport = Double.toString(score1) + "\t," + Double.toString(score2)+ "\t," + Double.toString(score3);
            //String gradeReport = Double.toString(average);
            return gradeReport;
        }
    
    
    }
    

3 个答案:

答案 0 :(得分:1)

您已注释掉elif个陈述。我想如果你取消注释它们会得到一些编译器错误。在Java中,elif应写为else if

最后,你的行

  String gradeReport = Double.toString(score1)\t, Double.toString(score2)\t,      Double.toString(score3); 

......你到底想要的是什么?我认为你已经意味着:

String gradeReport = Double.toString(score1)+"\t, "+Double.toString(score2)+"\t,      "+Double.toString(score3); 

但不清楚......如果 是你的意思,这里的学习要点是字符串文字应该用双引号括起来,而+运算符会被重载用于字符串连接(将一个字符串附加到另一个字符串)。

答案 1 :(得分:0)

else if条件下也存在逻辑错误。应该是:如果平均值> = 90,否则如果平均值> = 80,否则如果平均值> = 70,否则如果平均值> = 60,则其他。

public GradeReport()替换为public static void main(String[] args)。尝试将所有代码放在一个方法中 - 主要。该方法将按照您的预期进行分解(重构)它。

答案 2 :(得分:0)

从您的作业中,您可以解决以下有关您的代码的问题(对任何语法错误都抱歉,请不要因为我在3年后编写符合条件的Java而无法编写.net)

Class GradeReport{
//The values we get from the user
private string name
private int score1
private int score2
private int score3

//Things we are told we need to calculate at some point
private double average
private char grade

public GradeReport(){
// Get values from the user and validate them

// We will need to assign the values to the fields in some way 
// here...do we have somewhere else to do that already written?
}

public GradeReport(String name, int score1, int score2, int score3){
// We will probably want to assign these parameters to the fields here
}

public void calculateAverage(){
// You already have that pretty much, but you prob don't need to output it to the screen
}

public void calculateGrade(){
//Some sort of if...else logic is needed to work out the grade from the avg
}

public String toString(){
// We need to output our various bit of information here in a nicely formatted String
// I would recommend looking at String.Format() method in the Java API as a good starting point.

// At some point you will need to call calculateGrade and calculateAverage. It would 
// be useful to do that before you output to screen. It can either be done in here or 
// in your main class before you call toString()
}
}

我认为你非常接近你需要的答案,你只需要将所有部分组合在一起。请以各种方式查看格式化字符串。