英文:
Code is running but total price calculation is wrong
问题
import java.util.Scanner;
public class Hans
{
public static void main (String [] args)
{
final int EGG_DOZEN = 12;
final double DOZEN_PRICE = 3.25;
final double EGG_PRICE = 0.45;
int eggs;
Scanner keyboard = new Scanner(System.in);
System.out.print ("How many eggs has been ordered: ");
eggs = keyboard.nextInt();
int answer = eggs/EGG_DOZEN;
int remainder = eggs%EGG_DOZEN;
double SingleEgg = EGG_PRICE * remainder;
double DozenEgg = DOZEN_PRICE * EGG_DOZEN;
double TotalPrice = SingleEgg + DozenEgg;
System.out.print ("You odered "+ eggs + " eggs. That's " + answer + " dozen at " + DOZEN_PRICE + "$ per dozen and " + remainder + " loosen eggs at " + EGG_PRICE+" cents each for a total of " + TotalPrice + "$");
}
}
英文:
I'm having an error with the total price because the calculation is always wrong. I rechecked my code and the formulas were just right.
import java.util.Scanner;
public class Hans
{
public static void main (String [] args)
{
final int EGG_DOZEN = 12;
final double DOZEN_PRICE = 3.25;
final double EGG_PRICE = 0.45;
int eggs;
Scanner keyboard = new Scanner(System.in);
System.out.print ("How many eggs has been ordered: ");
eggs = keyboard.nextInt();
int answer = eggs/EGG_DOZEN;
int remainder = eggs%EGG_DOZEN;
double SingleEgg = EGG_PRICE * remainder;
double DozenEgg =DOZEN_PRICE * EGG_DOZEN;
double TotalPrice = SingleEgg + DozenEgg;
System.out.print ("You odered "+ eggs + " eggs.That's " + answer + " dozen at " + DOZEN_PRICE + "$ per dozen and " + remainder + " loosen eggs at " + EGG_PRICE+" cents each for a total of " + TotalPrice + "$");
}
}
答案1
得分: 1
你的第二个方程式是我猜测的问题。
double DozenEgg = DOZEN_PRICE * EGG_DOZEN;
这与用户输入无关。请改为:
double DozenEgg = DOZEN_PRICE * answer;
英文:
Your second equation is the problem I guess.
double DozenEgg =DOZEN_PRICE * EGG_DOZEN;
This is not related to the input of the user. Instead, try:
double DozenEgg= DOZEN_PRICE * answer;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论