java - 计算退房价格

标签 java methods

对于这个程序,我们应该计算用户输入的商品的总价。我们正在使用获取范围 double 的方法和询问用户是否要继续的方法。以下是我使用的方法:

public static double getRangedDouble(Scanner src, String prompt, double lo, double hi)
    {
        double retVal = lo - 1;
        String trash;
        do
        {
            System.out.print(prompt + " " + lo + " - " + hi);
            if(src.hasNextInt())
            {
                retVal = src.nextInt();
            }
            else
            {
                trash = src.nextLine();
            }
        }while(retVal < lo || retVal > hi);

        return retVal;
    }

    public static Boolean getYNConfirm(Scanner src, String prompt)
    {
        String input = "";
        boolean done=true;

        System.out.println(prompt + " Y or N");
        while(!done)
        {
            input = src.nextLine();
            if(input.equalsIgnoreCase("Y"))
            {
                done=true;
            }
            else if(input.equalsIgnoreCase("N"))
            {
                done=false;
            }
            else
            {
                System.out.println("Y or N");
            }
        }
        return done;
    }

这是直接来 self 的作业的描述:

At the 10$ store nothing is more than $10.00. Prompt the user for the price of their item (.50 cents to $9.99 dollars) using the getRangedDouble method and continue to input items as long as they indicate that they have more using your getYNConfirm method. Display the total cost of the item(s) to 2 decimal places with printf.

我知道如何使用程序中的方法,但我不知道如何让 getYNConfirm 方法起作用或如何在用户输入单独的价格时计算总价。任何帮助将不胜感激。

最佳答案

好吧,让我们进入你的问题:

At the 10$ store nothing is more than $10.00. Prompt the user for the price of their item (.50 cents to $9.99 dollars) using the getRangedDouble method and continue to input items as long as they indicate that they have more using your getYNConfirm method. Display the total cost of the item(s) to 2 decimal places with printf.

对于这个级别的任何编码问题,要做的第一件大事就是将其分解成各个组成部分。对于这个问题,这些是:

  1. 能够向用户询问商品的价格(勾选)
  2. 能够询问用户是否还有其他项目要输入(勾选......见下文)
  3. 只要第二步为真,就循环以上两步(TODO)
  4. 能够对循环的每个价格步骤中给出的值求和(TODO)

对于第 1 步,我们有 getRangedDouble(...),它会 check out 。为方便起见复制如下:

public static double getRangedDouble(Scanner src, String prompt, double lo, double hi){
  double retVal = lo - 1;
  String trash;
  do{
    System.out.print(prompt + " " + lo + " - " + hi);
    if(src.hasNextInt()){
       retVal = src.nextInt();
    } else {
       trash = src.nextLine();
    }
  } while(retVal < lo || retVal > hi);
  return retVal;
}

对于第 2 步,我们有 getYNConfirm(...) 方法,此处给出:

public static Boolean getYNConfirm(Scanner src, String prompt){
  String input = "";
  boolean done=true;
  System.out.println(prompt + " Y or N");
  while(!done){
    input = src.nextLine();
    if(input.equalsIgnoreCase("Y")){
      done=true;
    } else if(input.equalsIgnoreCase("N")) {
      done=false;
    } else {
      System.out.println("Y or N");
    }
  }
  return done;
}

不幸的是,这里面有一个逻辑错误。您将 done 初始化为 true,并且您的 while 循环结束了 while(!done) 条件。因此这是第一次 while(!true) --> while(false),它不执行。所以永远不会进入 while 循环,因此每次调用该方法时我们都会返回 true。要解决此问题,请考虑一旦看到“Y”就做什么——您最终会返回 true。您的方法首先经历了打破循环的 Action ,但我们可以跳过该步骤并直接跳到返回 true。因此考虑这个版本的 getYN... 方法:

public static boolean getYNConfirm(Scanner src, String prompt){
  String input = "";
  System.out.println(prompt + " Y or N");
  while(true){ //Loops forever until a return or break statement 
    input = src.nextLine();
     if(input.equalsIgnoreCase("Y")){
       return true;
     } else if(input.equalsIgnoreCase("N")) {
       return false;
     } else {
       System.out.println("Y or N");
     }
   }
}

这符合您原始版本的方法的意图,没有逻辑错误。

现在我们进入了大结局 - 编写一个方法,使用上述两种方法作为助手,循环并不断向用户询问更多价格,并在进行时求和。让我们把这个方法写成 main(String[] args),这样我们就可以运行它,看看会发生什么。

我们想在这里使用一个循环,以允许用户继续输入价格直到完成。我们可以使用伪代码对我们的问题进行建模,如下所示:

while(user not done inputting prices){
    get next price
    add price to sum of prices
    ask the user if they would like to continue
}
print sum of prices

就像您可以调用内置方法并存储输出结果(如 rentVal = src.nextInt())一样,您可以对您编写的方法执行相同的操作。例如,我们可以要求用户使用 getRangedDouble(...) 输入下一个价格。根据我们编写的方法头,它返回一个 double 类型的值,因此当我们存储此调用的输出时,它应该在一个 double 变量中:double nextPrice = getRangedDouble(...);

如果伪代码有意义,后面的代码其实比较简单:

public static void main(String[] args){

  Scanner s = new Scanner(System.in); //Allow for reading user input from console

  boolean looping = true; //True so long as the user isn't done inputting prices.
                          //Initially true so that at least one price is entered
  double sum = 0.0;       //The sum of prices thus far
  while(looping){
    double nextPrice = getRangedDouble(s, "Enter a price in the range", 0.5, 10.0);
    sum = sum + nextPrice; //Add the price into the sum
    looping = getYNConfirm(s, "Add another item to your cart?");
  }
  System.out.println(String.format("%.2f", sum)); //Prints sum, limited to 2 decimal places
}

关于java - 计算退房价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29725376/

相关文章:

java - MVC 中的异常包装

python - 在 Python 中是否可以声明必须重写该方法?

c# - 将 PHP 转换为 .Net C#

java - 如何将JTextField变成String/Long?

java - 满堂彩的计算方法不起作用

java - 如何监听发送到 ActorSystem 的消息(Akka 2.0)

JavaFX Java 在 Controller 之间传递参数字符串

java - SWT/JFace 等同于 JComponent.setDefaultLocale()?

ruby-on-rails - 如何使用 rspec 测试 Controller 方法?

java - 如何让这段代码进入循环,以便每次都询问用户输入,直到结果为 "kill"?