Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java do while循环在满足条件后保持循环_Java_Loops_Do While - Fatal编程技术网

java do while循环在满足条件后保持循环

java do while循环在满足条件后保持循环,java,loops,do-while,Java,Loops,Do While,我是一名新的java程序员,我正在编写一个程序,为3台打印机设置3个型号。如果用户输入了错误的值,我希望它继续向用户询问型号。我让它工作,但只有当用户的第一个值是3台打印机之一的数字时。如果第一个值不是一个可能的值,而第二个输入是,它仍然会重复循环 package printing; import java.util.Scanner; public class newClass { public static void main(String[] args) {

我是一名新的java程序员,我正在编写一个程序,为3台打印机设置3个型号。如果用户输入了错误的值,我希望它继续向用户询问型号。我让它工作,但只有当用户的第一个值是3台打印机之一的数字时。如果第一个值不是一个可能的值,而第二个输入是,它仍然会重复循环

package printing;

import java.util.Scanner;

public class newClass {

    public static void main(String[] args) {

        int count = 0;

        String machine1 = "546";
        String machine2 = "892";
        String machine3 = "127";


        Scanner s = new Scanner(System.in);

        System.out.print("Model Number:");
        String modelNumber = s.nextLine();
        // increment count if first input value is wrong
        if (!s.equals(machine1) || !s.equals(machine2) || !s.equals(machine3))
            count++;

        // if user inputs right value
        while (true) {
            if (modelNumber.equals(machine1)) {
                System.out.println("Machine 1 is online");
                break;
            }
            if (modelNumber.equals(machine2)) {
                System.out.println("Machine 2 is online");  
                break;
            }
            if (modelNumber.equals(machine3)) {
                System.out.println("Machine 3 is online");
                break;
            }

            // keep looping if user does not input values for machine1, machine2 or machine3
            do {
                System.out.println("Try again");
                System.out.print("Model Number:");
                String modelNumberFalse = s.nextLine();
                /* each time user gets value wrong the count variable goes up by 1 and
                   the loop breaks when count reaches 3 */
                count++;
                if (count == 3)
                    break;
            } while (!s.equals(machine1) || (!s.equals(machine2)) || (!s.equals(machine3)) && (count < 2));

        }
    }
}
包装印刷;
导入java.util.Scanner;
公共类新类{
公共静态void main(字符串[]args){
整数计数=0;
字符串机1=“546”;
字符串机2=“892”;
字符串机3=“127”;
扫描仪s=新的扫描仪(System.in);
系统输出打印(“型号:”);
字符串modelNumber=s.nextLine();
//第一个输入值错误时的增量计数
如果(!s.equals(machine1)| |!s.equals(machine2)| |!s.equals(machine3))
计数++;
//如果用户输入正确的值
while(true){
if(型号等于(机器1)){
System.out.println(“机器1在线”);
打破
}
if(modelNumber.equals(machine2)){
System.out.println(“机器2在线”);
打破
}
if(型号等于(机器3)){
System.out.println(“机器3在线”);
打破
}
//如果用户未输入machine1、machine2或machine3的值,请保持循环
做{
System.out.println(“重试”);
系统输出打印(“型号:”);
字符串modelNumberFalse=s.nextLine();
/*每次用户获得错误的值时,count变量都会增加1和
当计数达到3时,循环中断*/
计数++;
如果(计数=3)
打破
}而(!s.equals(machine1)| |(!s.equals(machine2))| |(!s.equals(machine3))&&&(count<2));
}
}
}

此外,每次用户输入错误的值时,我希望count变量递增,直到达到3,do while循环中断,但在我输入错误的值超过3次后,它会不断询问型号

有几个问题。这一行是错误的:

while(!s.equals(machine1) || (!s.equals(machine2)) || (!s.equals(machine3)) && (count < 2));
除非modelNumber、machine1、machine2和machine3都是相同的值,否则该值不能为false

另外,测试计数会把这搞得一团糟,而且是多余的,因为您正在测试它并在循环中中断

package printing;

import java.util.Scanner;

public class newClass {

    public static void main(String[] args) {

        int count = 0;

        String machine1 = "546";
        String machine2 = "892";
        String machine3 = "127";


        Scanner s = new Scanner(System.in);

        System.out.print("Model Number:");
        String modelNumber = s.nextLine();
        // increment count if first input value is wrong
        if (!s.equals(machine1) || !s.equals(machine2) || !s.equals(machine3))
            count++;

        // if user inputs right value
        while (true) {
            if (modelNumber.equals(machine1)) {
                System.out.println("Machine 1 is online");
                break;
            }
            if (modelNumber.equals(machine2)) {
                System.out.println("Machine 2 is online");  
                break;
            }
            if (modelNumber.equals(machine3)) {
                System.out.println("Machine 3 is online");
                break;
            }

            // keep looping if user does not input values for machine1, machine2 or machine3
            do {
                System.out.println("Try again");
                System.out.print("Model Number:");
                String modelNumberFalse = s.nextLine();
                /* each time user gets value wrong the count variable goes up by 1 and
                   the loop breaks when count reaches 3 */
                count++;
                if (count == 3)
                    break;
            } while (!s.equals(machine1) || (!s.equals(machine2)) || (!s.equals(machine3)) && (count < 2));

        }
    }
}
应该是

while(!modelNumber.equals(machine1) 
    && (!modelNumber.equals(machine2)) 
    && (!modelNumber.equals(machine3)));
看。应用这条规则会使

while(!(modelNumber.equals(machine1)
    || modelNumber.equals(machine2)
    || modelNumber.equals(machine3)))
这可能更容易阅读


此外,如果您将“return”替换为“break”;并对do while条件进行更改,则它会起作用。所以还有别的事情发生。在内部do while中调用break会导致控件返回到外部while循环的顶部。添加一个在中断之前设置并在外部while循环中测试的布尔标志将是解决此问题的一种方法。或者直接使用return。

有几个问题。这一行是错误的:

while(!s.equals(machine1) || (!s.equals(machine2)) || (!s.equals(machine3)) && (count < 2));
import java.util.Scanner;

public class newClass
{

    public static void main(String[] args)
    {
        int count = 0; 
        String machine1 = "546";
        String machine2 = "892";
        String machine3 = "127";

        Scanner s = new Scanner(System.in);

        while (true)
        {
            System.out.print("Model Number:");
            String modelNumber = s.nextLine();
            // increment count if first input value is wrong
            if ((!modelNumber.equals(machine1)) || (!modelNumber.equals(machine2)) || (!modelNumber.equals(machine3)))
                count++;

            if (count == 3)
            {
                System.out.println("You have utilized your maximum number of try's");
                break;
            }

            if (modelNumber.equals(machine1))
            {
                System.out.println("Machine 1 is online");
                break;
            }
            if (modelNumber.equals(machine2))
            {
                System.out.println("Machine 2 is online");
                break;
            }
            if (modelNumber.equals(machine3))
            {
                System.out.println("Machine 3 is online");
                break;
            }

            System.out.println("Try again");

        }
    }
}
除非modelNumber、machine1、machine2和machine3都是相同的值,否则该值不能为false

另外,测试计数会把这搞得一团糟,而且是多余的,因为您正在测试它并在循环中中断

package printing;

import java.util.Scanner;

public class newClass {

    public static void main(String[] args) {

        int count = 0;

        String machine1 = "546";
        String machine2 = "892";
        String machine3 = "127";


        Scanner s = new Scanner(System.in);

        System.out.print("Model Number:");
        String modelNumber = s.nextLine();
        // increment count if first input value is wrong
        if (!s.equals(machine1) || !s.equals(machine2) || !s.equals(machine3))
            count++;

        // if user inputs right value
        while (true) {
            if (modelNumber.equals(machine1)) {
                System.out.println("Machine 1 is online");
                break;
            }
            if (modelNumber.equals(machine2)) {
                System.out.println("Machine 2 is online");  
                break;
            }
            if (modelNumber.equals(machine3)) {
                System.out.println("Machine 3 is online");
                break;
            }

            // keep looping if user does not input values for machine1, machine2 or machine3
            do {
                System.out.println("Try again");
                System.out.print("Model Number:");
                String modelNumberFalse = s.nextLine();
                /* each time user gets value wrong the count variable goes up by 1 and
                   the loop breaks when count reaches 3 */
                count++;
                if (count == 3)
                    break;
            } while (!s.equals(machine1) || (!s.equals(machine2)) || (!s.equals(machine3)) && (count < 2));

        }
    }
}
应该是

while(!modelNumber.equals(machine1) 
    && (!modelNumber.equals(machine2)) 
    && (!modelNumber.equals(machine3)));
看。应用这条规则会使

while(!(modelNumber.equals(machine1)
    || modelNumber.equals(machine2)
    || modelNumber.equals(machine3)))
这可能更容易阅读

此外,如果您将“return”替换为“break”;并对do while条件进行更改,则它会起作用。所以还有别的事情发生。在内部do while中调用break会导致控件返回到外部while循环的顶部。添加一个在中断之前设置并在外部while循环中测试的布尔标志将是解决此问题的一种方法。或者直接使用return

import java.util.Scanner;

public class newClass
{

    public static void main(String[] args)
    {
        int count = 0; 
        String machine1 = "546";
        String machine2 = "892";
        String machine3 = "127";

        Scanner s = new Scanner(System.in);

        while (true)
        {
            System.out.print("Model Number:");
            String modelNumber = s.nextLine();
            // increment count if first input value is wrong
            if ((!modelNumber.equals(machine1)) || (!modelNumber.equals(machine2)) || (!modelNumber.equals(machine3)))
                count++;

            if (count == 3)
            {
                System.out.println("You have utilized your maximum number of try's");
                break;
            }

            if (modelNumber.equals(machine1))
            {
                System.out.println("Machine 1 is online");
                break;
            }
            if (modelNumber.equals(machine2))
            {
                System.out.println("Machine 2 is online");
                break;
            }
            if (modelNumber.equals(machine3))
            {
                System.out.println("Machine 3 is online");
                break;
            }

            System.out.println("Try again");

        }
    }
}
希望这能解决你的问题

希望这能解决您的问题

  • 第一:不使用或不需要时使用
  • 第二:你在重复 这次考试没有什么好理由
在代码中,您最终会重复相同的测试。这意味着,当您添加机器时,您必须在多个位置更新代码

软件的第一条规则是不要重复你自己。当下一个家伙被要求去碰运气时,他/她会找到第一个代码块并编辑它,可能永远不会注意到重复的代码块。复制粘贴的代码是未来许多bug的根源

您可以简化代码,使每个检查只进行一次,如下所示:

import java.util.Scanner;

public class newClass {

    public static void main(String[] args) {

        int count = 0;

        // for extra credit, try to make this an ArrayList
        // so you can keep adding models as needed
        // then you would adjust your tests to leverage the ArrayList
        // search functions
        String machine1 = "546";
        String machine2 = "892";
        String machine3 = "127";


        Scanner s = new Scanner(System.in);

        // when using a while loop, it is good practice to use a boolean
        // as your logic expands, multiple tests in the loop may set the
        // boolean to true or false
        // it is cumbersom to have large blocks of code in your while check
        boolean keepOnTrucking = true;

        System.out.print("Enter Model Number:");
        while (keepOnTrucking) {

            String modelNumber = s.nextLine();

            // when using multiple tests, it is good
            // to give each test its own line and end the line
            // with the logical operator that joins it to the next
            // it makes it easier to read
            // and easier to edit (add or remove tests)

            // Logical operator note:
            // Your test was: not A OR not B OR not C
            // This would NEVER work, as A != B != C
            // If a user entered machine2, the check would
            // fail for !(machine1), OR !(machine2) OR !(machine3)
            // because while (!s.equals(machine2)) would say false
            // (!s.equals(machine1)) would say true, and the OR
            // chain would stop and count it as an error.
            // Instead you want:
            // !(machine1) && !(machine2) && !(machine3)
            // Thus to to error, it has to not any of the machines.
            // If it is true for all three nots, then you have an error
            if (!machine1.equals(modelNumber) && 
                !machine2.equals(modelNumber) &&
                !machine3.equals(modelNumber)) {

                // you only increment on failure
                count++;

                // nice developers give meaningful feed back to users
                if (count>=3) {
                    System.out.print("Out of guesses! Go Away!"); // even when it is mean

                    // since you are nested in one while loop,
                    // this will break you out
                    break;
                } else {
                    System.out.print("Not a valid model number, please re-enter:");
                }
            } else {

                // the found a machine, so exit the while loop
                keepOnTrucking = false;

                if (machine1.equals(modelNumber)) {
                    System.out.println("Machine 1 is online");
                } else if (machine1.equals(modelNumber)) {
                    System.out.println("Machine 2 is online");
                } else { // since this ins the only one left, you don't need an if clause
                    System.out.println("Machine 3 is online");
                }
            }

        }
    }
}
  • 第一:不使用或不需要时使用
  • 第二:你在重复 这次考试没有什么好理由
在代码中,您最终会重复相同的测试。这意味着,当您添加机器时,您必须在多个位置更新代码

软件的第一条规则是不要重复你自己。当下一个家伙被要求去碰运气时,他/她会找到第一个代码块并编辑它,可能永远不会注意到重复的代码块。复制粘贴的代码是未来许多bug的根源

您可以简化代码,使每个检查只进行一次,如下所示:

import java.util.Scanner;

public class newClass {

    public static void main(String[] args) {

        int count = 0;

        // for extra credit, try to make this an ArrayList
        // so you can keep adding models as needed
        // then you would adjust your tests to leverage the ArrayList
        // search functions
        String machine1 = "546";
        String machine2 = "892";
        String machine3 = "127";


        Scanner s = new Scanner(System.in);

        // when using a while loop, it is good practice to use a boolean
        // as your logic expands, multiple tests in the loop may set the
        // boolean to true or false
        // it is cumbersom to have large blocks of code in your while check
        boolean keepOnTrucking = true;

        System.out.print("Enter Model Number:");
        while (keepOnTrucking) {

            String modelNumber = s.nextLine();

            // when using multiple tests, it is good
            // to give each test its own line and end the line
            // with the logical operator that joins it to the next
            // it makes it easier to read
            // and easier to edit (add or remove tests)

            // Logical operator note:
            // Your test was: not A OR not B OR not C
            // This would NEVER work, as A != B != C
            // If a user entered machine2, the check would
            // fail for !(machine1), OR !(machine2) OR !(machine3)
            // because while (!s.equals(machine2)) would say false
            // (!s.equals(machine1)) would say true, and the OR
            // chain would stop and count it as an error.
            // Instead you want:
            // !(machine1) && !(machine2) && !(machine3)
            // Thus to to error, it has to not any of the machines.
            // If it is true for all three nots, then you have an error
            if (!machine1.equals(modelNumber) && 
                !machine2.equals(modelNumber) &&
                !machine3.equals(modelNumber)) {

                // you only increment on failure
                count++;

                // nice developers give meaningful feed back to users
                if (count>=3) {
                    System.out.print("Out of guesses! Go Away!"); // even when it is mean

                    // since you are nested in one while loop,
                    // this will break you out
                    break;
                } else {
                    System.out.print("Not a valid model number, please re-enter:");
                }
            } else {

                // the found a machine, so exit the while loop
                keepOnTrucking = false;

                if (machine1.equals(modelNumber)) {
                    System.out.println("Machine 1 is online");
                } else if (machine1.equals(modelNumber)) {
                    System.out.println("Machine 2 is online");
                } else { // since this ins the only one left, you don't need an if clause
                    System.out.println("Machine 3 is online");
                }
            }

        }
    }
}

如何在一个while循环中编写它?thankshow我能把它写在一个while循环中吗?谢天谢地或者更好的办法是,删除否定词并使用,或者按照de morgan lawsWell,您可以将您的固定代码减少为:“应该是:
while(true);
”。这是一样的,并且不会解决OPs问题。如果不清楚我的意思:每个比较都是错误的,并且由于否定,“结果”将是
while(true&&(true)&(true)&(true))
while(true)
。如果这是有意的,那么应该用简单的
while(true)
循环替换它。@NathanHughes如果我看到你的建议,并且看到你仍然将
扫描仪
字符串
进行比较,那么我知道每次比较都是错误的。因为你否定了每一个比较,你会得到
,而(true)