2017-07-11 2 views
1

Je suis en train de créer un programme simple de calcul sur Java comme un simple premier projet que j'apprends Java.en boucle continue se termine sans ouvrir la instruction if

Problème: Après avoir exécuté le programme et après avoir fait mes calculs, j'ai voulu donner à l'utilisateur la possibilité de terminer le programme ou d'effectuer d'autres calculs. pour une raison quelconque, le programme n'utilise pas les instructions (if) que j'ai placées dedans, il semble l'ignorer et terminer mon programme sans permettre à l'utilisateur d'entrer son choix à la fin.

Je suis vraiment désolé si ma question n'est pas claire, je ne pouvais pas trouver une solution à mon problème en ligne, et je m'excuse si mon code semble vraiment désordonné.

import java.util.Scanner; 

public class Calculator { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 
     double fnum, snum, answer; 
     String choice, name, in; 

     System.out.println("Hello, whats your name?"); 
     name = input.nextLine(); 
     System.out.println(""); 
     System.out.println("Oh so your name is " + name + "!"); 
     System.out.println(""); 
     System.out.println("This program is a calculator that will do simple calculation of two numbers"); 
     System.out.println("There are four different options to choose from:"); 

     boolean running = true; 

     CAL: 
      while (running) { 
       System.out.println("type a for Addition, b for subtraction, c for multiplication and d for division"); 
       System.out.println("Then press enter!"); 

       choice = input.nextLine(); 

       while (!choice.equals("a") && 
         !choice.equals("A") && 
         !choice.equals("b") && 
         !choice.equals("B") && 
         !choice.equals("c") && 
         !choice.equals("C") && 
         !choice.equals("d") && 
         !choice.equals("D")) { 
        System.out.println("Wrong choice, Please try again"); 
        System.out.println("type a for Addition, b for subtraction, c for multiplication and d for division"); 

        choice = input.nextLine(); 
       } 

       if (choice.equals("a") || choice.equals("A")) { 

        System.out.println(name +" You have chosen Addition"); 
        System.out.println("Type the first number:"); 
        fnum = input.nextDouble(); 
        System.out.println("Type the second number:"); 
        snum = input.nextDouble(); 

        System.out.println("your answer is:"); 
        Addition addy = new Addition(fnum,snum); 
        System.out.println(addy.getans()); 

       } 

       if (choice.equals("b") || choice.equals("B")) { 

        System.out.println(name +" You have chosen Subtraction"); 
        System.out.println("Type the first number:"); 
        fnum = input.nextDouble(); 
        System.out.println("Type the second number:"); 
        snum = input.nextDouble(); 
        answer = fnum - snum; 
        System.out.println("your answer is:" 
          + answer); 

       } 

       if (choice.equals("c") || choice.equals("C")) { 

        System.out.println(name +" You have chosen Multiplication"); 
        System.out.println("Type the first number:"); 
        fnum = input.nextDouble(); 
        System.out.println("Type the second number:"); 
        snum = input.nextDouble(); 
        answer = fnum * snum; 
        System.out.println("your answer is:" 
          + answer); 

       } 

       if (choice.equals("d") || choice.equals("D")) { 

        System.out.println(name +" You have chosen Addition"); 
        System.out.println("Type the first number:"); 
        fnum = input.nextDouble(); 

        while (fnum == 0) { 
         System.out.println("invalid try again!"); 
         fnum = input.nextDouble(); 
        } 

        System.out.println("Type the second number:"); 
        snum = input.nextDouble(); 
        answer = fnum/snum; 

        System.out.println("your answer is:" 
          + answer);    
       } 

       System.out.println(""); 
       System.out.println("Thank you " + name + " for using this simple calculator :)"); 

       System.out.println("If you would like to try again press a the press Enter, if you wish to exit press any botton and then press enter"); 

       in = input.nextLine(); 

       if (in.equals("a")) { 
        System.out.println(""); 
        System.out.println("Thank you, please choose again"); 
        System.out.println(""); 
       } else { 
        System.out.println("Thank you and goodbye"); 
        break; 
       } 
      } 
    }  
} 

Répondre

0

C'est un problème simple causé par nextInt/Double/etc comportement.

Ces méthodes ne lisent pas le caractère de nouvelle ligne suivant de sorte que la prochaine ligne suivante renvoie toujours une chaîne vide (le reste de la ligne en cours).

Essayez de changer les (par exemple le cas d'addition) avec Double.parseDouble(input.nextLine());

if (choice.equals("a") || choice.equals("A")) { 
     System.out.println(name + " You have chosen Addition"); 
     System.out.println("Type the first number:"); 
     fnum = Double.parseDouble(input.nextLine()); 
     System.out.println("Type the second number:"); 
     snum = Double.parseDouble(input.nextLine()); 
     System.out.println("your answer is:"); 
     Addition addy = new Addition(fnum, snum); 
     System.out.println(addy.getans()); 
    } 

vous pouvez remarquer que la dernière mise au point in = input.nextLine(); appelé à demander l'entrée utilisateur sera toujours une chaîne vide.

+0

Merci, Cela a résolu mon problème. –