有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java基本银行程序正在内蕴地通过do循环。我做错了什么?

温柔点,这是我第一次

这是一个伤害:我在一个非专业的编程课上,到目前为止,我一直做得很好。但后来有了这个怪物。这个项目只是一个简单的银行计划,但我有一些问题,要么它永远循环或字符(s)不知道

这是我目前的代码;如果这看起来很奇怪,或者可能是做得不好,那是因为这就是课堂带我去的地方。仅供参考,下一节课是关于数组的(不知道)

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

任何帮助都将不胜感激


共 (2) 个答案

  1. # 1 楼答案

    while循环是无限的,因为它的条件始终为真:

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

    如果input == 'q',那么input != 'Q',那么表达式的计算结果为true,反之亦然。 你的循环永远不会结束。您需要将||更改为&&以使条件在逻辑上正确

  2. # 2 楼答案

    欢迎来到我的朋友

    您是否尝试过将底部的while'或'语句(| |)改为'and'(&;&;)->;虽然它不等于q,也不等于q,但请继续。你现在的说法永远都是正确的,因为不能两者都是

    即更改为->;while(输入!='q'&输入!='q')

    我找不到在线Java转换器来测试这个理论,所以如果这是错误的,请告诉我,我将删除这篇文章

    每个人都会在某个时刻被“如果”和“和”所迷惑