0

I just start to learn Java and I don't understand why in the code above the third question goes to the default case of the second switch, but if I put this question as the second and the second as the third it works well. Any idea (maybe some variable declaration/type error?)? Thanks in advance for your help.

package fr.one.cook;

import java.util.Scanner;

public class cook1 {

   public static void main(String[] args) {

      int CUIRE_DU_BOEUF = 500;
      int CUIRE_De_LAGNEAU = 400;
      int SECONDES = 60;
      int poidsDemande = 0;
      String typeDeViande;
      String typeDeCuisson;
      float tempsDeCuisson;
      int coefDeCuisson;
      float coef;
    
      Scanner scanner = new Scanner(System.in);
    
      System.out.println("Saisissez le type de viande (Boeuf ou Agneau) :");        
      typeDeViande = scanner.nextLine();
            
      System.out.println("Saisissez le poid de la viande (en gramme) :");       
      poidsDemande = scanner.nextInt();
    
      // Third question that not work as expected
      System.out.println("Saisissez le type de cuisson (bleu, à point ou bien cuit) :");
      typeDeCuisson = scanner.nextLine();           
    
      scanner.close(); 

      switch (typeDeViande) {
        case "Boeuf":               
            switch (typeDeCuisson) 
            {                   
                case "bleu": 
                    System.out.println("bleu");
                    coef = poidsDemande/CUIRE_DU_BOEUF;
                    coefDeCuisson = 10;
                    
                    tempsDeCuisson =  coef*coefDeCuisson;
                    tempsDeCuisson = tempsDeCuisson*SECONDES;
                    
                    System.out.println("Votre temps de cuisson est de " + tempsDeCuisson + "secondes");
                    break;
                    
                case "à point": 
                    System.out.println("à point");
                    coef = poidsDemande/CUIRE_DU_BOEUF;
                    coefDeCuisson = 17;
                    
                    tempsDeCuisson =  coef*coefDeCuisson;
                    tempsDeCuisson = tempsDeCuisson*SECONDES;
                    
                    System.out.println("Votre temps de cuisson est de " + tempsDeCuisson + "secondes");
                    break;
                
                case "bien cuit": 
                    System.out.println("bien cuit");
                    coef = poidsDemande/CUIRE_DU_BOEUF;
                    coefDeCuisson = 25;
                    
                    tempsDeCuisson =  coef*coefDeCuisson;
                    tempsDeCuisson = tempsDeCuisson*SECONDES;
                    
                    System.out.println("Votre temps de cuisson est de " + tempsDeCuisson + "secondes");
                    break;
                    
                default: 
                    // Third question goes here directly (= error)
                    System.out.println("Type de cuisson invalide (Boeuf)");
                    break;
                } // boeuf
        
            break;
            
        case "Agneau":              
            switch (typeDeCuisson) {                    
                case "bleu": 
                    coef = poidsDemande/CUIRE_De_LAGNEAU;
                    coefDeCuisson = 15;                     
                    tempsDeCuisson =  coef*coefDeCuisson;
                    tempsDeCuisson = tempsDeCuisson*SECONDES;                       
                    System.out.println("Votre temps de cuisson est de " + tempsDeCuisson + "secondes");
                    break;
                    
                case "à point": 
                    coef = poidsDemande/CUIRE_De_LAGNEAU;
                    coefDeCuisson = 25;
                    
                    tempsDeCuisson =  coef*coefDeCuisson;
                    tempsDeCuisson = tempsDeCuisson*SECONDES;
                    
                    System.out.println("Votre temps de cuisson est de " + tempsDeCuisson + "secondes");
                    break;
                
                case "bien cuit" : 
                    coef = poidsDemande/CUIRE_De_LAGNEAU;
                    coefDeCuisson = 40;
                    
                    tempsDeCuisson =  coef*coefDeCuisson;
                    tempsDeCuisson = tempsDeCuisson*SECONDES;
                    
                    System.out.println("Votre temps de cuisson est de " + tempsDeCuisson + "secondes");
                    break;
                    
                default : 
                    System.out.println("Type de cuisson invalide");
                    break;
                } // Agneau
            break;
            
            default : 
                System.out.println("Type de viande invalide (Agneau)");
                break;      
    }
 }
}
user3049922
  • 157
  • 1
  • 2
  • 10
  • You should try printing `typeDeCuisson` before the swtich, and you'll see it doesn't print what you typed. The switch-case isnt the problem. But for future questions, please consider making a [mcve] – OneCricketeer May 25 '22 at 16:38
  • Put `scanner.next();` right after your `scanner.nextInt();` and see if it helps. See https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – PM 77-1 May 25 '22 at 16:39
  • @user16320675 - Yes. `nextLine`. – PM 77-1 May 25 '22 at 17:21

0 Answers0