i'm currently trying to write a loop for a project where i am making a basic game of black jack.
in this game i want to make it loop round if the player chooses to twist. i currently have.
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
/** This program is made to simulate a game of black jack*/
public class first {
public static void main (String[] args ) {
/** this bit is creating the computers score. */
Random r = new Random();
int compscore= 0 ;
int card;
for (int i = 0 ; i<15; i = i + card) {
card = r.nextInt (12)+1;
compscore = (compscore + card);
System.out.println("compscore = " + compscore);
}
String st = JOptionPane.showInputDialog("1 to twist 2 to stick");
int st2 = Integer.parseInt(st);
int card2;
card2 = r.nextInt (12)+1;
int playerscore=card2;
int card3;
card3 = r.nextInt (12)+1;
System.out.println("player score " + playerscore);
// if a player twists this code deals with this action.
if (st2 == 1)
{
System.out.println("twist");
while (st2==1)
playerscore= (playerscore + card3);
st = JOptionPane.showInputDialog("1 to twist 2 to stick");
}
else if (st2 == 2)
{
System.out.println("stick");
if (playerscore>compscore)
{
System.out.println("player wins");
}
else
{
System.out.println("player loses");
}
}
//* this is to ensure that only the two valid inputs can be used in the program*/
else
{
System.out.println("invalid command");
}
}
}
origianlly i thought that if i used the value of st2 , i could control the loop by having the player type in again if they wanted to stick, but it just ends up on a inifinite loop of doing nothing. any help would be appreciated this is frustrating and i can't find anyhting on it anywhere.