java - 无法获取要打印的 java 变量

标签 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));

    }

}

最佳答案

我刚刚更改了打印 adjustmentCharge 的方式,因此您仍然可以使用 printf

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

使用 printf,您需要格式化要打印的值,在本例中,因为您尝试打印 double 值,所以需要使用 %f 对其进行格式化

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

关于java - 无法获取要打印的 java 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30961230/

相关文章:

java - 获取与系统格式相同的系统日期

Java Spring 安全密码编码器

r - 为什么 R 对象不在函数或 "for"循环中打印?

delphi - 如何在TPrinter上以实际尺寸打印图像?

delphi - 获取默认纸盒的名称?

file - 如何使用Golang在网络打印机上打印文件或格式文本

java - 为什么我的 Java 应用程序在全屏模式下运行如此缓慢? (开窗时很好)

java - 来自不同类的同名调用方法

css - 在打印预览中显示 : -ms-flexbox ! 重要 > 使 Internet Explorer 10 崩溃

java - spring mvc 验证异常不由 ControllerAdvice 处理