如何在用户输入“错误”后重置 double、int、float 等的值。

huangapple go评论63阅读模式
英文:

How to reset the value of a double, int, float etc after an "Incorrect" input by the user

问题

以下是您的代码翻译:

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("请输入您的密码。");

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

      if (input == pin) {
        System.out.println("正确");
        System.out.println("欢迎使用您的ATM");
        while (true) { // 除非选择“退出”,否则持续显示选项

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

          System.out.println(a + " - 查询余额");
          System.out.println(b + " - 取款");
          System.out.println(c + " - 存款");
          System.out.println(d + " - 退出");
          System.out.println("请选择您要进行的操作。");

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

          if (menuinput == a) {
            System.out.println(balance);
            continue;
          }
          if (menuinput == b) {
            System.out.println("请输入取款金额。");
            Scanner withdrawamount = new Scanner(System.in);
            double withdrawBalance = withdrawamount.nextDouble();
            if (withdrawBalance > balance)
              System.out.println("余额不足");
            else {
              balance -= withdrawBalance;
              System.out.println("您的新余额为 " + balance);
            }
          }
          if (menuinput == c) {

          }
          if (menuinput == d) {
            break;
          }
          if (attempts == 0) {
            System.out.println("已超出最大尝试次数");
          }
        }
      } else {
        System.out.println("错误");
        attempts--;
        System.out.println("您还有 " + attempts + " 次尝试机会。");
      }
    }
  }
}

注意:在您的原始代码中,有一些HTML实体编码(">)被使用,但是在翻译后这些编码已经被移除,代码中直接使用双引号和大于号。

英文:

My question is in relation to my line of code resembling an ATM. The current balance being 10000, What i have written shows a message to the user that if they withdraw over the given balance that it is Insufficient balance. However the balance continues to change thus when Inquiring balance the balance is now < the allowed balance. My question is how should i go about resetting the double value for the balance when the Insufficient balance message is shown to the user?

Here is my code.

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(&quot;Please enter your pin.&quot;);
while (attempts &gt; 0) {
Scanner keyboardpin = new Scanner(System.in);
long input = keyboardpin.nextLong();
if (input == pin) {
System.out.println(&quot;Correct&quot;);
System.out.println(&quot;Welcome to your ATM&quot;);
while (true) { // Keep printing your options unless &quot;Quit&quot; is chosen
int a = 1;
int b = 2;
int c = 3;
int d = 0;
System.out.println(a + &quot; - Inquire Balance&quot;);
System.out.println(b + &quot; - Withdraw&quot;);
System.out.println(c + &quot; - Deposit&quot;);
System.out.println(d + &quot; - Quit&quot;);
System.out.println(&quot;Please select what you want to do.&quot;);
Scanner menuselect = new Scanner(System.in);
int menuinput = menuselect.nextInt();
if (menuinput == a) {
System.out.println(balance);
continue;
}
if (menuinput == b) {
System.out.println(&quot;Please enter a withdrawal amount.&quot;);
Scanner withdrawamount = new Scanner(System.in);
double withdrawbalace = withdrawamount.nextDouble();
balance = (balance - withdrawbalace);
if (withdrawbalace &gt; balance)
System.out.println(&quot;Insufficient balance&quot;);
if (withdrawbalace &lt;= balance)
System.out.println(&quot;Youre new balance is &quot; + balance);
}
if (menuinput == c) {
}
if (menuinput == d) {
break;
}
if (attempts == 0) {
System.out.println(&quot;Maximum number of attempts exceeded&quot;);
}
}
} else {
System.out.println(&quot;Wrong&quot;);
attempts--;
System.out.println(&quot;You have &quot; + attempts + &quot; attempts remaining.&quot;);
}
}
}
}

答案1

得分: 3

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

if (menuinput == b) {
    System.out.println("请输入取款金额。");
    Scanner withdrawamount = new Scanner(System.in);
    double withdrawbalance = withdrawamount.nextDouble();
    if (withdrawbalance > balance)
        System.out.println("余额不足");
    if (withdrawbalance <= balance)
        balance = (balance - withdrawbalance);
        System.out.println("您的新余额为 " + balance);
}
英文:

Just check whether the user has enough balance before subtracting the amount.

