Мне сказали, что есть проблемы с моим форматированием, в конце. Однако я не могу понять, что это будет ... Сообщения об ошибках находятся в конце кода. ПОЖАЛУЙСТА, НЕ БЫТЬ ЧРЕЗВЫЧАЙНЫ С ВАШИМИ ОТВЕТАМИ И СКАЖИТЕ МЕНЯ, ЧТО ДЛЯ ФОКУСИРОВАНИЯ ИЛИ ОБЗОР (КАК ФОРМАТИРОВАТЬ). Мне нужно БЕТОННОЕ ВЕЩАНИЕ, ЧТОБЫ ИЗМЕНИТЬ. Спасибо!! Вот код:Что не так с форматированием в моем Java-коде?
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 является строка форматирования.
Относительно '' ПОЖАЛУЙСТА, НЕ БЫВАЙТЕСЬ С ВАШИМ ОТВЕТОМ И СКАЖИТЕ МЕНЯ, ЧТО ДЛЯ ФОКУСИРОВАНИЯ ИЛИ РАССМОТРЕНИЯ (КАК ФОРМАТИРОВАТЬ). Мне НЕОБХОДИМО БЕТОННОЕ ВЕЩАНИЕ ИЗМЕНИТЬ. "' - Пожалуйста, не ВЫЗЫВАЙТЕ или не требуйте. Вы получите гораздо лучшие ответы, если останетесь гражданскими. –
'$% 18.0f" '? Почему 18 передняя цифра? –
Теперь подождите минуту. Почему цикл while, когда вы используете цикл 'for'? Измените: while (maxdays> = 1) { вместо этого: if (maxDays> = 1) {в противном случае ваш цикл будет работать неопределенно. – DevilsHnd