温柔一点,这是我第一次。 :)
损害是这样的:我正在上一门非专业的编程课,到目前为止,我一直表现得很好。但后来出现了这个怪物。这个项目只是一个简单的银行程序,但我遇到了一些问题,要么它永远循环,要么字符未知。
这是到目前为止我的代码;如果它看起来很奇怪或者可能是以一种无效的方式完成的,那是因为这就是类(class)带我去的地方。仅供引用,下一个类是关于数组的(不知道)。
import java.text.*;
import java.util.*;
/**
* Bank program
*
* @********
* @3
*/
public class Proj3also
{
public static void main(String []args)
{
DecimalFormat df = new DecimalFormat("#0.00");
System.out.println("Welcome to the banking program.");
System.out.println("");
Scanner s = new Scanner(System.in);
System.out.print("Please enter your starting balance: ");
double bal = Double.parseDouble(s.nextLine());
System.out.print("");
double deposit;
double withdraw;
do
{ System.out.print("Enter (d)eposit, (w)ithdraw, (b)alance check, or (q)uit: ");
if (input == 'b' || input == 'B')
{
System.out.print("Your balance is: $");
System.out.println(df.format(bal));
System.out.println("");
}
else if (input == 'd' || input == 'D')
{
System.out.print("Enter the amount to deposit: ");
deposit = Double.parseDouble(s.nextLine());
bal = bal + deposit;
System.out.println("");
}
else if (input == 'q' || input == 'Q')
{
System.out.print("");
System.out.print("Exiting the banking program. Goodbye!");
}
else if (input == 'w' || input == 'W')
{
System.out.print("Enter the amount to withdraw: ");
withdraw = Double.parseDouble(s.nextLine());
if (withdraw > bal)
{
System.out.println("Transaction failed. Withdraw amount cannot exceed balance.");
System.out.println("");
}
else
{
bal = bal - withdraw;
System.out.println("");
}
}
}
while(input != 'q' || input != 'Q');
}
}
这就是讲师的计划:
http://i15.photobucket.com/albums/a376/decode_6/project3.png
任何帮助将不胜感激!
最佳答案
你的 while 循环是无限的,因为它的条件始终为真:
while(input != 'q' || input != 'Q');
如果input == 'q'
,则input != 'Q'
,因此表达式的计算结果为 true,反之亦然。
你的循环永远不会终止。您需要将 ||
更改为 &&
以使条件在逻辑上正确。
关于java - 基本的银行程序无限地执行 do 循环。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9693985/