简体   繁体   English

尝试将 if 语句放入 for 循环

[英]Trying to put an if-statement in a for loop

I am trying to input an if-statement inside of this for-loop so that whenever the user still has to eat the loop can be redone without having to lose the data already provided.我试图在这个 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);
                               }

it seems like you have some real syntax & semantic problems.看起来你有一些真正的语法和语义问题。 For example your for-loop condition is calorieCount <= calorieCount.例如,您的 for 循环条件是 calorieCount <= calorieCount。 Both sides are equal.双方是平等的。 Overall its hard to read your Code because its not formatted correctly.总的来说很难阅读你的代码,因为它的格式不正确。 Its hard to help right now - use a formatting Tool - Check Syntax and use a Debugger.现在很难提供帮助 - 使用格式化工具 - 检查语法并使用调试器。 Then clarify your question.然后澄清你的问题。 Naming of your variables could also be improved.变量的命名也可以改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM