我正在尝试以下面的格式打印代码,但似乎不知道如何打印。每当我尝试采用这种格式时,都会遇到很多错误。
import java.util.*;
class Discount
{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of bags: ");
int n=sc.nextInt(); //Input
System.out.println("Your total charge is "+(discount(n)+boxes(n)));
}
public static double discount(int n) {
double total=n*5.50;
double amount=0;
if (n<25) { //if then statments for discounts
amount=total;
} else if(n<50) { //25-49 bags
amount=total-total*(.05);
} else if(n<100) { //50-99 bags
amount=total-total*(.10);
} else if(n<150) { //100-149
amount=total-total*(.15);
} else if(n<200) { //150-199
amount=total-total*(.20);
} else if(n<300) { //200-299
amount=total-total*(.25);
} else {
amount=total-total*(.30);
}
return amount; //returning value after discount
}
public static double boxes(int n) {
double amount=0;
while(n>0) { //if clause to decide type of packets
if(n>=20) {
n-=20;
amount+=1.80;
} else if(n>=10) {
n-=10;
amount+=1.00;
} else if(n>=5) {
n-=5;
amount+=0.60;
} else {
amount+=0.60;
n=0;
}
}
return amount; //returning total shipping cost
}
}
订购的包袋数量:52 - 286.00 美元
折扣:10% - 28.60 美元
使用的盒子:
2 个大号 - 3.60 美元
1 个中等 - 1.00 美元
1 小号 - 0.60 美元
您的总费用为:262.60 美元
最佳答案
你可以尝试这样的事情:
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of bags: ");
int n=sc.nextInt(); //Input
System.out.println((discount(n)));
}
public static String discount(int n){
String discount = "Number of Bags Ordered: " + n + " - $";
double total=n*5.50;
discount += total + " \n";
double amount=0;
if(n<25) {//if then statments for discounts
amount=total;
}
else if(n<50)//25-49 bags
amount=total-total*(.05);
else if(n<100)//50-99 bags
amount=total-total*(.10);
else if(n<150)//100-149
amount=total-total*(.15);
else if(n<200)//150-199
amount=total-total*(.20);
else if(n<300)//200-299
amount=total-total*(.25);
else
amount=total-total*(.30);
float d = (float) (total - amount);
discount += "Discount: " + "$ " + d;
return discount; //returning value after discount
}
public static double boxes(int n){
double amount=0;
while(n>0) {//if clause to decide type of packets
if(n>=20){
n-=20;
amount+=1.80;
}
else if(n>=10){
n-=10;
amount+=1.00;
}
else if(n>=5){
n-=5;
amount+=0.60;
}
else{
amount+=0.60;
n=0;
}
}
return amount; //returning total shipping cost
}
你要把你想要的操作串联起来,我这里打折了,试试‘盒子’功能,有问题可以问我
关于java - 似乎无法正确打印我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60662948/