gpt4 book ai didi

JAVA While Loop 无法识别条件中的 String.equals()

转载 作者:行者123 更新时间:2023-12-02 04:20:01 24 4
gpt4 key购买 nike

好的!我试图让程序识别字母“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----------------------------------") ;

}


}

最佳答案

  1. 当输入不是Ee时做需要的处理
  2. 您最好使用 do...while 以避免使用 DepORWith = keyboard.nextLine(); 两次。

按如下方式进行:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Program {

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 = "";
do {
System.out.print("Deposit or Withdrawal D/W ? (Press E to Exit):");
DepORWith = keyboard.nextLine();
if (!(DepORWith.equalsIgnoreCase("D") || DepORWith.equalsIgnoreCase("W"))) {
if (!DepORWith.equalsIgnoreCase("E")) {
System.out.println("This is not a valid input");
} else {
System.out.println("Goodbye!");
}
} else if (!DepORWith.equalsIgnoreCase("E")) {
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();

}
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----------------------------------");
} else {
System.out.println("Goodbye!");
}
} while (!DepORWith.equalsIgnoreCase("E"));
}
}

示例运行:

Deposit or Withdrawal D/W ? (Press E to Exit):e
Goodbye!

另一个样本运行:

Deposit or Withdrawal D/W ? (Press E to Exit):x
This is not a valid input
Deposit or Withdrawal D/W ? (Press E to Exit):e
Goodbye!

另一个样本运行:

Deposit or Withdrawal D/W ? (Press E to Exit):d
Input amount for deposits:

另一个样本运行:

Deposit or Withdrawal D/W ? (Press E to Exit):x
This is not a valid input
Deposit or Withdrawal D/W ? (Press E to Exit):d
Input amount for deposits:

此外,更改代码中的变量名称以遵循Java naming conventions。例如double Deposit应该是double deposit

如有任何疑问/问题,请随时发表评论。

关于JAVA While Loop 无法识别条件中的 String.equals(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61032098/

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