package JackEnPoy;
import javax.swing.*;
import java.lang.Math;
public class JackEnPoy {
class Main
{
static JFrame f;
static String user;
static int num;
Main()
{
f = new JFrame();
}
public static String input()
{
user = JOptionPane.showInputDialog(f, "Select from rock, paper, scissor (r/p/s): ");
return user;
}
public static int computer()
{
int min=0, max=5;
int range = max - min + 1;
num = (int)(Math.random()*range) + min;
if(num!=0 && num!=2 && num!=5)
{
while(num!=0 && num!=2 && num!=5)
{
num = (int)(Math.random()*range) + min;
}
}
return num;
}
public static void main(String[] args)
{
int comp_win = 0, user_win = 0;
do
{
String comp="";
int n = computer();
if(n == 0)
comp = "r";
else if(n==2)
comp = "s";
else if(n==5)
comp = "p";
String user = input();
if(comp=="r" && user=="s")
comp_win++;
else if(user=="r" && comp=="s")
user_win++;
if(comp=="s" && user=="p")
comp_win++;
else if(user=="s" && comp=="p")
user_win++;
if(comp=="p" && user=="r")
comp_win++;
else if(user=="p" && comp=="r")
user_win++;
String msg;
if(comp_win > user_win)
msg = "Computer won this round";
else
msg = "User won this round";
JOptionPane.showMessageDialog(f, msg);
}while(comp_win<5 || user_win<5);
String winner;
if(comp_win > user_win)
winner = "Computer won this game";
else
winner = "User won this game";
JOptionPane.showMessageDialog(f, winner);
}
}
}
I just wanted to create a jackenpoy program against computer and the first to get 5 points win.
Whenever I enter my choice it only shows user won this round. I tried to enter my choice multiple times and even if it shows user won this round for the 5th time, it will only go back to ask user input
Aucun commentaire:
Enregistrer un commentaire