英文:
JAVA While Loop not recignizing String.eqauls() in Condition
问题
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("---ERROR ---\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("---ERROR ---\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("---ERROR ---\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("---ERROR ---\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("---ERROR ---\nINPUT D OR W OR E \n");
System.out.print("Deposit or Withdrawal D/W ?:");
DepORWith = keyboard.nextLine();
}
}
System.out.printf("Deposit Total: %,.2f \nWithdrawal Total: %,.2f", DepTotal, WithTotal);
System.out.print("\n----------------------------------");
System.out.print("\nThank you for Using Old National");
System.out.print("\n----------------------------------");
}
}
英文:
OK! I am trying to get the program to recognize the letter "E" to exit the while loop. Print my ending statements and then the program is done! Over! That's it! To test this I am just trying to exit as soon as the prompt asks me to put a letter, but no matter what it wont recognize that or any letter I switch it to (that's not D or W)
I have tried
- toUpperCase(), both after input and with the nextLine() method
- while (!DepORWith.equals("E") || !DepORWith.equals("e"))
- while (!DepORWith.equals("E") && !DepORWith.equals("e"))
- Switching the cases ever which way
- Trying to change it all to char types (BIG mistake)
- putting a letter as a place holder and then it'll just change
- Using .equalsIgnoreCase() with these other 'solutions'
- Using .isEmpty()
I've tried so much I don't remember. Some of these 'solution' ended up looping the questions forever no matter my input. I'll press E, get my own error, press E again and it loops my error. God I need help please.
I just need to press E , Print the not loop statements, and then it exits the program.
I'm putting the WHOLE Code down for full analysis please.
I am not advanced with Java this is for an Intro to Java Class, I really just know the basics
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
得分: 0
!DepORWith.equals("E") || !DepORWith.equals("e")
将始终返回 true。如果 DepORWith
是 "E"
,它不是 "e"
,反之亦然。从逻辑上讲,您需要一个 &&
条件:
while (!DepORWith.equals("E") && !DepORWith.equals("e")) {
尽管在这种情况下,您可以稍微优化代码并使用 equalsIgnoreCase
:
while (!DepORWith.equalsIgnoreCase("E")) {
英文:
!DepORWith.equals("E") || !DepORWith.equals("e")
will always return true. If DepORWith
is "E"
it's not e
and vise-versa. Logically, you need an `&& condition:
while (!DepORWith.equals("E") && !DepORWith.equals("e")) {
Although in this case, you could clean up the code a bit and use equalsIgnoreCase
:
while (!DepORWith.equalsIgnoreCase("E")) {
答案2
得分: 0
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) {
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) {
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"));
}
}
Please let me know if you have any further questions or if there's anything else I can assist you with.
英文:
- Do the required processing when the input is not
E
ore
- You better use
do...while
to avoid usingDepORWith = keyboard.nextLine();
twice.
Do it as follows:
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"));
}
}
A sample run:
Deposit or Withdrawal D/W ? (Press E to Exit):e
Goodbye!
Another sample run:
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!
Another sample run:
Deposit or Withdrawal D/W ? (Press E to Exit):d
Input amount for deposits:
Another sample run:
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:
Also, change the variable names in the code to follow Java naming conventions e.g. double Deposit
should be double deposit
.
Feel free to comment in case of any doubt/issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论