当前位置:  开发笔记 > 编程语言 > 正文

如何在没有程序退出的情况下在do-while循环中添加if语句?

如何解决《如何在没有程序退出的情况下在do-while循环中添加if语句?》经验,为你挑选了1个好方法。

我有2节课.该程序几乎模拟了ATM.用户将要求存款,取款或查看用户银行账户的余额.

首先将余额设置为零,然后用户可以存入或取出资金.

调用的第一个类CheckingAccount将具有方法而没有main方法,而TestCheckingAccount具有main方法.所有输入和输出都将在TestCheckingAccount类中进行,同时CheckingAccount进行计算.

如果用户选择存款,则帐户的余额将被添加到用户想要存入他/她的帐户的金额中.类中的getBalance方法CheckingAccount返回用户帐户的余额,同时processDeposit将用户想要的金额添加到余额中,同时processCheck减少用户选择余额的金额.

如果用户选择查看余额,则会打印用户余额.

CheckingAccount类
public class CheckingAccount{
    double myBalance = 0;

    public double getBalance(){
        return myBalance;
    }

    public void processDeposit(double amount){
        myBalance += amount;
    }

    public void processCheck(double amount){
        myBalance -= amount;
    }
}
TestCheckingAccount类
import java.util.Scanner;
public class TestCheckingAccount {

public static void main(String[] args) {

    CheckingAccount check = new CheckingAccount();

    Scanner in = new Scanner(System.in);

    String choice;
    double moneyAdded;
    double moneySubtracted;

    do
    {
        System.out.println("Deposit");
        System.out.println("Withdraw");
        System.out.println("Balance");
        System.out.println("Exit");
        System.out.println("**********************************************");
        System.out.println("Which operation would you like to use?");
        choice = in.nextLine();

        if(choice.equalsIgnoreCase("Deposit"))
        {
            System.out.println("How much money would you like to deposit?");
            moneyAdded = in.nextDouble();
            check.processDeposit(moneyAdded);
        }

        if(choice.equalsIgnoreCase("Withdraw"))
        {
            System.out.println("How much money would you like to withdraw?");
            moneySubtracted = in.nextDouble();

            check.processCheck(moneySubtracted);
        }
        else if(choice.equalsIgnoreCase("Balance"))
        {
            System.out.printf("$%.2f%n" , check.myBalance);
        }
        else if(choice.equalsIgnoreCase("Exit"))
        {
            System.exit(0);
        }

    }
    while(choice.equalsIgnoreCase("Deposit")||choice.equalsIgnoreCase("Withdraw")||choice.equalsIgnoreCase("Balance"));

    in.close();

}

}

程序运行正常,但我遇到的问题是程序必须询问用户他/她是否要存款,取款或查看余额,直到用户完成并退出程序.

因此,当程序询问用户是否要存入,取出或查看帐户中的余额时,该部分必须处于循环中.我把它放在一个do-while循环中.因此,当我运行程序并要求余额打印出余额然后循环好然后程序再次询问他/她是否想要提款,存款,查看余额或退出.

但当我要求程序撤销或存款时,它会运行但不会循环,而是退出循环.

我如何制作程序以便它可以取款或存款但仍然循环并返回菜单,询问用户是否要存款或取款而不仅仅是结束循环?



1> Elliott Fris..:

您可以使用无限循环并break在"退出"条件下.此外,您可以使用try-with-resourcesclose语句关闭您的Scanner.就像是,

try (Scanner in = new Scanner(System.in)) {      
  String choice;
  double moneyAdded;
  double moneySubtracted;

  while (true) {
    System.out.println("Deposit");
    System.out.println("Withdraw");
    System.out.println("Balance");
    System.out.println("Exit");
    System.out.println("**********************************************");
    System.out.println("Which operation would you like to use?");
    choice = in.nextLine();

    if (choice.equalsIgnoreCase("Deposit")) {
      System.out.println("How much money would you like to deposit?");
      moneyAdded = in.nextDouble();
      check.processDeposit(moneyAdded);
    } else if (choice.equalsIgnoreCase("Withdraw")) {
      System.out.println("How much money would you like to withdraw?");
      moneySubtracted = in.nextDouble();
      check.processCheck(moneySubtracted);
    } else if (choice.equalsIgnoreCase("Balance")) {
      System.out.printf("$%.2f%n", check.myBalance);
    } else if (choice.equalsIgnoreCase("Exit")) {
      break;
    }
  }
}

推荐阅读
我我檬檬我我186
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有