Java loop goes 4 times for some reason

while loop java
do-while loop java
for loop java
java repeat until
while loop example
java for loop execution order
java repeat loop
while loop java multiple conditions

So I have this assignment where I need to loop user's input 6 times. the loop, after finishing, looped again for 3 more times. I didn't add a for lop before it so I don't know how to handle it.

Here's the code for the method:

public static int[] getPlayerNumbers(int[] playNums) {
    Scanner input = new Scanner(System.in);

    for (int i = 0; i < playNums.length; i++) {
        System.out.println("Please enter numbers from 1-9: " + i);
        playNums[i] = input.nextInt();

        while (playNums[i] < 1 || playNums[i] > 9) {
            System.out.println("Invlaid input. Please only enter 1-9. ");
            playNums[i] = input.nextInt();
        }
    }

    return playNums;
}

I placed i to see the index and it goes 0 to 5 then returns to 0. I ran out of ideas, please help.


it seems your playNums has more than 6. try

public static int[] getPlayerNumbers(int[] playNums) {
Scanner input = new Scanner(System.in);

for (int i = 0; i < 6; i++) {
    System.out.println("Please enter numbers from 1-9: " + i);
    playNums[i] = input.nextInt();

    while (playNums[i] < 1 || playNums[i] > 9) {
        System.out.println("Invlaid input. Please only enter 1-9. ");
        playNums[i] = input.nextInt();
    }
}

return playNums;}

Loop statements in Java, In previous articles you learned about variables and types and now it's time to speak Before we are going any further let's check simple example printing " Hello! for (int i = 1; i <= 10; i++). System.out.println(i);. } } Let's count to 10: 1 2 3 4 5 6 Java for Loop In this tutorial, we will learn how to use for loop in Java with the help of examples and we will also learn about the working of Loop in computer programming. In computer programming, loops are used to repeat a specific block of code until a certain condition is met (test expression is false ).


I've tested your code and it seems to work for me. The playsNums contains certainly more than 6 values.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] playNums ={1, 2, 3, 4, 5, 6};
        getPlayerNumbers(playNums);
        for (int playNum: playNums) {
            System.out.println(playNum);
        }
    }

    public static int[] getPlayerNumbers(int[] playNums)
    {
        Scanner input = new Scanner(System.in);

        for (int i = 0; i < playNums.length; i++) {
            System.out.println("Please enter numbers from 1-9: " + i);
            playNums[i] = input.nextInt();
            while (playNums[i] < 1 || playNums[i] > 9) {
                System.out.println("Invalid input. Please only enter 1-9. ");
                playNums[i] = input.nextInt();
            }
        }
        return playNums;
    }
}

Continue and Break in Java, Learn about breaking a loop, and repeating a loop again in Java. we learned about loops which are used to repeat a certain process some number of times. since the condition of if satisfies, break; gets executed and the loop terminates. n = 4 n = 6 n = 7 n = 8 n = 9. In the above example, notice that n = 5 is not printed � Statement 1 sets a variable before the loop starts (int i = 0). Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again, if it is false, the loop will end. Statement 3 increases a value (i++) each time the code block in the loop has been executed.


Try this

public static int[] getPlayerNumbers(int[] playNums)
{
    Scanner input = new Scanner(System.in);

    for (int i = 0; i < 6; i++)
    {
        playNums[i] = input.nextInt();
        if(playNums[i] < 1 || playNums[i] > 9){
           System.out.println("Invlaid input. Please only enter 1-9. ");
           getPlayerNumbers(playNums);
        } else{

        System.out.println("Please enter numbers from 1-9: " + i);
            playNums[i] = input.nextInt();
        }

    }
    return playNums;

Loops: while and for, Loops are a way to repeat the same code multiple times. Any expression or variable can be a loop condition, not just comparisons: the� Output: Value of x:2 Value of x:3 Value of x:4 Enhanced For loop. Java also includes another version of for loop introduced in Java 5. Enhanced for loop provides a simpler way to iterate through the elements of a collection or array.


Java For Loop, For-Each Loop, While, Do-While Loop (ULTIMATE , In this tutorial, we'll cover the four types of loops in Java: the for loop, We're also going to cover common loop exceptions and errors like infinite loops, java. lang. The basic reason for using loops is when a repetitive action needs to be taken expression that determines whether to continue looping for one more iteration� Then you can exit the loop and continue on your way. Java For Loops. We'll start with For Loops, one of the most common types of loops. The "For" part of "For Loop" seems to have lost its meaning. But you can think of it like this: "Loop FOR a set number of times." The structure of the For Loop is this:


Loops in Java (for, while, do-while), Loops in java - various types of java loops with their syntax and examples Again the next day, you do the same things in the same order and go to which is required to output a particular value of a variable 800 times. Looping in Java is defined as performing some lines of code in an 4. do-while loop� The condition is important because we do not want the loop to be running forever. As soon as this condition is false, the loop stops. In Java there are three primary types of loops:-1. for loop 2. Enhanced for loop 3. while loop 4. do-while loop. 1. For loop in Java. Java for loop consists of 3 primary factors which define the loop itself.


[PDF] Java Loops and Methods, 4. 5. 6. 7. 8. 9. 10. This is an example of a counting loop. They are called this Without doing the calculations, can we determine how many times this loop will Why is this an infinite loop? count starts at 1, then goes to 0, -1, -2, etc., never Neither of these loops has any statements inside of it, so nothing happens in them! Then the condition results in false (as 4<=3 is false) and comes out to execute the statement after the loop. In a Java for loop, initialization is executed only once irrespective of a number of times the loop is executed. The condition is checked N+1 times where N is the number of times the body is executed.