-3

I wanted to program a java app that can print as many stars as the user want. The programm will ask the user how many starts he want to print. Here is my code :

import java.util.Scanner;


public class lab {
    public static void main(String[] args){

        int StarsN;
        Scanner input;
        input = new Scanner(System.in);

        System.out.println("How many stars do you need?");
        StarsN= input.nextInt();
    }
    public static void loopz(String[] args) {
        int loopEnd = StarsN;
        int loopStart;
        for (loopStart = 0;loopStart==loopEnd;loopStart++) {
            System.out.print("*");
        }
    }
}
Morta
  • 224
  • 1
  • 14
Majed
  • 23
  • 5
  • So what's the problem? – Sotirios Delimanolis Sep 20 '13 at 03:24
  • Perhaps you should start by studying Java by reading a book or so. – Math Sep 20 '13 at 03:25
  • it does not recognize StarsN in the second method – Majed Sep 20 '13 at 03:29
  • Your problem is the scope of your variable StarsN. You created StarsN in your main method; that means only your main method knows about StarsN. If you want loopz to know about the information in StarsN, you need to either pass it as an argument to the method, or make StarsN a class variable (declare it outside of a function, but inside of the curly braces for the lab class. – Ryan Sep 20 '13 at 04:05

4 Answers4

2

First thing to note.. I don't know why you are sending your loopz method a String[].. Here is what i would do differently in the loopz method:

public static void loopz(int numOfStars)
{
   for(int i = 0; i < numOfStars; i++)
      System.out.print("*");
}

Also call loopz in main and send it the parameter.

user2494817
  • 697
  • 4
  • 12
2

your for loop : loopStart = 0 then it says is loopStart == loopEnd , and it won't enter in the loop because loopStart don't equals loopEnd so you should change "==" in your loop to "<" .

Alien
  • 15,141
  • 6
  • 37
  • 57
erfan
  • 21
  • 5
1

Change for (loopStart = 0;loopStart==loopEnd;loopStart++) to for (loopStart = 0;loopStart < loopEnd;loopStart++).

And don't forget to call loopz() from main():

public static void main(String[] args){
    Scanner input = null;
    try {
        input=new Scanner(System.in);

        System.out.println("How many starts do you need ?");
        int StarsN= input.nextInt();
        loopz(StarsN);  //Add this
    } finally {
        if( input != null )
            input.close();
    }
}
public static void loopz(int numStars) { //You don't need the String[] args here since you never use it
    for (int loopStart = 0; loopStart < numStars;loopStart++) {
        System.out.print("*") ;
    }

}
musical_coder
  • 3,886
  • 3
  • 15
  • 18
1

Here is the answer:

import java.util.Scanner;


public class lab {
    public static void main(String[] args){

        int StarsN;
        Scanner input;
        input=new Scanner(System.in);

        System.out.println("How many starts do you need ?");
        StarsN= input.nextInt();

        int loopEnd = StarsN;
        int loopStart;
        for (loopStart = 0;loopStart<loopEnd;loopStart++) {
            System.out.print("*") ;
        }

    }

}

I really would like to teach you how to fish, instead of just giving you the fish, but I think that you need too much theory before this. Try to find some book or a good and complete tutorial to follow, I'm sorry but I don't know neither of both to say to you.

Math
  • 3,334
  • 4
  • 36
  • 51
  • 1
    ^^^^Thanks sir . well I study cs but I believe that we progressing so slow , so I decided to study something on my own – Majed Sep 20 '13 at 03:45
  • 1
    Ok. Make sure to keep studying and make the exercises the book proposes to you =) BTW, it is not a good idea to ask here at Stack Overflow what book to follow, but I'm sure Google is plenty of forum that can lead you to a good choice. – Math Sep 20 '13 at 03:48
  • @Majed maybe you should start by reading a [documentaton](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html) and start programming by tutorials in youtube. :) – Morta Jul 16 '18 at 08:41