## Java

Number of posts: 18218

This Forum Only

help with my blackjack program Posted by bw125 on 29 May 2008 at 6:24 PM
i have two questions

1: My program just keeps on using the same card value throughtout the whole program. Each of my cards are determined by a random number. The problem is my random number keeps on staying the same for every game which is held together by a loop that keeps on interating until the user runs of of money or wants to quit. Here is my random number method-

public int getRanNum(){
RanNum = (int)((13 - 2 + 2) * Math.random() + 2);
return(RanNum);
}

2. My 2nd problem is my betting. When the user inputs a bet greater than the current amount of money, it goes to the checkBet method which has the user input a new bet. However it just uses the first input of bet in my program. Here is the code-

from main method...

System.out.println("How much of your " + money.format(Cash) + " do you want to bet ");
bet = input.nextInt();
play.checkBet(bet, Cash);

from my class called card....

public void checkBet(int bet, int Cash){
if(bet > Cash){
Scanner input = new Scanner(System.in);
System.out.println("Not enough money enter a lower amount ");
bet = input.nextInt();
}
}
Re: help with my blackjack program Posted by zibadian on 29 May 2008 at 8:51 PM
: i have two questions
:
: 1: My program just keeps on using the same card value throughtout
: the whole program. Each of my cards are determined by a random
: number. The problem is my random number keeps on staying the same
: for every game which is held together by a loop that keeps on
: interating until the user runs of of money or wants to quit. Here is
: my random number method-
:
: public int getRanNum(){
: RanNum = (int)((13 - 2 + 2) * Math.random() + 2);
: return(RanNum);
: }
:
: 2. My 2nd problem is my betting. When the user inputs a bet greater
: than the current amount of money, it goes to the checkBet method
: which has the user input a new bet. However it just uses the first
: input of bet in my program. Here is the code-
:
: from main method...
:
: System.out.println("How much of your " + money.format(Cash) + " do
: you want to bet ");
: bet = input.nextInt();
: play.checkBet(bet, Cash);
:
:
: from my class called card....
:
: public void checkBet(int bet, int Cash){
: if(bet > Cash){
: Scanner input = new Scanner(System.in);
: System.out.println("Not enough money enter a lower
: amount ");
: bet = input.nextInt();
: }
: }
:
1) I've tested your random generator with this line several times:
```for (int i = 0; i < 10; i++)
System.out.print((int)((13 - 2 + 2) * Math.random() + 2)+", ");
```

and never got the same value for all 10. Here's the result of one of those runs: 5, 14, 4, 7, 8, 9, 13, 6, 3, 7,

2) In Java all method parameters are non-variable. Thus if the value of a parameter is changed within a method, it is never reflected back to the calling method. The solution is to make your method a function, which returns the bet value.

Note: I've removed the second thread you created since it was the same as this one.
Re: help with my blackjack program Posted by bw125 on 30 May 2008 at 1:07 PM
: : i have two questions
: :
: : 1: My program just keeps on using the same card value throughtout
: : the whole program. Each of my cards are determined by a random
: : number. The problem is my random number keeps on staying the same
: : for every game which is held together by a loop that keeps on
: : interating until the user runs of of money or wants to quit. Here is
: : my random number method-
: :
: : public int getRanNum(){
: : RanNum = (int)((13 - 2 + 2) * Math.random() + 2);
: : return(RanNum);
: : }
: :
: : 2. My 2nd problem is my betting. When the user inputs a bet greater
: : than the current amount of money, it goes to the checkBet method
: : which has the user input a new bet. However it just uses the first
: : input of bet in my program. Here is the code-
: :
: : from main method...
: :
: : System.out.println("How much of your " + money.format(Cash) + " do
: : you want to bet ");
: : bet = input.nextInt();
: : play.checkBet(bet, Cash);
: :
: :
: : from my class called card....
: :
: : public void checkBet(int bet, int Cash){
: : if(bet > Cash){
: : Scanner input = new Scanner(System.in);
: : System.out.println("Not enough money enter a lower
: : amount ");
: : bet = input.nextInt();
: : }
: : }
: :
: 1) I've tested your random generator with this line several times:
:
```:
: for (int i = 0; i < 10; i++)
:    System.out.print((int)((13 - 2 + 2) * Math.random() + 2)+", ");
: ```
:
: and never got the same value for all 10. Here's the result of one of
: those runs: 5, 14, 4, 7, 8, 9, 13, 6, 3, 7,
:
: 2) In Java all method parameters are non-variable. Thus if the value
: of a parameter is changed within a method, it is never reflected
: back to the calling method. The solution is to make your method a
: function, which returns the bet value.
:
: Note: I've removed the second thread you created since it was the
: same as this one.

Thank you very much. Ps. if anyone can find any individual .gif or .jpeg pictures of all the individual cards please contect me
Re: help with my blackjack program Posted by zibadian on 30 May 2008 at 1:28 PM
: : : i have two questions
: : :
: : : 1: My program just keeps on using the same card value throughtout
: : : the whole program. Each of my cards are determined by a random
: : : number. The problem is my random number keeps on staying the same
: : : for every game which is held together by a loop that keeps on
: : : interating until the user runs of of money or wants to quit. Here is
: : : my random number method-
: : :
: : : public int getRanNum(){
: : : RanNum = (int)((13 - 2 + 2) * Math.random() + 2);
: : : return(RanNum);
: : : }
: : :
: : : 2. My 2nd problem is my betting. When the user inputs a bet greater
: : : than the current amount of money, it goes to the checkBet method
: : : which has the user input a new bet. However it just uses the first
: : : input of bet in my program. Here is the code-
: : :
: : : from main method...
: : :
: : : System.out.println("How much of your " + money.format(Cash) + " do
: : : you want to bet ");
: : : bet = input.nextInt();
: : : play.checkBet(bet, Cash);
: : :
: : :
: : : from my class called card....
: : :
: : : public void checkBet(int bet, int Cash){
: : : if(bet > Cash){
: : : Scanner input = new Scanner(System.in);
: : : System.out.println("Not enough money enter a lower
: : : amount ");
: : : bet = input.nextInt();
: : : }
: : : }
: : :
: : 1) I've tested your random generator with this line several times:
: :
```: :
: : for (int i = 0; i < 10; i++)
: :    System.out.print((int)((13 - 2 + 2) * Math.random() + 2)+", ");
: : ```
: :
: : and never got the same value for all 10. Here's the result of one of
: : those runs: 5, 14, 4, 7, 8, 9, 13, 6, 3, 7,
: :
: : 2) In Java all method parameters are non-variable. Thus if the value
: : of a parameter is changed within a method, it is never reflected
: : back to the calling method. The solution is to make your method a
: : function, which returns the bet value.
: :
: : Note: I've removed the second thread you created since it was the
: : same as this one.
:
: Thank you very much. Ps. if anyone can find any individual .gif or
: .jpeg pictures of all the individual cards please contect me
:
Why don't you make them yourself? It isn't as hard as it sounds. Nearly every font has the card-suit symbols included: ♠♣♥♦ (I hope these 4 will appear correctly). You can easily type them into paint (or another drwing program) and add a number/letter to it to produce an image.
Alternatively you could dynamically let your program draw them onto a Graphics object to produce the images.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic