简体   繁体   中英

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
  • the program will print from number a to number b if a<b .
  • If a>b , the program will print from b to 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.

     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 . 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.

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

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 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);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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