英文:
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("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.");
}
}
}
}
答案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("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);
}
</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();
}
}
以下是一些建议...
- 尽量选择有意义的变量名。
- 如果变量具有恒定的值,请将其设为
final
。 - 如果变量名称由两个或更多单词组成,请将第二个单词的首字母大写(例如
menuInput
)。 - 如果您对不同情况检查相同的条件,可以使用
switch
或if
、else if
、else
。 - 切勿忘记关闭您使用的对象,以释放内存。
祝您好运!
英文:
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("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();
}
}
And here are some tips...
- Try to choose meaningful variable names.
- If the variable has a constant value make it
final
- If the variable name consists of two, or more, words capitalize the first letter of the second word (eg.
menuInput
) - If you check the same condition for different cases you can use
switch
orif
,else if
,else
- Never forget to close the objects you used to release memory
Good Luck
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论