简体   繁体   English

JAVA-For循环和If else语句

[英]JAVA - For loop and If else statement

I have been trying to figure out what's wrong with this answer to the question. 我一直试图找出问题的答案出了什么问题。 Really need help! 真的需要帮助! Any help is appreciated! 任何帮助表示赞赏! Thanks 谢谢

Question

  • you can enter two ints(a and b), and 您可以输入两个整数(a和b),并且
  • the program will print from number a to number b if a<b . 如果a<b ,程序将从数字a打印到数字a<b
  • If a>b , the program will print from b to a. 如果a>b ,程序将从b打印到a。
  • If b is the same as a , the program will ask the user to enter another number b until it is not equal to a. 如果b is the same as a ,则程序将要求用户输入另一个数字b,直到它不等于a。

     Scanner sc = new Scanner(System.in); System.out.println("Enter a: "); int a = sc.nextInt(); System.out.println("Enter b: "); int b = sc.nextInt(); if(a > b) { for(int i = b; b >= a; b--) { System.out.println(b); } } else if (a < b) { for(int i = a; a <= b; a++) { System.out.println(i); } } else { System.out.println("Enter another number b: "); int numberb = sc.nextInt(); } 

    } }

I made a few corrections to your current attempt, which was not far off from being functional. 我对您当前的尝试做了一些更正,这与功能的实现并不遥不可及。 First, I use a loop to keep prompting the user to input a b number until a does not equal b . 首先,我使用循环不断提示用户输入b数字,直到a不等于b为止。 With distinct a and b in hand, I then do a single loop to print out the range of numbers from smallest to greatest, inclusive on both ends. 握住不同的ab ,然后执行一个循环以打印出从最小到最大(包括两端)的数字范围。

Scanner sc = new Scanner(System.in);
System.out.println("Enter a: ");
int a = sc.nextInt();
int b;
do {
    System.out.println("Enter b: ");
    b = sc.nextInt();
} while (b == a);

for (int i=Math.min(a, b); i <= Math.max(a,b); ++i) {
    System.out.println(i);
}

To allow the user to enter a b until it's different of a you can use a do while loop 要允许用户输入b直到与a不同,可以使用do while loop

Scanner sc = new Scanner(System.in);
System.out.println("Enter a: ");
int a = sc.nextInt();
int b = 0;
do {
    System.out.println("Enter b: ");
    b = sc.nextInt();
} while (a == b);

Then to print you can simply do : 然后打印即可:

for (int i=Math.min(a, b); i <= Math.max(a,b); ++i) {
    System.out.println(i);
}

Or correct your code : 或更正您的代码:

 if (a > b) {
    for (int i = b; i <= a; i++) {   // i is the index to change
        System.out.println(i);       // use i
    }
} else if (a < b) {
    for (int i = a; i <= b; i++) {   // i is the index to change
        System.out.println(i);       // use i
    }
}

Your for loop iteration is wrong. 您的for循环迭代是错误的。 For your third condition I have made few changes. 对于您的第三个条件,我进行了一些更改。 Change your code: 更改您的代码:

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        test t = new test();
        System.out.println("Enter a: ");
        int a = sc.nextInt();
        System.out.println("Enter b: ");
        int b = sc.nextInt();
        if(a==b) {
            do {
                System.out.println("Both are same enter again");
                b = sc.nextInt();
            }while(a==b);
            t.loop(a, b);
        }else {
            t.loop(a,b);
        }
    }
    void loop(int a, int b) {
        if(a > b) {
            for(int i = b; i <= a; i++) {
                System.out.println(i);
            }
        } else if (a < b) {
            for(int i = a; i <= b; i++) {
                System.out.println(i);
            }
        }
    }
}

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

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