2012-03-13 5 views
0

穏やかで、これは私の初めてのことです。 :)基本的な銀行プログラムはdoループを無限に通過しています。私は間違って何をしていますか?

ここではダメージがあります:私はそれまで、私はうまくやっている非主要なプログラミングクラスです。しかし、その後、この怪物があった。このプロジェクトは単純な銀行プログラムですが、私は永遠にループするか、知られていないcharを持つ問題があります。

ここまでは私のコードです。もしそれが奇妙に見えたり、非効率的なやり方で行われたりするのは、それがクラスが私を連れてきた場所だからです。参考までに、次のクラスは配列(アイデアなし)についてです。

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

!その条件が常に真であるため、

+0

あなたは実際にどこで 'input'を読んでいますか? 'input = s.nextLine()'のようなものです。 – jv42

答えて

1

あなたのwhileループは、無限である:

while(input != 'q' || input != 'Q'); 

input == 'q'場合、input != 'Q'、式がtrueと評価、およびその逆ようにします。 ループは決して終了しません。条件を論理的に正しくするには、||&&に変更する必要があります。

0

ようこそ私の友人!

'または'ステートメント(||)を 'および'(& &) - > 'q'に等しくなく、 'Q'と等しくない間に底を変更しようとしましたか?あなたの現在のステートメントは、両方になることはできないので、常に真です。

つまり、(入力!= 'q' & &入力!= 'Q');に変更します。

私はこの理論をテストするためにオンラインのJavaコンバーターを見つけることができないので、これが間違っていると私に知らせてください、私は投稿を削除します。

ある時点で誰もがIfsとAndsによってキャッチされます。

関連する問題