if (menuinput == b) {
System.out.println(&quot;Please enter a withdrawal amount.&quot;);
Scanner withdrawamount = new Scanner(System.in);
double withdrawbalace = withdrawamount.nextDouble();
if (withdrawbalace &gt; balance)
System.out.println(&quot;Insufficient balance&quot;);
if (withdrawbalace &lt;= balance)
balance = (balance - withdrawbalace);
System.out.println(&quot;Youre new balance is &quot; + balance);
}
</details>
# 答案2
**得分**: 1
您的主要问题已在先前的回答中得到解决,但您的代码逻辑存在一些错误。我已经为您修复了它。
```java
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("请输入您的密码。");
Scanner keyboard = new Scanner(System.in);
while (attempts > 0) {
long input = keyboard.nextLong();
if (input == pin) {
System.out.println("正确");
System.out.println("欢迎使用ATM");
// 除非选择“退出”,否则始终显示选项
while (true) {
int a = 1;
int b = 2;
int c = 3;
int d = 0;
System.out.println(a + " - 查询余额");
System.out.println(b + " - 取款");
System.out.println(c + " - 存款");
System.out.println(d + " - 退出");
System.out.println("请选择您要进行的操作。");
int menuInput = keyboard.nextInt();
if (menuInput == a) {
System.out.println(balance);
} else if (menuInput == b) {
System.out.println("请输入取款金额。");
double withdrawBalance = keyboard.nextDouble();
if (balance >= withdrawBalance) {
balance -= withdrawBalance;
System.out.println("您的新余额为:" + balance);
} else System.out.println("余额不足");
} else if (menuInput == c) {
// 存款代码在此处
} else if (menuInput == d) break;
}
} else {
attempts--;
if (attempts == 0) {
System.out.println("已超过最大尝试次数");
break;
}
System.out.println("错误");
System.out.println("您还有 " + attempts + " 次尝试机会。");
}
}
keyboard.close();
}
}

以下是一些建议...

  1. 尽量选择有意义的变量名。
  2. 如果变量具有恒定的值,请将其设为 final
  3. 如果变量名称由两个或更多单词组成,请将第二个单词的首字母大写(例如 menuInput)。
  4. 如果您对不同情况检查相同的条件,可以使用 switchifelse ifelse
  5. 切勿忘记关闭您使用的对象,以释放内存。

祝您好运!

英文:

Your main problem has been solved in the previous answer, but your code logic has some errors. I have fixed it for you.

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(&quot;Please enter your pin.&quot;);
        Scanner keyboard = new Scanner(System.in);

        while (attempts &gt; 0) {
            long input = keyboard.nextLong();

            if (input == pin) {
                System.out.println(&quot;Correct&quot;);
                System.out.println(&quot;Welcome to your ATM&quot;);

                // Keep printing your options unless &quot;Quit&quot; is chosen
                while (true) {
                    int a = 1;
                    int b = 2;
                    int c = 3;
                    int d = 0;

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

                    int menuInput = keyboard.nextInt();

                    if (menuInput == a) {
                        System.out.println(balance);

                    } else if (menuInput == b) {
                        System.out.println(&quot;Please enter a withdrawal amount.&quot;);
                        double withdrawBalance = keyboard.nextDouble();

                        if (balance &gt;= withdrawBalance) {
                            balance -= withdrawBalance;
                            System.out.println(&quot;Your new balance is &quot; + balance);
                        } else System.out.println(&quot;Insufficient balance&quot;);

                    } else if (menuInput == c) {
                        // Deposit code here
                    } else if (menuInput == d) break;
                }

            } else {
                attempts--;

                if (attempts == 0) {
                    System.out.println(&quot;Maximum number of attempts exceeded&quot;);
                    break;
                }
                System.out.println(&quot;Wrong&quot;);
                System.out.println(&quot;You have &quot; + attempts + &quot; attempts remaining.&quot;);
            }
        }
        keyboard.close();
    }
}

And here are some tips...

  1. Try to choose meaningful variable names.
  2. If the variable has a constant value make it final
  3. If the variable name consists of two, or more, words capitalize the first letter of the second word (eg. menuInput)
  4. If you check the same condition for different cases you can use switch or if, else if, else
  5. Never forget to close the objects you used to release memory

Good Luck

huangapple
  • 本文由 发表于 2020年8月30日 11:59:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63653816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定