PDA

View Full Version : Help with passing variables


davedave22
10-10-2008, 07:53 PM
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:
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 ;]

A1ien51
10-10-2008, 08:40 PM
Java != JavaScript

VortexCortex
10-10-2008, 09:09 PM
The code you posted is Java. This is a Javascript forum, but I happen to be a Java programmer too....


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 main( String[] 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.

davedave22
10-10-2008, 11:12 PM
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 ;]