首页 > 解决方案 > 调试 Java 餐价计算器

问题描述

我在调试此代码时遇到了一些问题,希望大家能够提供帮助。

如果你看一下main方法接近尾声,你会发现int partySplit.split.nextInt()Scanner split = new Scanner(System.in);. 第一个问题是程序似乎跳过了这一点。为什么要跳过这组代码?

第二个问题是它会打印出“总餐价为 128 美元”,但我已经删除了System.out.println("Total meal price is $" + groupTotalMealPrice);我还重建了程序(使用 IntelliJ),看看它是否只是一个编译问题。我也复制并粘贴到一个新的类文件中,我得到了相同的结果。

我想看到的是它询问聚会的人数,然后不仅打印出总数,还打印出每个人的总数,具体取决于拆分。我也希望看到我删除的文本实际上消失了。

对此的任何帮助都会很棒。

代码:

import java.text.DecimalFormat;
import java.util.Scanner;

public class MealPriceCalculator {

    //note 'double' in 'public static double' is the return type.
    //notice that is has been changed from 'void' which means return nothing.
    //when this is changed, we will need to denote a return value.
    //in this case, we are returning the result to the main method.
    //when returning a variable, you are making it accessible in another method.
    public static double calculateTotalMealPrice(double listedMealPrice,
                                               double tipRate,
                                               double taxRate) {
        double tip = (tipRate * listedMealPrice) / 100;
        double tax = (taxRate * listedMealPrice) / 100;
        double result = listedMealPrice + tip + tax;

        return result;

    }

    //note 'void' in 'public static void' is the return type.
    //this means that nothing is returned.
    public static void main(String[] args) {

        Scanner mealPrice = new Scanner(System.in);
        Scanner tip = new Scanner(System.in);
        Scanner tax = new Scanner(System.in);
        Scanner split = new Scanner(System.in);

        System.out.println("Please enter the meal price (excluding $):");
        double listedMealPrice = mealPrice.nextDouble();
        System.out.println("You entered $" + listedMealPrice + " as the cost of your meal.");

        System.out.println("Please enter the tip percentage (excluding %)");
        double tipRate = tip.nextDouble();

        System.out.println("You entered " + tipRate + "% as the percentage you wish to tip.");

        System.out.println("Please enter the tax rate (excluding the %):");
        double taxRate = tax.nextDouble();
        System.out.println("You entered " + taxRate + "% as the tax rate.");

        System.out.println("Please enter the number of people that will be splitting the bill.");
        int partySplit = split.nextInt();
        System.out.println("You entered " + partySplit + " as the number of people you will be" +
                "splitting the bill with.");


        double groupTotalMealPrice = calculateTotalMealPrice(listedMealPrice, tipRate, taxRate);
        System.out.println(groupTotalMealPrice);

        double individualMealPrice = groupTotalMealPrice / partySplit;
        System.out.println(individualMealPrice);
    }
}

结果:

Please enter the meal price (exluding $):
100
You entered $100.0 as the cost of your meal.
Please enter the tip percentage (excluding %)
20
You entered 20.0% as the percentage you wish to tip.
Please enter the tax rate (excluding the %):
8
You entered 8.0% as the tax rate.
Your total meal price is $128.0

Process finished with exit code 0

标签: java

解决方案


您可以使用一个对象来获取所有输入,而不是为每个输入使用每个 Scanner 对象

Scanner sc= new Scanner(System.in);

double listedMealPrice = sc.nextDouble();
double tipRate = sc.nextDouble();
double taxRate = sc.nextDouble();

推荐阅读