java - 基本的银行程序无限地执行 do 循环。我究竟做错了什么?

标签 java loops if-statement

温柔一点,这是我第一次。 :)

损害是这样的:我正在上一门非专业的编程课,到目前为止,我一直表现得很好。但后来出现了这个怪物。这个项目只是一个简单的银行程序,但我遇到了一些问题,要么它永远循环,要么字符未知。

这是到目前为止我的代码;如果它看起来很奇怪或者可能是以一种无效的方式完成的,那是因为这就是类(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/

相关文章:

file - 大文件的 awk 和 sum 行

Python - 循环运行 3 次

带有 boolean 值的 JavaScript if 语句

java - 在 Spark JavaRDD 转换中使用可序列化的 lambda

java - 如何使用 uriSMSURI 从收件箱中获取未读消息?

处理部分大型数据集后,PHP PDO fetch() 循环终止

log(n) 的 3 个嵌套循环的 Java Big O 表示法

javascript - 循环未完成

java - 在 JDBC 连接字符串中选择负载平衡策略

java - 从java搜索时Mongo Id被_id覆盖