首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tip计算器语句混淆

Tip计算器语句混淆
EN

Stack Overflow用户
提问于 2014-09-27 23:00:32
回答 2查看 284关注 0票数 0

这是密码。基本上,在我输入第二个客户账单金额后,它会提前给我看一条提示,并询问是否还有其他客户。我要它问我是否有其他客户,但我不希望它问有关小费,直到我输入"n“。有什么想法吗?

代码语言:javascript
复制
import java.util.Scanner;


public class H3_TipCalc {

  public static void main(String[] args) {

    Scanner input = new Scanner (System.in);
    System.out.println("Please enter your bill amount.");
    double bill = input.nextDouble();

    String multiplecust = ("Y");
    //int mc=1;

    while (multiplecust.equalsIgnoreCase("y")) 

    {


            Scanner usrin = new Scanner (System.in);

        System.out.println("Is there another customer? y or n?");
        multiplecust = usrin.nextLine();
        //mc++;



        if (multiplecust.equals("y")){
            System.out.println("Please enter their bill amount.");

            }
        else if (multiplecust.equals("n")){
            System.out.println("What tip prcentage would you like to use? Enter as a decimal.");
        }

        double tip = usrin.nextDouble();
        System.out.println("Your tip owed will be " + bill*tip + ".");
    }
  }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-27 23:12:52

由于else if,您可以输入最后一个客户的账单金额或小费百分比,但不能同时输入两者。相反,尝试以下逻辑:

代码语言:javascript
复制
while( there are more individual customer amounts to enter ) {
    enter next customer amount
}

get tip percentage

show final bill

注意,提示百分比是在while循环之后输入的,因为它只是一个一次性条目。

票数 0
EN

Stack Overflow用户

发布于 2014-09-27 23:09:39

你的逻辑扭曲了。

最好在循环中移动完整的逻辑,并检查是否在循环结束时退出(而不是开始):

代码语言:javascript
复制
do
  read bill amount
  read tip amount
  read next customer
while (wants to continue)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26079972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档