import java.util.*;
public class RockPaperScissors {
public String toGenerate() {
String x = null;
Random generator = new Random();
int y = generator.nextInt(2);
switch (y) {
case 0 : x = "Rock"; break;
case 1 : x = "Paper"; break;
case 2 : x = "Scissors"; break;
}
return x ;
}
public void toDisplayResult(String x , String y) {
String comp = x;
String c = y;
c = c.toLowerCase();
comp = comp.toLowerCase();
if(comp.equals(c))
System.out.print("\nDRAW !!!");
else if(comp.equals("rock") && c.equals("paper")) //Not Excuting
System.out.print("\nYou WIN !!!");
else if(comp.equals("rock") && (c.equals("scissors") || c.equals("scissor")))
System.out.print("\nYou LOSE !!!");
else if(comp.equals("scissors") && c.equals("paper"))
System.out.print("\nYou LOSE !!!");
else if(comp.equals("scissors") && c.equals("rock"))
System.out.print("\nYou WIN !!!");
else if(comp.equals("paper") && (c.equals("scissor") || c.equals("scissors")))
System.out.print("\nYou WIN !!!");
else if(comp.equals("paper") && c.equals("rock"))
System.out.print("\nYou LOSE !!!");
}
public void toIntroduce() {
System.out.println("Hi !!! Welcome To \"The ROCK-PAPER-SCISSORS\" v0.0508\nDeveloped By Saikat Das\nInstagram : @saikat._");
}
public static void main(String args[]) {
RockPaperScissors obj = new RockPaperScissors();
Scanner in = new Scanner(System.in);
String x = null;
obj.toIntroduce();
System.out.println();
do {
System.out.print("Enter Your Choice : ");
String c = in.next();
String comp = obj.toGenerate();
System.out.print(comp);
System.out.println();
obj.toDisplayResult(comp, c);
System.out.println();
System.out.print("Press \"Y\" To Play Again OR \"N\" To Quit ");
x = in.next();
System.out.println();
}while(x.equalsIgnoreCase("Y"));
System.out.println("Thanks For Playing !!!");
System.out.println("Hope You Enjoyed This Epic Game");
System.out.println("More Updates Will Be Coming");
System.out.println("Follow Me ON Instagram And Stay Tuned !!!");
}
}
Я также научился делать windows, используя JFrame. Но я понятия не имею, как просто отобразить его в моем созданном окне.
import java.awt.*;
import javax.swing.JFrame;
public class Windows extends Canvas{
public void window() {
JFrame window = new JFrame();
Canvas canvas = new Windows();
window.setSize(800, 600);
canvas.setSize(800, 600);
window.add(canvas);
window.pack();
window.setTitle("This Is My Own Window");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.getContentPane().setBackground(Color.white);
}
public static void main(String args[]) {
Windows obj = new Windows();
obj.window();
}
}
Я также думал, если этот метод вообще возможен, если нет, то как я могу это сделать?