简体   繁体   中英

Why do I only get one place behind my decimal point instead of two?

I have everything showing right when I try this problem except the decimal point place. I shows it should be xxx.xx and my answer only allows for xxx.x. Every time I try to fix it, I mess up everything else. Can you please help show me what I missed or did incorrectly? Here is what I have:

import java.util.Scanner;
import java.text.DecimalFormat;

public class TestAvgAndGrade{
    
    static double average;
    static char grade;
    public static void main (String[] args){
        
        Scanner keyboard = new Scanner(System.in);
        
        char letter1;
        char letter2;
        char letter3;
        char letter4;
        char letter5;
        double score1;
        double score2;
        double score3;
        double score4;
        double score5;
        double score;
        
        System.out.print("Enter test grade for student 1: ");
        score1 = keyboard.nextDouble();
        
        System.out.print("Enter test grade for student 2: ");
        score2 = keyboard.nextDouble();
        
        System.out.print("Enter test grade for student 3: ");
        score3 = keyboard.nextDouble();
        
        System.out.print("Enter test grade for student 4: ");
        score4 = keyboard.nextDouble();
        
        System.out.print("Enter test grade for student 5: ");
        score5 = keyboard.nextDouble();
        
        System.out.println("The letter grades are as follows:");
        
        double average = calcAverage(score1, score2, score3, score4, score5);
        
        System.out.println("Student 1: " + determineGrade(score1)
                           + "\nStudent 2: " + determineGrade(score2)
                           + "\nStudent 3: " + determineGrade(score3)
                           + "\nStudent 4: " + determineGrade(score4)
                           + "\nStudent 5: " + determineGrade(score5)
                           + "\nThe average grade was: " + ((double)average));
    }
    public static double calcAverage(double score1, double score2, double score3, double score4, double score5) {
        
        double average = (score1+score2+score3+score4+score5) / 5.0;
        return average;
    }
    public static char determineGrade(double score){
        if(score<=100 && score>=90)
        {
            return 'A';
        }
        else if(score>=80)
        {
            return 'B';
        }
        else if(score>=70)
        {
            return 'C';
        }
        else if(score>=60)
        {
            return 'D';
        }
        else
        {
            return 'F';
        }
    }
}
NumberFormat nf = NumberFormat.getNumberInstance(Locale.UK);
nf.setMinimumFractionDigits(2);nf.setMaximumFractionDigits(2);
System.out.println("The average grade was: " + nf.format(average));
//or Point or Comma alike
System.out.println("The average grade was: " + new DecimalFormat("##0.00").format(average));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM