// **如何将数学随机生成器放入此程序中。 谢谢 :) ** /
//无论如何,这个程序都是关于使用JOption实用程序的摇滚,纸张,剪刀游戏。 //你能帮我把数学随机生成器放在播放器2中吗?因为我不知道如何把它。 import javax.swing.JOptionPane; 课堂活动13 {
public static void main(String []args){
int p1 = 0;
int p2 = 0;
String player1 = JOptionPane.showInputDialog("Select an option:\n(r) Rock\n(p) Paper\n(s) Scissor\nPlease input a keyword for player 1");
String player2 = JOptionPane.showInputDialog("Select an option:\n(r) Rock\n(p) Paper\n(s) Scissor\nPlease input a keyword for player 2");
String rock = "R";
String paper = "P";
String scissor = "S";
if(player1.equalsIgnoreCase(rock)){
p1 = 1;
}
if(player1.equalsIgnoreCase(paper)){
p1 = 2;
}
if(player1.equalsIgnoreCase(scissor)){
p1 = 3;
}
if(player2.equalsIgnoreCase(rock)){
p2 = 1;
}
if(player2.equalsIgnoreCase(paper)){
p2 = 2;
}
if(player2.equalsIgnoreCase(scissor)){
p2 = 3;
}
if(p1 == p2){
JOptionPane.showMessageDialog(null, "Draw", "Result", JOptionPane.INFORMATION_MESSAGE);
}
if(p1 == 1 && p2 == 2){
JOptionPane.showMessageDialog(null, "Player 2 Wins", "Result", JOptionPane.INFORMATION_MESSAGE);
}
if(p1 == 1 && p2 == 3){
JOptionPane.showMessageDialog(null, "Player 1 Wins", "Result", JOptionPane.INFORMATION_MESSAGE);
}
if(p1 == 2 && p2 == 1){
JOptionPane.showMessageDialog(null, "Player 1 Wins", "Result", JOptionPane.INFORMATION_MESSAGE);
}
if(p1 == 2 && p2 == 3){
JOptionPane.showMessageDialog(null, "Player 2 Wins", "Result", JOptionPane.INFORMATION_MESSAGE);
}
if(p1 == 3 && p2 == 1){
JOptionPane.showMessageDialog(null, "Player 2 Wins", "Result", JOptionPane.INFORMATION_MESSAGE);
}
if(p1 == 3 && p2 == 2){
JOptionPane.showMessageDialog(null, "Player 1 Wins", "Result", JOptionPane.INFORMATION_MESSAGE);
}
}
}
答案 0 :(得分:1)
随机数可以由java.util.Random生成。
准备生成随机数:即代码放置在开始附近。
import java.util.Random;
Random randomGenerator = new Random(); /* create intance of Random */
将1到3之间的随机数设置为变量p2的代码
p2 = randomGenerator.nextInt(3) + 1 /* nextInt(3) will return random number between 0 to 2, so add 1 */
很简单。