我們正在進行小組作業。該計劃的一部分用於輸入「支付」,然後是學生的SIN(社會保險號碼),以及按多少扣除其帳戶。例如,pay,193678187,900
。如何使用!.equals和字符串?
將SIN與來自文件的SIN列表進行比較,生成一個數組並進行比較,curSin.equals(stuSin))
。 curSin
和stuSin
都是字符串。
如果找到SIN,它將減少學生餘額(未顯示)的金額並將其寫入文件write(student,inrec)
。
如果未找到SIN,則應返回「SIN未找到」。
我們的問題是,即使找到SIN,它仍然會返回「SIN未找到」。我們認爲這與if語句和不等於比較有關,但我們已經嘗試了所有我們能想到的並且無法使其正常工作的東西。
任何幫助將不勝感激。 謝謝。
下面的代碼片段。
if (cmd.equals("pay") && p.length == 3)
{ // first word is "pay";three terms (cmd, sin, $)
System.out.println("pay");
// Pay: decreases student payment, notifies of overpayment
String stuSin = p[1];
double stupay = Double.parseDouble(p[2]);
for (int i = 0; i < student.length; i++)
{
Student x1 = student[i];
String curSin = x1.getSin();
if (curSin.equals(stuSin))
{
double curpay = x1.getBal();
double newBal = curpay - stupay;
if (stupay > curpay)
{
JOptionPane.showMessageDialog(null, "Overpayment");
System.exit(0);
}
x1.setBal(newBal);
System.out.println(x1.getBal());
write(student, inrec);
}
else if (!curSin.equals(stuSin))
{
JOptionPane.showMessageDialog(null, "SIN not found"); // sin not found
System.exit(0);
}
}// end of for loop
}
調試!在if之前加上'System.out.println(cmd)'。另外'p.length == 3'是多餘的。 – 2012-04-11 20:00:44
爲什麼在你的陳述中你有一個條件?你在第一個if(if(curSin.equals(stuSin)))中檢查相同的條件。 – 2012-04-11 20:00:46
@TomaszNurkiewicz我不認爲p.length是多餘的。由於這只是一個代碼片段,所以不可能告訴,但我猜想這是需要的。 – emory 2012-04-11 20:08:09