Java中实现输出时去除不必要小数的课程作业问题
Hey there! Totally get you're a Java beginner working on a course assignment—no worries at all. Your code already does what it needs to, so great job getting that far! Now, let's fix that output formatting issue so numbers like 85.00 show up as just 85, while numbers with actual decimal parts (like 85.50) keep their relevant digits.
The Problem with Your Current Code
Right now you're using %.2f in your System.out.format calls, which forces exactly two decimal places no matter what. That's why even whole numbers get .00 tacked on. We need a way to automatically hide those trailing zeros and the decimal point when they aren't needed.
Solution 1: Use %g Format Specifier (Quick Fix)
Java's %g format specifier automatically removes trailing zeros and unnecessary decimal points. It works well for this case without needing extra imports. Just replace your %.2f with %g:
System.out.format("theoryGrade: %g%%%n", theoryGrade); System.out.format("practicalGrade: %g%%%n", practicalGrade); System.out.format("finalGrade: %g%%%n", finalGrade);
Note: %g uses significant figures rather than fixed decimal places, so it might round numbers with more than two decimal digits to keep things concise. For example, 85.123 would become 85.12 (or 85.1 depending on the value).
Solution 2: Use DecimalFormat (More Control)
If you want precise control over how many decimal places to show (e.g., max two, but none if the number is whole), DecimalFormat is the way to go. Here's how to implement it:
First, add the import for
DecimalFormatat the top of your file:import java.text.DecimalFormat;Create a
DecimalFormatobject with the pattern#.##—this means "show up to two decimal places, but omit trailing zeros and the decimal point if not needed":DecimalFormat df = new DecimalFormat("#.##");Replace your
System.out.formatcalls withSystem.out.printlnusing the formatter:System.out.println("theoryGrade: " + df.format(theoryGrade) + "%"); System.out.println("practicalGrade: " + df.format(practicalGrade) + "%"); System.out.println("finalGrade: " + df.format(finalGrade) + "%");
Full Modified Code
Here's your complete code with the DecimalFormat solution applied:
import java.util.Scanner; import java.text.DecimalFormat; public class Assignment1 { public static void main (String cyclops[]) { Scanner input = new Scanner (System.in); DecimalFormat df = new DecimalFormat("#.##"); System.out.print("Welcome to the CST8215 Final Mark Calculator \n"); System.out.print("Entered Lab mark out of 10: "); float lab = input.nextFloat(); System.out.print("Entered Quiz/Test mark out of 10: "); float quiz_test = input.nextFloat(); System.out.print("Entered assignment mark out of 20: "); float assignment = input.nextFloat(); System.out.print("Entered midterm mark out of 20: "); float midterm = input.nextFloat(); System.out.print("Entered final exam mark out of 40: "); float final_exam = input.nextFloat(); float theoryGrade = (quiz_test + midterm + final_exam) * 100 / 70; float practicalGrade = (assignment + lab) * 100 / 30; float finalGrade = (lab + quiz_test + assignment + midterm + final_exam); System.out.println("theoryGrade: " + df.format(theoryGrade) + "%"); System.out.println("practicalGrade: " + df.format(practicalGrade) + "%"); System.out.println("finalGrade: " + df.format(finalGrade) + "%"); } // end of main } // end of class
How It Works
- For whole numbers like
85.00, the formatter will output just85 - For numbers with one decimal place like
85.50, it will output85.5 - For numbers with two decimal places like
85.12, it will output85.12
This should perfectly meet your assignment's requirement of not showing unnecessary decimals!
内容的提问来源于stack exchange,提问作者ross

