Java成绩计算器去除多余小数0问题技术求助
Got it, let's fix that annoying trailing zero issue in your grade output! The problem with your current code is that you're using fixed decimal places with printf, which forces those .00 or .0 even when the grade is an integer. Here are two solid approaches to make the output clean and match your expected format:
Approach 1: Use DecimalFormat (Recommended)
DecimalFormat lets you define flexible formatting patterns that automatically omit trailing zeros. This is the most concise and maintainable way to handle dynamic decimal lengths.
First, import the necessary class:
import java.text.DecimalFormat;
Then update your code to use formatters tailored to each grade type:
public class GradeCalculator { public static void main(String[] args) { // Replace these with your actual input values int test = 10, midterm = 15, exam = 25; int assignment = 10, lab = 5; double theoryGrade = (test + midterm + exam) * 100.0 / 70; double practicalGrade = (assignment + lab) * 100.0 / 30; double finalGrade = (lab + test + assignment + midterm + exam); // Define formatters: # omits trailing zeros DecimalFormat theoryFormatter = new DecimalFormat("#.##"); // Up to 2 decimals DecimalFormat practicalFormatter = new DecimalFormat("#"); // Integer only (or #.## if decimals are possible) DecimalFormat finalFormatter = new DecimalFormat("#.#"); // Up to 1 decimal // Print with clean formatting System.out.println("Theory grade: " + theoryFormatter.format(theoryGrade) + "%"); System.out.println("Practical grade: " + practicalFormatter.format(practicalGrade) + "%"); System.out.println("Final grade: " + finalFormatter.format(finalGrade) + "%"); } }
How it works:
- The
#symbol in the pattern tellsDecimalFormatto only show digits where necessary. So:- An integer grade like
50will print as50% - A two-decimal grade like
74.29prints as74.29% - A one-decimal grade like
77.5prints as77.5%
- An integer grade like
Approach 2: Conditional printf Formatting
If you prefer to stick with printf, you can manually check if each grade is an integer and select the appropriate format specifier:
public class GradeCalculator { public static void main(String[] args) { // Replace these with your actual input values int test = 10, midterm = 15, exam = 25; int assignment = 10, lab = 5; double theoryGrade = (test + midterm + exam) * 100.0 / 70; double practicalGrade = (assignment + lab) * 100.0 / 30; double finalGrade = (lab + test + assignment + midterm + exam); // Theory Grade: Show 0 decimals if integer, else 2 decimals if (theoryGrade == Math.floor(theoryGrade)) { System.out.printf("Theory grade: %.0f%%%n", theoryGrade); } else { System.out.printf("Theory grade: %.2f%%%n", theoryGrade); } // Practical Grade: Show 0 decimals if integer, adjust decimals as needed if (practicalGrade == Math.floor(practicalGrade)) { System.out.printf("Practical grade: %.0f%%%n", practicalGrade); } else { System.out.printf("Practical grade: %.2f%%%n", practicalGrade); // Change to %.1f if needed } // Final Grade: Show 0 decimals if integer, else 1 decimal if (finalGrade == Math.floor(finalGrade)) { System.out.printf("Final grade: %.0f%%%n", finalGrade); } else { System.out.printf("Final grade: %.1f%%%n", finalGrade); } } }
Note:
When comparing doubles for equality (like theoryGrade == Math.floor(theoryGrade)), be cautious with floating-point precision issues. For grade calculations (which are based on integers), this should be safe, but if you're dealing with arbitrary doubles, consider using a small epsilon value (e.g., Math.abs(theoryGrade - Math.floor(theoryGrade)) < 0.0001) to avoid false negatives due to precision errors.
内容的提问来源于stack exchange,提问作者Akram Nadri

