代码正在运行,但总价格计算有误。

huangapple go评论75阅读模式
英文:

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;

huangapple
  • 本文由 发表于 2020年10月6日 16:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64222027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定