简体   繁体   English

如何仅在说是/继续时循环。 我的代码只是不断循环,不关心布尔 I 代码

[英]How to loop only when say yes/continue. My code just keep looping and don't care about boolean I code

I need to write code and in the end, the user can choose to continue or stop.我需要编写代码,最终用户可以选择继续或停止。 But after try and error for many times, I just can't find the solution.但是经过多次尝试和错误,我就是找不到解决方案。 It just keep looping.它只是不断循环。

import java.io.*;

public class medicine {
  public static void main(String args[]) throws IOException {

    boolean choice;

    do {

      int age;
      String answer = null;
      BufferedReader n = new BufferedReader(new InputStreamReader(System.in));

      System.out.println("Enter Your Name: ");
      String name = n.readLine();

      System.out.println("Enter Your Age: ");
      age = Integer.parseInt(n.readLine());
      System.out.println("Your recomended paracetamol dosage: ");

      if (age < 1) {
        System.out.println("60 - 120 mg");
      } else if ((age >= 1) && (age <= 5)) {
        System.out.println("120 - 240 mg");
      } else if ((age >= 6) && (age <= 12)) {
        System.out.println("240 - 480 mg");
      } else {
        System.out.println("INVALID AGE, PLEASE REFER TO PHARMACY OR DOCTOR NEAR YOU... THANK YOU");
      }

      System.out.println("Do You Want to Continue?");

    } while (choice = true);
  }
}

This is the output.这是输出。 Like you can see, it just looping and don't seem to take any input for yes or no.如您所见,它只是循环,似乎没有输入是或否。

Enter Your Name: 
Nik
Enter Your Age: 
12
Your recomended paracetamol dosage: 
240 - 480 mg
Do You Want to Continue?
Enter Your Name: 
Sarah
Enter Your Age: 
10
Your recomended paracetamol dosage: 
240 - 480 mg
Do You Want to Continue?
Enter Your Name:

Try to read input from user after your question "do you want to continue" , something like:尝试在您的问题“您想继续吗”之后读取用户的输入,例如:

String choice= n.next();
boolean continue= choice.equalsIngoreCase("yes") || choice.equalsIngoreCase("yes")
and change the condition to while (continue);
while (choice=true)

= is an assignment, not a comparison. =是赋值,而不是比较。 You are always setting choice to true here, so the condition is always true, and the loop always continues.您总是在此处将choice设置为true ,因此条件始终为真,并且循环始终继续。

Use == instead;使用==代替; or, better, simply use:或者,更好的是,只需使用:

while (choice)

which avoids the possibility of the typo.这避免了错字的可能性。

However, you will then need to tackle the problem that you don't otherwise set the value of choice : I suppose you have just forgotten to read something from the scanner.但是,您将需要解决您没有以其他方式设置choice值的问题:我想您刚刚忘记从扫描仪中读取某些内容。

暂无
暂无

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

相关问题 当我不断单击时,为什么我的for循环不继续? - Why doesn't my for loop continue when I keep clicking? 当你不关心错误时,等待和异步 - await & async when you don't care about errors 在 JavaScript ES8 async/await 中,如何取消等待那些我不关心的? - In JavaScript ES8 async/await, how to unwait those I don't care about? 想在我的代码中使用 for 循环,但我不知道怎么可能 - A want to use the for loop in my code, but I don't know how is it possible 如果我没有分页怎么办?只要保持循环播放……为什么需要分页(页脚)? - What happens if I don't have pagination?just keep for looping the post…why need pagination, for footer? 我的代码刚刚决定停止工作,我不知道为什么。 (javascript 谷歌 appscript) - My code just decided to stop working and I don't know why. (javascript google appscript) 有时我的机器人无法继续流动。 一张卡继续循环。(直接线特定问题,而不是在 MS 团队中上升) - Some times my bot can't flow continue. one card continues looping.(directline specific problem and not rise in MS-Teams) 关于代码中的函数,我不知道它们对什么意味着什么 - about functions in code that I don't know what they mean for what 不明白为什么我的 for 循环代码不起作用 - Don't understand why my for loop code isn't working For循环仅在内部函数完成后才继续循环吗? - For loop continue looping only when a function inside is finish?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM