Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 ;]

  • #2
    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
    Java != JavaScript
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #4
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ;]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •