好的,我会的我试图让程序识别字母“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,打印not循环语句,然后它退出程序。我把整个代码下来进行全面分析,请。我不先进的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----------------------------------") ;
}
}
字符串
2条答案
按热度按时间k4ymrczo1#
1.当输入不是
E
或e
时,执行所需的处理1.最好使用
do...while
,以避免使用DepORWith = keyboard.nextLine();
两次。按以下步骤操作:
字符串
运行示例:
型
另一个样本运行:
型
另一个样本运行:
型
另一个样本运行:
型
另外,将代码中的变量名更改为Java naming conventions,例如
double Deposit
应该是double deposit
。mcdcgff02#
!DepORWith.equals("E") || !DepORWith.equals("e")
将始终返回true。如果DepORWith
是"E"
,则不是e
,反之亦然。逻辑上,您需要一个`&&条件:字符串
虽然在这种情况下,你可以清理一下代码,使用
equalsIgnoreCase
:型