简体   繁体   中英

Can't seem to get my code to printout correctly

I am trying to print the the code out in the format below but can't seem to figure out how. When ever I try to get into that format I get alot of errors.

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
    }
}

Number of Bags Ordered: 52 - $ 286.00

Discount: 10% - $ 28.60

Boxes Used:

2 Large - $ 3.60

1 Medium - $ 1.00

1 Small - $ 0.60

Your total charge is: $ 262.60

You can try something like this:

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
    }

You have to concatenate the operations you want, I did the discount here, try the 'boxes' function, if you have questions, ask me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM