0

So hi i'm getting infinite loop problem i don't know whats wrong with my code i'm trying to make a number sequence format is at the bottom i think the problems are in my condition?

import java.util.Scanner;
public class tester {
    public static void main(String[] args) {
        Scanner x = new Scanner(System.in);


        int n;         

        System.out.print("Enter how many numbers to display");
        n = x.nextInt();


        while(n!=0) {                     //is this right?
            for ( int i = 0; i<=n; i++) {
                if(i%2==0) {
                    n += 2;
                    System.out.print(n);

                } else {
                    n += 3;
                    System.out.print(n);
                }  
            }

        }
    }
}   

Outputs i'm trying to get

Enter how many numbers to display : 5
1 3 6 8 11

2. 
Enter how many numbers to display : 16
1 3 6 8 11 13 16 18 21 23 26 28 31 33 36 38  //but im getting infinite loops

 // the sequence pattern is +2 then +3
0

5 Answers 5

3

The problem is here: while(n!=0) and here: for ( int i = 0; i<=n; i++). For the while loop, will keep on going until n is equal to 0. For the for loop, this will most likely keep on going for ever.

Your code has two problems:

  1. If you provide a non negative value, this will keep on going for ever (since you are always only incrementing n).
  2. Even if you do supply a negative number, n would n need to become exactly 0 to stop.

Depending on what you need to do, you will need to change the condition. Judging by the output, n would need to be positive and thus you would need to stipulate some upper range for n in which the while loop would stop.

EDIT: You only need to have 1 loop to do what you are after. Also, n denotes the amount of elements, thus it needs to stay fixed throughout the execution of the program. In your case, you where increasing it all the time.

    Scanner x = new Scanner(System.in);
    int n;
    System.out.print("Enter how many numbers to display");
    n = x.nextInt();

    int count = 0;
    int i = 1;
    while (count < n) {                     //is this right?            
        if (count % 2 == 0) {
            System.out.print(i + " ");
            i += 2;
        } else {
            System.out.print(i + " ");
            i += 3;                
        }            
        count++;
    }
Sign up to request clarification or add additional context in comments.

Comments

2

Two problems:

int stop = n; // declare one local var to stop the for loop 

if (n != 0) { //switch to if condition
    for (int i = 0; i <= stop; i++) {  
      //loop's exit condition wasn't met because 'n' was also being incremented
        if (i % 2 == 0) {
            n += 2;
            System.out.print(n+" ");

        } else {
            n += 3;
            System.out.print(n+" ");
        }
    }     
}

Comments

1

Use 'if' condition in place of 'while' loop

3 Comments

still gives an infinite loop i think its my condition in the if else statements?
Ya wright, you are changing the 'n' value in loop, that is also a reason for infinite loop.
in 'for' loop , 'i' is increment by one step, at the same time 'n' is increment by 2 or 3 steps. Better to use another local variable instead of changing the 'n' value.
1

You have to replace your while-loop with an if-condition like so:

import java.util.Scanner;
public class tester {
    public static void main(String[] args) {

        Scanner x = new Scanner(System.in);        
        int n;         

        System.out.print("Enter how many numbers to display");
        n = x.nextInt();
        int stop = n;

        if(n!=0) { //if statement checks if n!=0
            for ( int i = 0; i<=stop; i++) { 
                  //stop replaces n because n is incremented in your for-loop
                if(i%2==0) {
                    n += 2;
                    System.out.print(n);

                } else {
                    n += 3;
                    System.out.print(n);
                }  
            }
        }

    }
}   

2 Comments

still gives an infinite loop
did you also change the for loop? it shouldnt loop infinite
0

Based on your answers I found a solution that works:

int n;    

System.out.print("Enter how many numbers to display");
n = x.nextInt();
int k = -2; // so that it starts with 1 when i add +3
int stop = n-1;   

if(n!=0) {                     
    for ( int i = 0; i<=stop; i++) {
        if(i%2==0) {
            k += 3;
            System.out.print(k+" ");
        } else {
            k += 2;
            System.out.print(k+" ");
        }  
    }
}

2 Comments

While posting your final solution as an answer is helpful, be sure to mark one of the other answers as accepted, whichever answer helped you the most in getting you to your solution.
well then place your upvotes :D Also my answere should have gotten rid of the infinit loop...

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.