【发布时间】:2020-07-16 19:35:08
【问题描述】:
好的!我试图让程序识别字母“E”以退出while循环。打印我的结束语句,然后程序就完成了!超过!而已!为了测试这一点,我只是在提示要求我输入一个字母时尝试退出,但无论它不会识别它或我将它切换到的任何字母(不是 D 或 W) 我试过了
- toUpperCase(),在输入之后和使用 nextLine() 方法
- while (!DepORWith.equals("E") || !DepORWith.equals("e"))
- while (!DepORWith.equals("E") && !DepORWith.equals("e"))
- 以哪种方式切换案例
- 试图将其全部更改为 char 类型(大错误)
- 将字母作为占位符,然后它就会改变
- 将 .equalsIgnoreCase() 与这些其他“解决方案”一起使用
- 使用 .isEmpty()
我已经尝试了很多,我不记得了。无论我的输入如何,其中一些“解决方案”最终都会永远循环问题。我会按 E,得到我自己的错误,再次按 E,它会循环我的错误。上帝,我需要帮助。
我只需要按 E ,打印非循环语句,然后退出程序。 我把整个代码放下来进行全面分析。 我对 Java 不熟悉,这是一个 Java 类的介绍,我真的只知道基础知识
import java.util.Scanner ;
import java.io.* ;
public class IOExam {
public static void main(String[] args) throws IOException
{
double Deposit = 0;
boolean DepStop = false;
int DEPTransactionNUM = 0;
double DepTotal = 0;
double Withdrawal = 0;
boolean WithStop = false;
int WITHTransactionNUM = 0;
double WithTotal = 0;
Scanner keyboard = new Scanner(System.in);
//Ask For choice
String DepORWith = "" ;
while (!DepORWith.equals("E"))
{
System.out.print("Deposit or Withdrawal D/W ? (Press E to Exit):") ;
DepORWith = keyboard.nextLine() ;
if (DepORWith.equalsIgnoreCase("D"))
{
// Create Document First
PrintWriter DepositTXT = new PrintWriter ("Deposit.txt");
DepositTXT.println("Transaction Number \t Amount");
DepositTXT.println("--------------------------------------");
//Input Deposit
while (DepStop == false)
{
DEPTransactionNUM += 1;
System.out.print("Input amount for deposits:") ;
Deposit = keyboard.nextDouble() ;
keyboard.nextLine() ;
if (Deposit < 0)
{
System.out.print("---ERRROR ---\nInput NON NEGATIVE amount for deposits:") ;
Deposit = keyboard.nextDouble() ;
keyboard.nextLine() ;
}
DepTotal = DepTotal + Deposit;
DepositTXT.printf("\n\t%d \t\t\t $%,.2f", DEPTransactionNUM, Deposit);
String Confirmation ;
System.out.print("Would you Like to deposit again? Y/N: ");
Confirmation = keyboard.nextLine() ;
if (Confirmation.equalsIgnoreCase("y"))
{
DepStop = false;
}
else if (Confirmation.equalsIgnoreCase("n"))
{
DepStop = true;
}
else
{
System.out.println("---ERRROR ---\nINPUT Y OR N\nWould you Like to deposit again? Y/N") ;
Confirmation = keyboard.nextLine() ;
}
}
DepositTXT.println("\n--------------------------------------");
DepositTXT.printf("\nTotal \t\t\t $%,.2f", DepTotal);
DepositTXT.close();
}
else if (DepORWith.equalsIgnoreCase("W"))
{
// Create Document First
PrintWriter WithdrawalTXT = new PrintWriter ("Withdrawal.txt");
WithdrawalTXT.println("Transaction Number \t Amount");
WithdrawalTXT.println("--------------------------------------");
//Input Withdrawals
while (WithStop == false)
{
WITHTransactionNUM += 1;
System.out.print("Input amount for withdrawal:") ;
Withdrawal = keyboard.nextDouble() ;
keyboard.nextLine() ;
if (Withdrawal < 0)
{
System.out.print("---ERRROR ---\nInput NON NEGATIVE amount for withdrawal:") ;
Withdrawal = keyboard.nextDouble() ;
keyboard.nextLine() ;
}
WithTotal = WithTotal + Withdrawal;
WithdrawalTXT.printf("\n\t%d \t\t\t $%,.2f", WITHTransactionNUM, Withdrawal);
String Confirmation ;
System.out.print("Would you Like to withdrawal again? Y/N: ");
Confirmation = keyboard.nextLine() ;
if (Confirmation.equalsIgnoreCase("y"))
{
WithStop = false;
}
else if (Confirmation.equalsIgnoreCase("n"))
{
WithStop = true;
System.out.print("Deposit or Withdrawal D/W ? (Press E to Exit):") ;
DepORWith = keyboard.nextLine() ;
}
else
{
System.out.println("---ERRROR ---\nINPUT Y OR N\nWould you Like to withdrawal again? Y/N: ") ;
Confirmation = keyboard.nextLine() ;
}
}
WithdrawalTXT.println("\n--------------------------------------");
WithdrawalTXT.printf("\nTotal \t\t\t $%,.2f", DepTotal);
WithdrawalTXT.close();
}
else
{
System.out.print("---ERRROR ---\nINPUT D OR W OR E \n") ;
System.out.print("Deposit or Withdrawal D/W ?:") ;
DepORWith = keyboard.nextLine() ;
}
}
System.out.printf("Deposit Total: %,.2d \nWithdrawal Total: %,.2d",DepTotal, WithTotal) ;
System.out.print("\n----------------------------------") ;
System.out.print("\nThank you for Using Old National") ;
System.out.print("\n----------------------------------") ;
}
}
【问题讨论】:
-
这个while条件
while (!DepORWith.equals("E") || !DepORWith.equals("e"))将总是为真(想想看)。所以不是||,而是&&。更好的是,只需使用单个.equalsIgnoreCase(...) -
你应该遵循 Java 命名约定:变量名应该用驼峰命名。
标签: java string methods while-loop