0

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

    }

3 Answers 3

5

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

while(true) and break is not very 'good-practice', do/while is made for :)
@azro Refactored, thanks, and you just eliminated one line of unnecessary code. Now let's think of a way to replace the whole thing with a one-line lambda ;-)
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.