Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Some trubles with a simple program

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Some trubles with a simple program

    I have to write a simple "Black Jack" game with dices. Game can have random number of players.
    At the beginning of the game each player roll 3 dices. Program have to add values of the dices and show the result.
    Afterwards asks program if you want to continue, also "if you want to roll next dice"...until player answer "no".
    Then it should save the value of points this player gained, and go to the next player. When i debugged program it turned out that instead of saving each
    "value of points" in arrayPOINTS, it jumpes of whole "for" and value in arrayPOITS is deleted (=0). I don't know how to refer players to an array either.
    I'm not so good in "arrays", so I have some though time writin' this program.

    Here's my "roll-dice" class:

    package practice5;
     
     
     
    public class Terning {
     
    	public int dice;
     
     
    	public Terning() {
    		roll();
    	}
     
    	public int roll() {
     
    			dice = (int)(Math.random()*6) +1;
    			return dice;
    		}
     
    	}

    Here's my player class (It should at least save name and value of poits gained) and player method (Ask about name, roll 3 dices and assume values):

    package practice5;
     
    import java.util.Scanner;
     
    public class Player {
     
    	String player;
    	int points;
     
    	public void setPoints(int points) {
    		this.points = points;
    	}
    	public String getPlayer() {
    		return player;
    	}
    	public void setPlayer(String player) {
    		this.player = player;
    	}
    	public int getPoints() {
    		return points;
    	}
     
     
    	public Player() {
    		int kast1;
    		int kast2;
    		int kast3;
     
     
    		Scanner inn = new Scanner(System.in);
    		System.out.print("Name: ");
    		player = inn.next();
     
    		Terning terning = new Terning();
    		kast1 = terning.roll();
    		kast2 = terning.roll();
    		kast3 = terning.roll();
    		points = kast1 + kast2 + kast3;
    		System.out.print(points);
    		System.out.println();
     
    	}
    }

    And my main program, with "Turn-method = omGang()":
    In addition: How can I call variable in omGang-method from Player-constructor?

    package practice5;
    import java.util.Scanner;
     
    public class MainProgram {
     
     
    	public static void main(String[] args) {
     
    		int ANTALL_SPILLERE = 2;
     
    		// Hopper ut av while, så blir i=0 igjen.
    		for(int j=0;j<ANTALL_SPILLERE;j++) {
    			Player spiller = new Player();
    			omGang();
    		}
    	}
     
    	public static void omGang() {
    		int ANTALL_SPILLERE = 2;
    		boolean choice = true;
    		String valg;
    		int next_kast;
    		int Sum = Player.points; // (How can I call variable in omGang-method from Player constructor?)
    		//	int ANTALL_SPILLERE = 2;
    		String[] Spillere;
    		Spillere = new String[ANTALL_SPILLERE];
    		int[] Poeng;
    		Poeng = new int[ANTALL_SPILLERE];
    		Terning terning = new Terning();
    		Scanner ny = new Scanner(System.in);
     
    		// hopper ut av løkka etter første omgang, hvorfor lagrer den ikke verdiene?
    		for(int i=0;i<ANTALL_SPILLERE+1;i++) {
    			while(choice == true) {
     
    				System.out.print("Vil du fortsette?(j/n): ");
    				valg = ny.next();
    				if(valg.equalsIgnoreCase("n")){
    					choice = false;
    					break;
    				}
    				next_kast = terning.roll();
    				Sum = Sum + next_kast;
    				System.out.println(Sum);
    				Poeng[i] = Sum;
     
     
    				if(Sum>21) {
    					System.out.print(Spillere[i]);
    					System.out.print(" tapte. ");
    					choice = false;
    					break;
    				}
    				if(Sum==21) {
    					System.out.print(Spillere[i]);
    					System.out.print(" vunnet. ");
    					choice = false;
    					break;
    				}
    			}
     
    		}
    		System.out.print("TEST");
    	}
    }

    It would be awesome if some1 could help me out with this1


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Some trubles with a simple program

    How can I call variable in omGang-method from Player-constructor
    Variables defined in methods can not be referenced by other code. They are local to the method.
    The method could return the value of a variable.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    OPTYMISTA (February 14th, 2013)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Some trubles with a simple program

    an extra line in "Player-constructor"
    Constructors do NOT return the value of a variable.

    The omGang() method can return a value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    OPTYMISTA (February 14th, 2013)

  6. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Some trubles with a simple program

    Now I've figured out how I refer each player object to different array-position. Such as array.png. Now I want to do the same thing with points. I've written an extra statement in "omGang()" that says:
     return sum;
    there sum is the assumed value of points after rolling dice.
    I'm beginner and I want to understand what I'm doing, step by step. No matter if I sound like an idiot So thank YOU in advance for your attention to this matter.

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Some trubles with a simple program

    Are you getting errors now? Copy the full text of the error messages and post them.
    Does the code do what you want now? If not please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Re: Thanks for help

    Player object:

    package practice5;
     
    import java.util.Scanner;
     
    public class Player {
     
    	String player;
    	int points;
     
    	public void setPoints(int points) {
    		this.points = points;
    	}
    	public String getPlayer() {
    		return player;
    	}
    	public void setPlayer(String player) {
    		this.player = player;
    	}
    	public int getPoints() {
    		return points;
    	}
     
     
    	public Player() {
     
    		Scanner inn = new Scanner(System.in);
    		System.out.print("Name: ");
    		player = inn.next();
     
    	}
     
    	public String toString() {
    		return String.format(player);
    	}
    }


    omGang-method:
     public static int omGang() {
    		int kast1;
    		int kast2;
    		int kast3;
    		int sum;
            int ANTALL_SPILLERE = 2;
            boolean choice = true;
            String valg;
            int next_kast;
     
            String[] Spiller;
            Spiller = new String[ANTALL_SPILLERE];
     
            int[] Poeng;
            Poeng = new int[ANTALL_SPILLERE];
     
            Scanner ny = new Scanner(System.in);
     
    		Terning terning = new Terning();
    		kast1 = terning.roll();
    		kast2 = terning.roll();
    		kast3 = terning.roll();
    		sum = kast1 + kast2 + kast3;
    		System.out.print(sum);
    		System.out.println();
     
            // hopper ut av løkka etter første omgang, hvorfor lagrer den ikke verdiene?
            for(int i=0;i<ANTALL_SPILLERE+1;i++) {
                while(choice == true) {
     
                    System.out.print("Vil du fortsette?(j/n): ");
                    valg = ny.next();
                    if(valg.equalsIgnoreCase("n")){
                        choice = false;
                        break;
                    }
                    next_kast = terning.roll();
                    sum = sum + next_kast;
                    System.out.println(sum);
                    Poeng[i] = sum;
     
     
                    if(sum>21) {
                        System.out.print(Spiller[i]);
                        System.out.print(" tapte. ");
                        choice = false;
                        break;
                    }
                    if(sum==21) {
                        System.out.print(Spiller[i]);
                        System.out.print(" vunnet. ");
                        choice = false;
                        break;
                    }
     
                }
     
            }
            System.out.print("TEST");
            return sum;
        }

    and main:
       public static void main(String[] args) {
     
            int ANTALL_SPILLERE = 2;
            String[] Spiller = new String[ANTALL_SPILLERE];
            int[] arrayPOINTS = new int[ANTALL_SPILLERE];
     
            // Hopper ut av while, så blir i=0 igjen.
            for(int j=0;j<ANTALL_SPILLERE;j++) {
                Player nySpiller = new Player();
                Spiller[j] = String.valueOf(nySpiller);
                omGang();
                arrayPOINTS[j] = Integer.valueOf(omGang.sum);
            }
        }


    My problem is that I can't set
    arrayPOINTS[j] = Integer.valueOf(omGang.sum);
    , there sum is return statement of omGang-method. Uhm, a player object already encapsulates a name and a score (points). So I just have to add up the points of the current player. But how can I do it? Later I want to compare scores, so I can decide who wins. Shouldn't values of points for each player be saved in an new array (arrayPOINTS) then?

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Some trubles with a simple program

    have to add up the points of the current player. But how can i do that?
    Where are the points? In this array: arrayPOINTS
    A way to add up the contents of an array is to use a loop. Initialize the sum to 0 before the loop, then add each element to the sum in the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Some trubles with a simple program

    I have assumed the value of array "arrayPOINTS" in omGang-method. Here:
     next_kast = terning.roll();
                    sum = sum + next_kast;
                    System.out.println(sum);
                    Poeng[i] = sum;

    But this array is local and only for omGang-method, so I use it only to assume the value of points. What I want now is that this value can be saved in my array, which I call in main.

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Some trubles with a simple program

    Are you asking how to return a value from a method to the code where the method is called?
    What value(s) do you want returned from the method?


    Another idea would be to pass a reference to the array to the method so that the method could update the contents of the array:
      omGang(theArray);
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. please help with simple program!!!
    By jokneez in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 14th, 2011, 10:42 AM
  2. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  3. simple program
    By Memb in forum Paid Java Projects
    Replies: 0
    Last Post: March 17th, 2010, 01:47 PM
  4. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM
  5. Someone please help me write a simple program!!!
    By ocean123 in forum Loops & Control Statements
    Replies: 3
    Last Post: June 14th, 2009, 09:46 PM