JAVA While Loop não recignizing String.eqauls () na Condição

AgWordSmith:

ESTÁ BEM! Eu estou tentando obter o programa para reconhecer a letra "E" para sair do loop while. Imprimir minhas declarações terminando e, em seguida, o programa é feito! Sobre! É isso aí! Para testar isto, estou apenas tentando sair assim que o prompt de me pede para colocar uma carta, mas não importa o que ele não vai reconhecer que ou qualquer carta que eu mude para (que não é D ou W) Tentei

  • toUpperCase (), tanto após a entrada e com a nextLine () Método
  • while (! DepORWith.equals ( "E") ||! DepORWith.equals ( "E"))
  • while (! DepORWith.equals ( "E") &&! DepORWith.equals ( "e"))
  • Alternando os casos sempre que maneira
  • Tentando mudar tudo para tipos CHAR (grande erro)
  • colocando uma carta como um local reservado e, em seguida, ele vai apenas mudar
  • Usando .equalsIgnoreCase () com estes 'soluções'
  • Usando .isEmpty ()

Eu tentei tanto que eu não me lembro. Alguns destes 'solução' acabou looping as perguntas para sempre, não importa a minha entrada. Vou pressionar E, obter o meu próprio erro, pressione E novamente e ele faz um loop meu erro. Deus, preciso de ajuda por favor.

Eu só preciso de imprensa E, imprima a não instruções de loop, e, em seguida, sai do programa. Estou colocando todo o código para baixo para análise completa por favor. Eu não sou avançado com Java isto é para uma introdução ao Java Class, eu realmente só sabe o básico

    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----------------------------------") ;

   }


}
Arvind Kumar Avinash:
  1. Fazer o processamento necessário quando a entrada não é Eoue
  2. É melhor usar do...whilepara evitar o uso DepORWith = keyboard.nextLine();duas vezes.

Fazê-lo da seguinte forma:

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"));
    }
}

Uma corrida de amostra:

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

Outra corrida de amostra:

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!

Outra corrida de amostra:

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

Outra corrida de amostra:

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:

Além disso, altere os nomes de variáveis no código a seguir Java convenções de nomenclatura por exemplo, double Depositdeve ser double deposit.

Sinta-se livre para comentar em caso de qualquer dúvida / questão.

Acho que você gosta

Origin http://10.200.1.11:23101/article/api/json?id=410435&siteId=1
Recomendado
Clasificación