为什么我的小数点后只有一位而不是两位?
Why do I only get one place behind my decimal point instead of two?
当我尝试这个问题时,除了小数点位置外,我的所有内容都显示正确。我显示它应该是 xxx.xx 而我的答案只允许 xxx.x。每次我尝试修复它时,我都会搞砸其他一切。你能帮我看看我错过了什么或做错了什么吗?这是我拥有的:
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));
当我尝试这个问题时,除了小数点位置外,我的所有内容都显示正确。我显示它应该是 xxx.xx 而我的答案只允许 xxx.x。每次我尝试修复它时,我都会搞砸其他一切。你能帮我看看我错过了什么或做错了什么吗?这是我拥有的:
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));