JAVA While 循环无法识别条件中的 String.eqauls()

问题描述 投票:0回答:2

好的!我试图让程序识别字母“E”以退出 while 循环。打印我的结束语句,然后程序就完成了!超过!就是这样!为了测试这个,我只是在提示要求我输入一个字母时尝试退出,但无论如何它都不会识别该字母或我将其切换到的任何字母(不是 D 或 W) 我试过了

  • toUpperCase(),无论是在输入之后还是使用 nextLine() 方法
  • while (!DepORWith.equals("E") || !DepORWith.equals("e"))
  • while (!DepORWith.equals("E") && !DepORWith.equals("e"))
  • 以任何方式切换案例
  • 尝试将其全部更改为字符类型(大错误)
  • 将字母作为占位符,然后它就会改变
  • 将 .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----------------------------------") ;

   }


}
java string methods while-loop
2个回答
0
投票
  1. 当输入不是
    E
    e
  2. 时,进行所需的处理
  3. 您最好使用
    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 命名约定,例如

double Deposit
应该是
double deposit


0
投票

!DepORWith.equals("E") || !DepORWith.equals("e")
将始终返回 true。如果
DepORWith
"E"
,则不是
e
,反之亦然。从逻辑上讲,你需要一个 `&& 条件:

while (!DepORWith.equals("E") && !DepORWith.equals("e")) {

尽管在这种情况下,您可以稍微清理一下代码并使用

equalsIgnoreCase
:

while (!DepORWith.equalsIgnoreCase("E")) {
© www.soinside.com 2019 - 2024. All rights reserved.