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