【问题标题】:JAVA While Loop not recignizing String.eqauls() in ConditionJAVA While Loop 无法识别条件中的 String.equals()
【发布时间】: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"))总是为真(想想看)。所以不是||,而是&amp;&amp;。更好的是,只需使用单个 .equalsIgnoreCase(...)
  • 你应该遵循 Java 命名约定:变量名应该用驼峰命名。

标签: java string methods while-loop


【解决方案1】:

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

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

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

while (!DepORWith.equalsIgnoreCase("E")) {

【讨论】:

    【解决方案2】:
    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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 2020-08-02
      • 2021-12-01
      • 2017-07-21
      • 2014-06-03
      • 2020-10-30
      • 1970-01-01
      相关资源
      最近更新 更多