Java number sequence loop

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

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++; } 
0

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+" "); } } } 
0

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

3

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like