这就是问题:
根据销售的数量,Ernesto's Eggs的鸡蛋批发价格不同:
0但不包括4打:每打0.50美元
4打,但不包括6打:每打0.45美元
6打到但不包括11打:每打0.40美元
11打或打打:每打0.35美元
额外鸡蛋的价格为每打价格的1/12。
a)编写一个程序,询问用户鸡蛋的数量,然后计算账单。程序输出应类似于:
输入鸡蛋数:126
您的费用是每打0.40美元或每蛋0.033美元。
你的账单是4.20美元
这是我写的,但我没有正确的数字。
/*
Program 1 (Ernestos Eggs)
*/
package eggs;
import java.util.Scanner;
/**
*
* @author Laptop
*/
public class Eggs {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter number of eggs: ");
int numberofeggs=sc.nextInt();
int numberofdozens=numberofeggs/12;
if(numberofeggs>=0 && numberofeggs<48){
double costpereggs= 0.50*(1/12);
double total= (0.50*numberofdozens)+costpereggs;
System.out.println("Your cost is $0.50 per dozen or " +costpereggs+ " per egg.");
System.out.print("Your bill comes to $" +total);
}
else if (numberofeggs>=48 && numberofeggs<72){
double costpereggs= 0.45*(1/12);
double total= (0.45*numberofdozens)+costpereggs;
System.out.println("Your cost is $0.45 per dozen or " +costpereggs+ "per egg.");
System.out.println("Your bill comes to $" +total);
}
else if (numberofeggs>=72 && numberofeggs<132) {
double costpereggs= 0.40*(1/12);
double total= ((0.40*numberofdozens)+costpereggs);
System.out.println("Your cost is $0.40 per dozen or " +costpereggs+ " per egg.");
System.out.println("Your bill comes to $" +total);
}
else {
double costpereggs= 0.35*(1/12);
double total= (0.35*numberofdozens)+costpereggs;
System.out.println("Your cost is $0.35 per dozen or " +costpereggs+ "per egg.");
System.out.println("Your bill comes to $" +total);
}
}
}
答案 0 :(得分:0)
为什么要增加每个蛋的成本?这与总数无关。
double total = numberofdozens *0.35;
double costpereggs = total/numberofeggs;
这应该是你想要做的事情。