使用方法来回答是/否

时间:2018-10-28 01:51:24

标签: java

我有这个代码。正在调用askToContinue()方法以询问用户是否要继续,但是我的问题是,无论我输入什么,它都只会忽略选择并再次启动程序。我在代码中缺少什么导致其忽略了我的选择?

public class FutureValueApp {

  public static void main(String[] args) {

    System.out.println("Welcome to the Future Value Calculator\n");

    Scanner sc = new Scanner(System.in);
    String choice = "y";
    while (choice.equalsIgnoreCase("y")) {
        // get the input from the user
        System.out.println("DATA ENTRY");
        double monthlyInvestment = getDoubleWithinRange(sc,
                "Enter monthly investment: ", 0, 1000);
        double interestRate = getDoubleWithinRange(sc,
                "Enter yearly interest rate: ", 0, 30);
        int years = getIntWithinRange(sc,
                "Enter number of years: ", 0, 100);
        System.out.println();

        // calculate the future value
        double monthlyInterestRate = interestRate / 12 / 100;
        int months = years * 12;
        double futureValue = calculateFutureValue(
                monthlyInvestment, monthlyInterestRate, months);

        // print the results
        System.out.println("FORMATTED RESULTS");
        printFormattedResults(monthlyInvestment, 
                interestRate, years, futureValue);
        System.out.println();

        askToContinue(sc);
    }
}

private static void printFormattedResults(double monthlyInvestment, 
        double interestRate, int years, double futureValue){
    // get the currency and percent formatters
    NumberFormat c = NumberFormat.getCurrencyInstance();
    NumberFormat p = NumberFormat.getPercentInstance();
    p.setMinimumFractionDigits(1);
    // format the result as a single string
    String results
      = "Monthly investment:   " + c.format(monthlyInvestment) + "\n"
      + "Yearly interest rate: " + p.format(interestRate / 100) + "\n"
      + "Number of years:      " + years + "\n"
      + "Future value:         " + c.format(futureValue) + "\n";
        System.out.println(results);
  }

public static String askToContinue(Scanner sc){
    // see if the user wants to conti1nue
    System.out.print("Continue? (y/n): ");
    String choice = sc.next();
    System.out.println();
    return choice;
}

4 个答案:

答案 0 :(得分:1)

您在正确的轨道上。改变这个

askToContinue(sc);

choice = askToContinue(sc);

因为您需要分配askToContinue返回的值到名为choice的本地引用。

答案 1 :(得分:1)

您没有将askToContinue的结果分配给在循环中检查的选择变量。 可能的困惑是askToContinue方法内部的选择变量。注意,这是一个不同的变量,不会影响while语句中检查的选择变量。

答案 2 :(得分:0)

在方法内部定义变量时,方法外部的代码无法识别该变量,即使它具有相同的名称。因此,例如,在您的代码中,

public static String askToContinue(Scanner sc){
    // see if the user wants to conti1nue
    System.out.print("Continue? (y/n): ");
    String choice = sc.next(); // this choice variable exists only for the 
                               // askToContinue method
                               // Once you assign it over here and return it 
                               // with the code below, you should use the returned 
                               // value to update the variable choice, which is 
                               // defined outside your askToContinue method
    System.out.println();
    return choice;
}

因此,正如其他答案所指出的那样,

choice = askToContinue(sc);

然后代码将正常运行,因为main方法中定义的选择变量将根据您输入的值进行更新

答案 3 :(得分:0)

基于 约翰·卡梅林(John Camerin)的答案,要跳过代码中的双 assigning ,可以在类中定义choice变量,使其成为全局static变量:

public class FutureValueApp {

public static String choice;

}

或将其作为您方法中的第二个参数发送:

askToContinue(sc,choice);