Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,157 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,782 people online right now. Registration is fast and FREE... Join Now!




Help with looping

 
Reply to this topicStart new topic

Help with looping

davedave22
post 9 Oct, 2008 - 06:31 PM
Post #1


New D.I.C Head

*
Joined: 29 Sep, 2008
Posts: 4

Okay, i'd like to thank everyone who helped me with my previous issues with java ;] But now i have a new problem! For the moment i want to have the user input a number between 100 and 500, any other number and the user will have to retry another number unless it is zero which will stop the program. So, i know that part is working but i want to be able to test it so i can implement the next part of my program.

CODE
import java.util.*;
import java.lang.Math;

class Random
{

    public static void main( String[] args)
{

        int snumber;

        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a value of n:  ");
        snumber = scan.nextInt();
            if (snumber == 0)
{
                           System.out.println("Thanks for playing, and make it a great                                         day! :)"); }
    while (100 > snumber || snumber > 500)
{
    
System.out.println("So sorry. The value must be between 100 and 500 inclusively.");
        System.out.print("Enter a value of n:  ");
        snumber = scan.nextInt();
        if (snumber == 0) break;    
        
}
    
    

}

        
    private void generate(snumber) {
        
        for (snumber; snumber < 1; snumber--)     
{
            System.out.println("test " + snumber);
}
}
}


I want to use the "for" loop so for example i typed in the number 150, the for loop will generate an output each time it goes through the loop subtracting one each time so the output would look like:
test 149
test 148
test 147
ect...
Until "snumber" equals zero. I believe i am having problems with the parameters(because i am bad at them x_X) thanks for the help guys.
User is offlineProfile CardPM

Go to the top of the page

Locke37
post 9 Oct, 2008 - 06:35 PM
Post #2


I'm not a thief...I prefer the term TREASURE HUNTER!

Group Icon
Joined: 20 Mar, 2008
Posts: 911



Thanked 30 times

Dream Kudos: 300
My Contributions


Here's a tutorial (Written by me!) on loops. for included.

Looping

It covers the basic syntax for the loops. I notice you're having some trouble with the declaration of a for loop.
User is offlineProfile CardPM

Go to the top of the page

davedave22
post 9 Oct, 2008 - 08:26 PM
Post #3


New D.I.C Head

*
Joined: 29 Sep, 2008
Posts: 4

Very nice tutorial and thanks for the reply Locke37, got me on the right track with looping. However, i think i am more stuck with using parameters. For example i just wanted to see if i could get the number to be printed out that i had input into a seperate method and nothing came out

CODE
import java.util.*;
import java.lang.Math;

class Random
{

    public static void main( String[] args)
{

        int snumber;

        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a value of n:  ");
        snumber = scan.nextInt();
            if (snumber == 0)
{
                           System.out.println("Thanks for playing, and make it a great day! :)");
            System.exit(0);
}
    while (100 > snumber || snumber > 500)
{
    
System.out.println("So sorry. The value must be between 100 and 500 inclusively.");
        System.out.print("Enter a value of n:  ");
        snumber = scan.nextInt();
        if (snumber == 0)
{
        System.out.println("Thanks for playing, and make it a great day! :)");
                System.exit(0);
}    
        
}
    
}

    public void generate(int snumber) {

    System.out.println("n is " + snumber);

}
}


I edited my code a little to fix it up towards the specifications i need, but i am confused on using parameters X_X thanks again for the help
User is offlineProfile CardPM

Go to the top of the page

Locke37
post 10 Oct, 2008 - 07:23 AM
Post #4


I'm not a thief...I prefer the term TREASURE HUNTER!

Group Icon
Joined: 20 Mar, 2008
Posts: 911



Thanked 30 times

Dream Kudos: 300
My Contributions


QUOTE(davedave22 @ 9 Oct, 2008 - 09:26 PM) *

Very nice tutorial and thanks for the reply Locke37, got me on the right track with looping. However, i think i am more stuck with using parameters. For example i just wanted to see if i could get the number to be printed out that i had input into a seperate method and nothing came out

CODE
import java.util.*;
import java.lang.Math;

class Random
{

    public static void main( String[] args)
{

        int snumber;

        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a value of n:  ");
        snumber = scan.nextInt();
            if (snumber == 0)
{
                           System.out.println("Thanks for playing, and make it a great day! :)");
            System.exit(0);
}
    while (100 > snumber || snumber > 500)
{
    
System.out.println("So sorry. The value must be between 100 and 500 inclusively.");
        System.out.print("Enter a value of n:  ");
        snumber = scan.nextInt();
        if (snumber == 0)
{
        System.out.println("Thanks for playing, and make it a great day! :)");
                System.exit(0);
}    
        
}
    
}

    public void generate(int snumber) {

    System.out.println("n is " + snumber);

}
}


I edited my code a little to fix it up towards the specifications i need, but i am confused on using parameters X_X thanks again for the help


Well, I see nothing wrong.

The only thing I see is not an error(at least I don't believe it is), but it's just a little confusing to anybody that reads the code.

When you declare your generate(int snumber) method...I wouldn't name the parameter snumber, as there is a variable with the same identifier in your main method.

I'd change it to this.

java
public void generate(int num)
{
System.out.println("n is " + num);
}


One more thing...you never make a call to the generate() method. That might affect the outcome of the program.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 02:05PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month