似乎无法正确打印我的代码

时间:2020-03-12 23:12:20

标签: java

我正在尝试以以下格式打印代码,但似乎无法弄清楚。每当我尝试采用这种格式时,都会遇到很多错误。

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 USD

1小-$ 0.60

您的总费用为:$ 262.60

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

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
    }

您必须合并所需的操作,我在这里做了折扣,请尝试“框”功能,如果您有任何疑问,请问我。