无法获取要打印的java变量

时间:2015-06-21 04:20:40

标签: java printing

我无法让变量adjustedCharge打印出(文本被打印但不是值)我试图追踪但仍然无法正确打印。也没有错误。

import java.util.Scanner;
public class Assignment {

    public static void main(String[] args) {
        //Declare scanner
        Scanner input = new Scanner(System.in);
        //Prompt for information
        System.out.print("Customer's name:");
        String name = input.nextLine();

        System.out.print("Customer's address:");
        String address = input.nextLine();

        System.out.print("Customer's phone number:");
        String phone = input.nextLine();

        System.out.print("Customer's licence number:");
        String licence = input.nextLine();

        System.out.print("Customer's credit card number:");
        String ccard = input.nextLine();

        System.out.print("Credit card expiry date (MM/YYYY):");
        String expiry = input.nextLine();

        System.out.print("Hire length (in days):");
        int hire = Integer.parseInt(input.nextLine());

        System.out.print("Make/Model:");
        String model = input.nextLine();

        System.out.print("Registration of car:");
        String rego = input.nextLine();

        System.out.print("Hire rate (per day) Either S, M, L:");
        char inputRate = input.nextLine().charAt(0);

        System.out.print("Total days hired out:");
        int totalDays = Integer.parseInt(input.nextLine());
        //
        double dtotalDays = totalDays;

        double surcharge = dtotalDays - hire;

        //Daily hire rate / Stage 2
        double rate = 0;

        if (inputRate == 'S' || inputRate == 's'){
            rate = (char) 80.0;
            }           
        if (inputRate == 'M' || inputRate == 'm'){
            rate= 110.0;
        }
        if (inputRate == 'L' || inputRate == 'l'){
            rate= 140.0;
        }


        //Calculate late fees
        double penalty = rate * (surcharge * 2);


        //Start Stage 3
        double dCost=0;
        double tCost=0;

        StringBuilder dDetail = new StringBuilder("The damage Details are:" + "\n");
        StringBuilder tDetail = new StringBuilder("The traffic fines are:" + "\n");

        //Setup an exit statement
        boolean quit = false;

        while (!quit){

            System.out.println("<<<Enter one of the following commands:>>>");
            System.out.println("A - Damage Repair");
            System.out.println("B - Traffic Infringement");
            System.out.println("X - Exit Menu");

            String schoiceEntry = input.next();
            char choiceEntry = schoiceEntry.charAt(0);
            //Integer.parseInt(input.nextLine());
            double damageCost = 0;

            switch (choiceEntry){
                case 'A': 
                case 'a':
                        input.nextLine();
                        System.out.println("Enter a description of the damage:"); 
                        String damageDetail = input.nextLine();
                        System.out.println("Enter the damage cost:"); 
                        damageCost = input.nextInt();
                        System.out.print("The damage is: " + damageDetail + "\n");
                        System.out.print("The damage cost is: " + "$" + damageCost + "\n");
                        dDetail.append("-" + damageDetail + "\n");
                        dCost = dCost + damageCost;
                        System.out.println("----------------------------------");
                break;
                case 'B': 
                case 'b':   
                        input.nextLine();
                        System.out.print("Enter a description of the traffic infringement:"); 
                        String trafficDetail = input.nextLine();
                        System.out.println("Enter the traffic infringement cost:");
                        double trafficCost = Integer.parseInt(input.nextLine());
                        tDetail.append("-" + trafficDetail + "\n");
                        tCost = tCost + trafficCost;
                        System.out.println("----------------------------------");
                break;
                case 'X': 
                case 'x':   
                        quit = true; 
                        System.out.print("***Menu entry has been terminated***" + "\n");
                        //System.out.printf("Total traffic cost is: %75s\n", "$" + tCost + "\n");
                        //System.out.printf("Total Damage cost is: %77s\n", "$" + dCost + "\n");
                        //System.out.printf(tDetail + "\n");
                        //System.out.print(dDetail + "\n");
                break;

                default:
                        System.out.print("Please enter either a valid menu selection (A, B, or X):" + "\n");
                break;
            }

        }

        double dhire = hire;
        double charge = dhire*rate;
        double adjustedCharge = charge + penalty;


        //Print summary
        System.out.println("---------------------------------------------"+
                 "------------------------------------------------------\n" +
        "***CUSTOMER DETAILS:***\n");
        System.out.printf("Name: %93s\n", name);
        System.out.printf("Address: %90s\n", address);
        System.out.printf("Phone Number: %85s\n", phone);
        System.out.printf("Licence Number: %83s\n", licence);
        System.out.printf("Credit Card Number: %79s\n", ccard);
        System.out.printf("Credit Card Expiry: %79s\n", expiry);
        System.out.println( "\n***CAR HIRE DETAILS:***\n");
        System.out.printf("Make/Model: %87s\n", model);
        System.out.printf("Registration Number: %78s\n", rego);
        System.out.printf("Hire Length (days): %79s\n", model);
        System.out.printf("Daily Hire Rate: %82s\n", rate);


        System.out.printf("Basic Hire Charge: %80s\n\n", charge);
        System.out.printf("Days hired: %87s\n", totalDays);

        if (totalDays == hire){
        System.out.printf("Late Return Surcharge: %76s\n", "$0.00");
        }
        if (totalDays > hire){
        System.out.printf("Late Return Surcharge: %76s\n", + penalty);
        }




        System.out.printf("Adjusted Hire Charge: %77s\n", "\n", "$" + adjustedCharge + "\n");
        System.out.print(dDetail + "\n");
        System.out.printf("Total damage cost is: %78" + "s\n", "$" + dCost + "\n");
        System.out.printf(tDetail + "\n");
        System.out.printf("Total traffic fines incurred: %70s\n", "$" + tCost + "\n");
        System.out.printf("Final hire charge: %79s\n", "$" + (adjustedCharge + dCost + tCost));

    }

}

2 个答案:

答案 0 :(得分:2)

我刚刚更改了打印出调整后的充电方式,因此您仍然可以使用printf

    System.out.printf("Adjusted Hire Charge: %77s","$");
    System.out.printf("%.1f",adjustedCharge);
    System.out.printf("\n");

使用printf,您需要格式化您正在打印的值,在这种情况下,因为您尝试打印双精度数,所以需要使用%f格式化

我还注意到我之前的回答弄乱了你输出的其余部分的间距。所以这是固定的解决方案!只需将间距更改为您想要的

答案 1 :(得分:0)

或者你可以

使用 将两个参数传递给printf

System.out.printf("Adjusted Hire Charge: %77s\n", "$" + adjustedCharge + "\n")

而不是

 System.out.printf("Adjusted Hire Charge: %77s\n", "\n", "$" + adjustedCharge + "\n");