Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2008, 07:53 PM   PM User | #1
davedave22
New to the CF scene

 
Join Date: Oct 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
davedave22 is an unknown quantity at this point
Help with passing variables

Hey guys, this is my first post here and i'm glad there are people out there willing to help. So here's my problem:
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 getNumber(int num)  
{  
          System.out.print("n is " + num);  
}  
	
	





}
Well, this is a problem i've been having with java since i started using it this year. I'm trying to pass the variable "snumber" that the user has input into another method "getNumber". It compiles but i am not getting the print of "n is --" . Help is appreciated ;]
davedave22 is offline   Reply With Quote
Old 10-10-2008, 08:40 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Java != JavaScript
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 10-10-2008, 09:09 PM   PM User | #3
VortexCortex
Regular Coder

 
Join Date: May 2005
Posts: 142
Thanks: 0
Thanked 0 Times in 0 Posts
VortexCortex is an unknown quantity at this point
Exclamation

The code you posted is Java. This is a Javascript forum, but I happen to be a Java programmer too....

PHP Code:
import java.util.*;
import java.lang.Math;

// Note: java.lang.Math has a method named random()... to make things less confusing we'll rename the class to "MyGame"
class MyGame
{
    
// instance variable declaration
    
private int snumber;
    private 
Scanner scan;
    
    
// Constructor.
    
public MyGame()
    {    
// Initialize variables here.
        
snumber = -1;
        
scan = new Scanner(System.in);
    }
    
    
// The game loop.
    
public void runGame()
    {
        while (
snumber != 0)
        {
            
System.out.print("Enter a value of n or '0' to exit: ");
            
snumber scan.nextInt();
            if ( (
snumber >= 100) && (snumber <= 500) )
            {    
// Play your game here.
                // Note: this line calls the showNumber function which outputs the value of the Instance variable "snumber"
                
showNumber();
            } else if (
snumber != 0// Avoids the exit case of Zero
            
{    // Oops, bad number, try again.
                
System.out.println("So sorry. The value must be between 100 and 500 inclusively.");
            }
        }
        
// Nothing to do after this line, the program will exit.
        
System.out.println("Thanks for playing, and make it a great day! :)");
    }
    
    
// Main method get's called from a static context.
    
public static void mainString[] args
    {    
// Create a new MyGame object.
        
MyGame application = new MyGame();
        
// Start playing the game.
        
application.runGame();
        
    }

    
// Output the current n value.
    
public void showNumber()  
    {    
// outputs the number stored in the instance variable "snumber"
        
System.out.println("n is " getNumber());
    }
    
    
// returns the snumber value
    
public int getNumber()
    {
        return 
snumber;
    }


Copy and paste this code into your text editor.
Save the file as "MyGame.java"

Compile via: javac MyGame.java

Run via: java MyGame

Moderator's note: Please move this to the Java / JSP forum.

Last edited by VortexCortex; 10-10-2008 at 09:10 PM.. Reason: Comment correction.
VortexCortex is offline   Reply With Quote
Old 10-10-2008, 11:12 PM   PM User | #4
davedave22
New to the CF scene

 
Join Date: Oct 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
davedave22 is an unknown quantity at this point
Very Sorry about that hehe, guess i didn't realize. VortexCortex- thank you very much for your support. You really did a number on my code lol, but that is exactly what i wanted. And also thank you for adding in the notes about what everything does. Works exactly how i want! again, thank you ;]
davedave22 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:41 PM.


Advertisement
Log in to turn off these ads.