java - boolean Do-While 循环永不停止

标签 java boolean do-while

这是每个人都告诉我要做的工作产品。谢谢大家,我会努力保持 从现在开始我的代码更干净。这段代码只是练习,最终将成为我的计算机在启动时运行的锁定系统。谁愿意讨厌的青春期前 child 在电脑上乱搞?不是这个人。

import java.io.*;
import java.util.Scanner;  /
class AgeChecker
{   
    public static void main (String[] args) throws Exception  /*@Exception- thrown to allow reading
    {                                                                       of single characters*/
    char ans; //(Read from user input)
    String name;
    boolean loop = false; //To loop back after a section, add loop = true.
                          //To stop the program after a section, add loop = false.
    do
    {
    Scanner dd = new Scanner(System.in);
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  // Needed to read the 
    BufferedReader in1 = new BufferedReader(new InputStreamReader(System.in)); // character input

    System.out.println("What is your name? "); name = dd.nextLine();
        {   

        System.out.println("Are you 14 years of age or older? (y/n) "); ans = (char)in.read();


                                    //Using if, else-if and else to make sure
            if (ans == 'y')         //I have a good grasp of what i already know
            {   
                    System.out.println("Welcome, " + name + "! Are you 21 years of age or older? (y/n) ");
                    ans = (char)in1.read();

                    if (ans == 'n')
                    {
                    System.out.println("Welcome, " + name + "!");
                    loop = false;
                    }

                    else if (ans == 'y')
                    {
                    System.out.println("Welcome, " + name + "! Would you like a drink? ");
                    loop = false;
                    }
                }   
            else if (ans == 'n')
                {   
                System.out.println("We're sorry. Only those at the age of 14 or older may access this program. ");
                loop = false;
                }   
            else 
                {
                        System.out.println("Invalid input. ");
                        loop = true;
                }
            }
        }
    }
    while (loop == true); //Put here to line up with the 'do' at the top
}

感谢大家的帮助

最佳答案

替换

while (fee = true);

while (fee == true);

或更好:

while (fee);

fee = true 是一个赋值并返回分配的值 (true)。

也替换

else 
System.out.println("Invalid input. ");
fee = true;

else {
   System.out.println("Invalid input. ");
   fee = true;
}

(感谢罗希特)。

请注意,如果您的代码具有较少的空格和 block (为什么这么多?),那么(包括您自己)会更容易获取这些错误。那些code conventions可能有用。

关于java - boolean Do-While 循环永不停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14223436/

相关文章:

Java绝对文件路径没有给出所需的路径

Java FTPS 无法检索文件列表(FileZilla 客户端工作正常)

c - 如何在不使用 if 或 for 的情况下确定数字是正数、负数还是零?

Scalac 给出错误发现 : scala. Boolean(false) required : java. lang.Boolean

objective-c - Objective-C "do - while"

java - 为不可修改的列表实现迭代器

java - Hibernate Search 或 Compass

java - JAXB 将 boolean 值编码为复杂类型

c - 虽然声明不起作用 C

Java 验证基本计算器程序中的输入