Matthew Swart
Matthew Swart

Reputation: 41

for loop * line java

I need to ask for a number to be input and the output will be a line of * to the value of the input num. I am going wrong somewhere, I have asked for the input and made it int j but when I do my for loop I am getting errors.

Please know I am not looking for the direct answer but if someone could please point me in the right direction that would be awesome. Thanks in advance.

import java.util.Scanner;

public class Question48 
{
    public static void main(String[] args) 
    {
        //Declaring and naming the scanner
        Scanner input = new Scanner(System.in);

        //Declaring the variables needed for the class
        int j;

        //Prompt
        System.out.print("Enter a number here between 1 & 10: ");
        j = input.nextInt();

        if(int k = 0; k <= j; k++)
        {
            System.out.println("* ");
        }
    }
}

Upvotes: 0

Views: 100

Answers (2)

SoulRayder
SoulRayder

Reputation: 5166

 if(int k = 0; k <= j; k++)
    {
        System.out.println("* ");
    }

should be

for(int k = 0; k <= j; k++)
    {
        System.out.println("* ");
    }

The code you used wouldn't even run since if syntax is violated.

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201439

If I understand you, change this

if(int k = 0; k <= j; k++)
{
  System.out.println("* ");
}

to

for (int k = 0; k < j; k++)
{
  System.out.print("*");
}
System.out.println();

Upvotes: 3

Related Questions