[英]Rudimentary bank program is going through the do loop infinately. What am I doing wrong?
溫柔一點,這是我第一次。 :)
造成的損害是:到目前為止,我參加的是非主流編程班,我一直都很好。 但是后來有這個怪物。 這個項目只是一個簡單的銀行程序,但是我遇到了一些問題,要么它永遠循環,要么不知道字符。
到目前為止,這是我的代碼; 如果它看起來很奇怪,或者以一種無效的方式完成,那是因為那是班上帶我去的地方。 僅供參考,下一堂課是關於數組的(不知道)。
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循環是無限的,因為其條件始終為true:
while(input != 'q' || input != 'Q');
如果input == 'q'
,則input != 'Q'
,因此表達式的計算結果為true,反之亦然。 您的循環將永遠不會終止。 您需要更改||
&&
使條件在邏輯上正確。
歡迎來到我的朋友stackoverflow!
您是否嘗試過將'or'語句(||)更改為'and'(&&)->的底部,而其底部不等於'q'並且不等於'Q',則繼續。 您當前的陳述將始終為真,因為兩者不能同時為真。
即更改為-> while(input!='q'&& input!='Q');
我找不到在線Java轉換器來測試該理論,所以讓我知道這是否是錯誤的,我將刪除該帖子。
每個人在某個時候都會被Ifs和Ands迷住。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.