尝试将if语句放入for循环中

时间:2020-02-29 04:02:01

标签: java for-loop if-statement

我正在尝试在此for循环中输入一个if语句,以便每当用户仍然需要食用该循环时,都可以重做而不必丢失已经提供的数据。

boolean yes = true;
                       boolean no = false;
                       double calorieCount = DCN;
                       int snackCalories = 300;
                       int mealCalories = 1100;
                       double meal = mealCalories;
                       double snack = snackCalories;
    for (int i = 1; calorieCount <= calorieCount; calorieCount--) {
                           System.out.println("Did you eat? yes or no");
                           String answer = sc.next();

                        if(yes == true) { //You did eat 
                               System.out.println("Did you eat a meal(1) or a snack(2)?");
                               int foodChoice = sc.nextInt();
                               if(foodChoice == 1) //Meal 
                               System.out.println("Your calories left for the day : " + (calorieCount - meal));
                                }
                               int foodChoice = sc.nextInt();
                                if(foodChoice == 2) {//Snack                  
                               calorieCount = calorieCount - snack;
                               System.out.println("Your calories left for the day : " + (calorieCount - snack));
                               }
                               else {
                               System.out.println("Your calories left for the day : " + calorieCount);
                               };
                               else(no == false){
                                   System.out.println(calorieCount);
                               }

1 个答案:

答案 0 :(得分:0)

您似乎有一些真正的语法和语义问题。例如,您的循环条件是卡路里计数<=卡路里计数。双方平等。总体而言,由于其格式不正确,很难阅读您的代码。现在很难提供帮助-使用格式化工具-检查语法并使用调试器。然后澄清您的问题。您的变量的命名也可以得到改善。