【问题标题】:How to reset the value of a double, int, float etc after an "Incorrect" input by the user如何在用户输入“不正确”后重置 double、int、float 等的值
【发布时间】:2020-12-18 13:41:09
【问题描述】:

我的问题与我的类似于 ATM 的代码行有关。当前余额为10000,我写的内容向用户显示了一条消息,如果他们退出给定的余额,则它是Insufficient balance。然而,当Inquiring balance 余额现在小于允许的余额时,余额会继续变化。我的问题是当Insufficient balance 消息显示给用户时,我应该如何重置余额的双倍值?

这是我的代码。

import java.util.Scanner;

class app {
  public static void main(String[] args)
  {
    long pin = 2927942074l;
    double balance = 10000.0;
    int attempts = 3;

    System.out.println("Please enter your pin.");

    while (attempts > 0) {
    Scanner keyboardpin = new Scanner(System.in);
    long input = keyboardpin.nextLong();

    if (input == pin) {
          System.out.println("Correct");
          System.out.println("Welcome to your ATM");
        while (true) { // Keep printing your options unless "Quit" is chosen

          int a = 1;
          int b = 2;
          int c = 3;
          int d = 0;

            System.out.println(a + " - Inquire Balance");
            System.out.println(b + " - Withdraw");
            System.out.println(c + " - Deposit");
            System.out.println(d + " - Quit");
            System.out.println("Please select what you want to do.");

          Scanner menuselect = new Scanner(System.in);
          int menuinput = menuselect.nextInt();

          if (menuinput == a) {
              System.out.println(balance);
              continue;
          }
          if (menuinput == b) {
            System.out.println("Please enter a withdrawal amount.");
          Scanner withdrawamount = new Scanner(System.in);
          double withdrawbalace = withdrawamount.nextDouble();
          balance = (balance - withdrawbalace);
          if (withdrawbalace > balance)
            System.out.println("Insufficient balance");
          if (withdrawbalace <= balance)
            System.out.println("Youre new balance is " + balance);
          }
          if (menuinput == c) {

          }
          if (menuinput == d) {
              break;
          }
      if (attempts == 0) {
          System.out.println("Maximum number of attempts exceeded");
          }
        }
      } else {
      System.out.println("Wrong");
      attempts--;
      System.out.println("You have " + attempts + " attempts remaining.");
      }
    }
  }
}

【问题讨论】:

    标签: java variables integer double reset


    【解决方案1】:

    在扣除金额之前,只需检查用户是否有足够的余额。

    if (menuinput == b) {
        System.out.println("Please enter a withdrawal amount.");
        Scanner withdrawamount = new Scanner(System.in);
        double withdrawbalace = withdrawamount.nextDouble();
        if (withdrawbalace > balance)
            System.out.println("Insufficient balance");
        if (withdrawbalace <= balance)
            balance = (balance - withdrawbalace);
            System.out.println("Youre new balance is " + balance);
    }
    

    【讨论】:

      【解决方案2】:

      你的主要问题已经在上一个答案中解决了,但是你的代码逻辑有一些错误。我已经给你修好了。

      import java.util.Scanner;
      
      class app {
      
          public static void main(String[] args) {
              final long pin = 2927942074L;
              double balance = 10000.0;
              int attempts = 3;
      
              System.out.println("Please enter your pin.");
              Scanner keyboard = new Scanner(System.in);
      
              while (attempts > 0) {
                  long input = keyboard.nextLong();
      
                  if (input == pin) {
                      System.out.println("Correct");
                      System.out.println("Welcome to your ATM");
      
                      // Keep printing your options unless "Quit" is chosen
                      while (true) {
                          int a = 1;
                          int b = 2;
                          int c = 3;
                          int d = 0;
      
                          System.out.println(a + " - Inquire Balance");
                          System.out.println(b + " - Withdraw");
                          System.out.println(c + " - Deposit");
                          System.out.println(d + " - Quit");
                          System.out.println("Please select what you want to do.");
      
                          int menuInput = keyboard.nextInt();
      
                          if (menuInput == a) {
                              System.out.println(balance);
      
                          } else if (menuInput == b) {
                              System.out.println("Please enter a withdrawal amount.");
                              double withdrawBalance = keyboard.nextDouble();
      
                              if (balance >= withdrawBalance) {
                                  balance -= withdrawBalance;
                                  System.out.println("Your new balance is " + balance);
                              } else System.out.println("Insufficient balance");
      
                          } else if (menuInput == c) {
                              // Deposit code here
                          } else if (menuInput == d) break;
                      }
      
                  } else {
                      attempts--;
      
                      if (attempts == 0) {
                          System.out.println("Maximum number of attempts exceeded");
                          break;
                      }
                      System.out.println("Wrong");
                      System.out.println("You have " + attempts + " attempts remaining.");
                  }
              }
              keyboard.close();
          }
      }
      

      这里有一些提示...

      1. 尝试选择有意义的变量名。
      2. 如果变量具有常数值,则使其为final
      3. 如果变量名包含两个或多个单词,则第二个单词的第一个字母大写(例如menuInput
      4. 如果您检查不同情况下的相同条件,您可以使用switchifelse ifelse
      5. 永远不要忘记关闭用于释放内存的对象

      祝你好运

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-04
        • 2018-07-07
        • 2014-07-09
        • 2023-03-14
        • 2020-02-12
        • 2015-03-17
        相关资源
        最近更新 更多