猿问

创建 while 和 for 循环以查看是否大于、等于或小于 B。然后打印结果

我想使用 while 或/和 for 循环来比较 a 和 b 的值(由用户输入),然后打印哪个更大,如果其中一个等于零,则循环停止。


我试图为它编写代码但没有运气,因为我不理解 while 和 for 循环。


import java.util.Scanner;


public class Set31 {




    public static void main(String args[])

        {

            int a = 1;

            int b = 4;


            Scanner input = new Scanner(System.in);


            while (a < b && a!=0 && b!=0)

            {


                System.out.println("b is bigger than a");


                System.out.print("enter a ");

                a = input.nextInt();


                System.out.print("enter b ");

                b = input.nextInt();

            }   


            while (a > b && a!=0 && b!=0)

            {


                System.out.println("a is bigger than b");


                System.out.print("enter a ");

                a = input.nextInt();


                System.out.print("enter b ");

                b = input.nextInt();

        }

            while (a == b && a!=0 && b!=0)

            {

                System.out.println("a is equal to b");


                System.out.print("enter a ");

                a = input.nextInt();


                System.out.print("enter b ");

                b = input.nextInt();

            }



    }



}

在我输入输入数字大约 1-4 次后运行程序后循环停止。


扬帆大鱼
浏览 162回答 3
3回答

凤凰求蛊

您的代码周围没有主循环。以下所有 while 循环都应该可以解决问题。while(a!=0&nbsp;&&&nbsp;b!=0)&nbsp;{ ... }因此,如果您的输入之一为零,则不会继续循环并且例程结束。否则,您的其他三个循环之一将处理输入,请求新输入,然后由主循环重新开始。

慕桂英4014372

您可以为此使用一个do-while循环,该循环将始终至少执行一次,然后在bora等于 0 时退出:public static void main(String[] args){&nbsp; &nbsp; @SuppressWarnings("resource")&nbsp; //Used to remove unclosed warning&nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; int a;&nbsp; &nbsp; int b;&nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter value for a: ");&nbsp; &nbsp; &nbsp; &nbsp; a = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter value for b: ");&nbsp; &nbsp; &nbsp; &nbsp; b = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; if (a < b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("a is less than b");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (a > b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("a is greater than b");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (a == b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("a is equal to b");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } while (a != 0 && b != 0);&nbsp; &nbsp; System.out.println("Loop has finished!");}示例运行:Enter value for a:&nbsp;1Enter value for b:&nbsp;3a is less than bEnter value for a:&nbsp;3Enter value for b:&nbsp;5a is less than bEnter value for a:&nbsp;0Enter value for b:&nbsp;1a is less than bLoop has finished!您不需要使用while循环来检查每个条件,而是可以使用简单的ifandif else语句来检查每个外部循环的值是否更大。

慕桂英546537

除了编辑循环条件之外,您还可以使用语句来改变控制流来退出循环中break。这可以这样做:int a;int b;Scanner input = new Scanner(System.in);while(true) {&nbsp; &nbsp; System.out.println("Enter a: ");&nbsp; &nbsp; a = Integer.parseInt(input.nextLine());&nbsp; &nbsp; System.out.println("Enter b: ");&nbsp; &nbsp; b = Integer.parseInt(input.nextLine());&nbsp; &nbsp; if(a == 0 || b == 0) break;&nbsp; &nbsp; String comparison = (a > b) ? "is bigger than" : (a < b) ? "is less than" : "is equal to";&nbsp; &nbsp; System.out.println("a "+comparison+" b");}String exitReason = (a == 0 && b == 0) ? "a and b are" : (a == 0) ? "a is" : "b is";System.out.println("Exited because "+exitReason+" equal to zero");
随时随地看视频慕课网APP

相关分类

Java
我要回答