gpt4 book ai didi

java - Java孤儿案

转载 作者:行者123 更新时间:2023-12-01 18:11:08 24 4
gpt4 key购买 nike

孤立案例错误以下是将在程序中完成的任务:-

  1. 接受客户存款并更新余额。
  2. 显示余额。
  3. 计算并存入复利。
  4. 允许提款并更新余额。
  5. 检查最低余额,如有必要,处以罚款并更新余额

我收到“孤立案例”错误

情况1:S1.Display();

情况1:S2.Display();

请帮忙。这是代码:

import java.util.*;

class Account

{
String custname;

double accno;

double bal;


Account(String c, double a, double b)

{custname = c;

accno = a;

bal = b;
}



void Display()

{System.out.println("Account Holder: "+custname);

System.out.println("Account Number "+accno);
System.out.println("Balance : "+bal);
}



void Deposit()

{double dep;

Scanner sc = new Scanner(System.in);

System.out.println("Please Enter the amount your want to deposit:");

dep = sc.nextDouble();

bal = bal + dep;

System.out.println("Updated Details....");

Display();
}


void Withdraw()

{double wth;

Scanner sc = new Scanner(System.in);

System.out.println("Please enter the amount you want to withdraw");

wth = sc.NextDouble();

bal = bal - wth;

System.out.println("Updated details....");

Display();

}

}



class SavAccount extends Account

{ String acctype;


SavAccount(String c, double a, double b)
{Super(c, a, b);

acctype = "Savings";

}



void ComInt()

{int months;

Scanner sc = new Scanner(System.in);

System.out.println("Please enter the duration of the account in months");

months = sc.NextInt();

double rate, inte;
rate = 0.04;

inte = months *rate * bal;

bal = bal + inte;

System.out.println("Compund Interest : "+inte);
}



void Display()

{System.out.println("Account Holder: "+custname);

System.out.println("Account Number "+accno);

ComInt();

System.out.println("Balance : "+bal);

System.out.println("Account Type: "+acctype);

}

}



class CurAccount extends Account

{String acctype;

CurAccount(String c, double a, double b)

{Super(c, a, b);

acctype = "Current";

}


void Penalty()
{
if(bal<5000.00)

{bal = bal - 100.00;
System.out.println("Fine deducted Rs.100/-");

}
}

void Display()

{System.out.println("Account Holder: "+custname);

System.out.println("Account Number "+accno);

Penalty();

System.out.println("Balance : "+bal);

System.out.println("Account Type: "+acctype);

if(bal<=5000.00)

{System.out.println("Warning!! Please maintain balance above Rs.5000/-");

}
}


}
class Accmain
{public static void main(Strings args[])

{SavAccount S1 = new SavAccount("Aniruddha", 134.00, 15000.00)
;
CurAccount S2 = new CurAccount("Tyrel" , 135.00, 6000.00);

int num = 2;


String c = "y";

int n;

double temp;
double accs[] = new double[10];

accs[0] = S1.accno;

accs[1] = S2.accno;

Scanner sc = new Scanner(System.in);

while (c == "y");

{System.out.println("Please enter your Account number:");

temp = sc.nextDouble();

if(accs[0] == temp)

{System.out.println("Welcome "+ S1.custname);

System.out.println("Account Type: "+ S1.acctype);

System.out.println("1.Display");

System.out.println("2.Withdraw");

System.out.println("3.Deposit");

n = sc.nextInt();

Switch(n)
;
{
case 1 : S1.Display();

case 2 : S1.Withdraw();

case 3 : S1.Deposit();

default :System.out.println("Bad Choice ");

c = "n";

}

}
else if(accs[1] == temp)

{System.out.println("Welcome "+ S2.custname);

System.out.println("Account Type: "+ S2.acctype);

System.out.println("1.Display");

System.out.println("2.Withdraw");

System.out.println("3.Deposit");

n = sc.nextInt()
;
Switch(n);
{
case 1 : S2.Display();

case 2 : S2.Withdraw();

case 3 : S2.Deposit();
default :System.out.println("Bad Choice ");

c = "n";
}

}

}

}



}

最佳答案

几个问题。

1) 额外的 ;最终使得切换在该语句上立即完成。

Switch(n); <--

显然所有病例都变成了孤儿。

如果删除 Switch(n) 之后的冒号你会没事的。

 Switch(n)
{
case 1 : S1.Display();

case 2 : S1.Withdraw();

2) 之后你又遇到了另一个问题。每个案例之后您都需要休息。否则,即使找到匹配项,您的交换机也会执行所有情况。要停止该情况,请在每个案例后添加 break

  Switch(n)
{
case 1 : S1.Display(); break;

case 2 : S1.Withdraw();break;
...

3) 当您写 c == "y" 时比较引用而不是相等。

您需要使用equals()检查字符串相等性的方法

阅读How do I compare strings in Java?

4) 该行 Super(c, a, b);不会编译为 S一定是小写的。 Java 区分大小写。

关于java - Java孤儿案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32880666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com