【发布时间】:2017-04-30 12:33:21
【问题描述】:
最后有人告诉我,我的格式有问题。但是,我不知道那会是什么......错误消息在代码的末尾。 请不要对您的答案模棱两可,并告诉我要重点关注或审查的内容(如格式化)。我需要具体的改变。谢谢!! 代码如下:
public static void main(String[] args)
{
double pennies = 0.01; //number of pennies
double totalPennies = 0.01; //the variable that is added to
int maxDays; //number of days the loop will double the pennies to
int day = 0; //the variable used to count each day in the loop
//Create a String variable for user input with a console window.
String input;
//Create a scanner object for keyboard input into the console.
Scanner keyboard = new Scanner(System.in);
//Ask user for the number of days to count.
input = JOptionPane.showInputDialog("Enter the maximum number of days worked in your pay period"
+ " and I will tell you how much money you made. Please enter at least 1.");
maxDays = Integer.parseInt(input); //this makes the user's string input into
//an integer data type
//Input Validation
while(maxDays < 1)
{
System.out.println("You have entered a number that "
+ "is less than 1. \nPlease enter a number that is at"
+ " least 1 or more:");
maxDays = keyboard.nextInt();
}
//next we have the first loop that will only execute if the user enters at least 1
while(maxDays >= 1)
{
//here's the chart
System.out.println("Day Day's Income Total "); //titles for the chart
System.out.println("----------------------------------"); //34 dashes/spaces
System.out.println("1 $0.01 $0.01 "); //the first day
//this next nested loop will do the actual counting and add to the chart with
//each iteration
for(day = 2; day <= maxDays; day++)
{
pennies = pennies * 2; //double pennies
totalPennies += pennies; //add the new value of pennies to the running total
//add information from each iteration to the chart
System.out.printf("%10.0d" + "$%18.0f" + "$%6.0f", day, pennies, totalPennies );
}
}
}
以下是错误消息:
Exception in thread "main" java.util.IllegalFormatPrecisionException: 0
at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2984)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2729)
at java.util.Formatter.parse(Formatter.java:2560)
at java.util.Formatter.format(Formatter.java:2501)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at assignment4PenniesForPay.assignment4PenniesForPay.main(assignment4PenniesForPay.java:65)
第 65 行是格式化行。
【问题讨论】:
-
关于,
"PLEASE DON'T BE AMBIGUOUS WITH YOUR ANSWERS AND TELL ME WHAT TO FOCUS ON OR REVIEW (LIKE FORMATTING).I NEED CONCRETE THINGS TO CHANGE."-- 请不要大喊大叫或苛求。如果你保持文明,你会得到更好的答案。 -
$%18.0f"?为什么前面有 18 个数字? -
现在在这里等一下。为什么在使用“for”循环时有“while”循环?将:while (maxdays >= 1) { 改为: if (maxDays >= 1) { 否则您的循环将无限期运行。
-
和@DevilsHnd 说的一样,但也要检查上面的while (
while(maxDays < 1)),因为如果满足这个条件,它就永远不会逃脱!你需要的是if / else结构。
标签: java formatting counting