Results 1 to 5 of 5
- 01-08-2009, 11:12 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
Calling a variable from a different java class
Hi:
I have created a class and inside the class a setter method. This setter method doesnt seem to recognize the variables within the constructor method.
Here is the code of the main program:
Here is the new classJava Code:import java.util.*; class Roulette { static int [] moneyLeft = new int [50]; static String [] players = new String [50]; public static void main (String [] args) { System.out.println("\n WELCOME TO ROULETTE"); System.out.println("---------------------------------------------------------------"); System.out.println("First type the name of the players."); System.out.println("When you are done entering their names, enter \"no\" to continue.\n"); Scanner keyboard = new Scanner (System.in); for(int i=1; i< 50; i++) { System.out.println("What is the name of player #" + i + "?"); String s = keyboard.next(); if(s.equalsIgnoreCase("no")) { i = 50; } else { players[i] = s; System.out.println("How much money does " + players[i] + " want to start off with?"); moneyLeft[i] = keyboard.nextInt(); } } int h =1; Bet actuaBet; System.out.println("How much would you like to bet?"); int moneySpent = keyboard.nextInt(); //moneyLeft[h] = moneyLeft[h] - moneySpent; System.out.println("What Type of bet would you like to do?"); String response = keyboard.next(); if (response.equalsIgnoreCase("N")) { System.out.println("What number do you choose?"); int choice = keyboard.nextInt(); Bet actualBet = new Bet (moneySpent, choice); actualBet.number(); } else if (response.equalsIgnoreCase("O")) { Bet actualBet = new Bet (moneySpent); actualBet.odd(); } else if (response.equalsIgnoreCase("E")) { Bet actualBet = new Bet (moneySpent); actualBet.even(); } else if (response.equalsIgnoreCase("H")) { Bet actualBet = new Bet(moneySpent); System.out.println(actualBet.high()); } else if (response.equalsIgnoreCase("L")) { Bet actualBet = new Bet (moneySpent); actualBet.low(); } else if (response.equalsIgnoreCase("R")) { rules(); } else { System.out.println("That is not a choice"); } } static void rules() { System.out.println("ROULETTE RULES"); System.out.println("-----------------"); System.out.println("If you bet correctly on a NUMBER from 1 to 36 you will earn 10 times your bet\n"); System.out.println("If you bet correctly on a EVEN, ODD, HIGH, or LOW number you will double your bet\n"); System.out.println("IF THE ROULETTE SPINS 0 YOU ALWAYS LOSE\n\n"); System.out.println("BETTING OPTIONS"); System.out.println("-------------------"); System.out.println("N for betting on a NUMBER between 1 and 36"); System.out.println("H for betting on a HIGH number (between 19 and 36)"); System.out.println("L for betting on a LOW number (between 1 and 18)"); System.out.println("O for betting on a ODD number (1, 3, 5, ...)"); System.out.println("E for betting on a EVEN number (2, 4, 6, ...)"); } }
I am currently just working with one setter method, high();Java Code:class Bet { private int spent; private int choice; public Bet(int s) { int spent = s; } public Bet(int s, int c) { int spent = s; int choice = c; } public int number() { int earning, number = (int) (Math.random() * 36); if (number == choice) { earning = spent*10; } else { earning = spent*-1; } return earning; } public Bet high() { int earning, number = (int) (Math.random() * 36); if (number > 18) { earning = this.spent * 2; } else { earning = this.spent * -1; } return new Bet(earning); } public int low() { int earning, number = (int) (Math.random() * 36); if (number <= 18) { earning = spent*2; } else { earning = spent*-1; } return earning; } public int odd() { int earning, number = (int) (Math.random() * 36); if (number%2 != 0) { earning = spent*2; } else { earning = spent*-1; } return earning; } public int even() { int earning, number = (int) (Math.random() * 36); if(number%2 == 0) { earning = spent*2; } else { earning = spent*-1; } return earning; } public String toString() { return "" + spent; } }
When I ask the method to print the output, it print 0.
Any help?
-
One problem I'm seeing on quick look at your code is that you redeclare your spent and choice int variables inside of the Bet constructors. When you do this, the actual fields will not be updated as you would desire. Note the difference between this:
and this:Java Code:class Bet { private int spent; private int choice; public Bet(int s) { int spent = s; // an int variable local only to this constructor }
Java Code:class Bet { private int spent; private int choice; public Bet(int s) { spent = s; // the actual spent field }
- 01-09-2009, 12:44 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
I changed this, as you suggested, but i still dont get the value of the variable.
- 01-09-2009, 12:47 AM #4
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
actually it did work. thanks a million
-
Similar Threads
-
problem calling function from class to class
By alin_ms in forum New To JavaReplies: 3Last Post: 12-19-2008, 07:35 PM -
excecuting a jar file by calling a java class
By Lavanya.vitria in forum Advanced JavaReplies: 1Last Post: 12-13-2008, 12:11 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
Calling a variable from main to another class
By itsme in forum New To JavaReplies: 1Last Post: 12-18-2007, 03:35 PM -
calling a java class from html
By Ed in forum Advanced JavaReplies: 1Last Post: 07-08-2007, 12:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks