package hellomysql; import java.util.Random; import java.util.Scanner; public class hellosin { public static void main(String[] agrs){ int max=100; int min=1; Scanner shui=new Scanner(System.in); Random random = new Random(); int rd=random.nextInt(max)%(max-min+1) + min;; System.out.println("随机数:"+rd); while(true){ int cmcc=shui.nextInt(); if(cmcc==rd){ System.out.println("正确"); }else{ System.out.println("错误"); } } } }好东西!!!!!!!!!!!!
2,怎么用java写一个猜数字游戏
import java.util.Scanner;public class GuessNum public static void main(String[] args) Scanner sc = new Scanner(System.in); int max = 3;//随机数的最大值 int times = 5;//玩的局数 int score = 0;//初始的积分 for (int i = 0; i < times; i++) int x = (int) (Math.random() * (max+1)) ; System.out.println("第"+(i+1)+"局:主机已经生成一个[0~"+max+"]的数字"); System.out.println("请玩家输入一个数字:"); int y = sc.nextInt(); if (x == y) System.out.println("You Win"); score++; } else System.out.println("Sorry"); } } System.out.println("你的得分是:"+score); }}输出第1局:主机已经生成一个[0~3]的数字请玩家输入一个数字:2You Win第2局:主机已经生成一个[0~3]的数字请玩家输入一个数字:1Sorry第3局:主机已经生成一个[0~3]的数字请玩家输入一个数字:0You Win第4局:主机已经生成一个[0~3]的数字请玩家输入一个数字:2Sorry第5局:主机已经生成一个[0~3]的数字请玩家输入一个数字:1You Win你的得分是:3java实现的简单猜数字游戏代码,通过随机数与逻辑判断来实现游戏功能 代码如下: import java.util.inputmismatchexception; import java.util.scanner; public class main { public static void main(string[] args) { // 产生一个随机数 int number = (int) (math.random() * 100) + 1; // 加入count int count = 0; // 在这里加入最大值,和最小值 int max = 100; int min = 1; while (true) { // 键盘录入数据 scanner sc = new scanner(system.in); system.out.println("请输入你要猜的数据:(" + min + "~" + max + ")"); try { count++; int guessnumber = sc.nextint(); // 判断 if (guessnumber > number) { max = guessnumber; system.out.println("你猜大了"); } else if (guessnumber < number) { min = guessnumber; system.out.println("你猜小了"); } else { system.out.println("恭喜你,花了" + count + "次就猜中了"); // 问是否继续 system.out.println("请问还要继续吗?(yes)"); sc = new scanner(system.in); string str = sc.nextline(); if ("yes".equals(str)) { // 重写赋值随机数 number = (int) (math.random() * 100) + 1; count = 0; max = 100; min = 1; } else { break; } } } catch (inputmismatchexception e) { system.out.println("你输入的数据有误"); } } } }
3,利用图形用户界面设计实现猜数字游戏
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestGuessNumber extends JFrame {
private static final long serialVersionUID = -4922390040187620343L;
private JLabel label;
private JTextField[] text = new JTextField[3];
private JButton button;
private String[] nums = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
private String[] number;
private int times;
public static void main(String[] args) {
new TestGuessNumber();
}
public TestGuessNumber() {
JOptionPane.showMessageDialog(null, "。。。", "游戏规则",
JOptionPane.INFORMATION_MESSAGE); // 。。。处填写规则,换行用“\n”
init();
}
private void init() {
number = getNumber();
this.setTitle("猜数字");
this.setBounds(500, 400, 200, 160);
this.setResizable(false);
this.getContentPane().setLayout(new GridLayout(3, 1));
label = new JLabel("请输入你猜的数字", JLabel.CENTER);
this.getContentPane().add(label);
JPanel panel = new JPanel(new FlowLayout());
for (int i = 0; i < text.length; i++) {
text[i] = new JTextField(3);
panel.add(text[i]);
}
this.getContentPane().add(panel);
button = new JButton("确定");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (JTextField tmp : text) {
if (! tmp.getText().matches("^[0-9]$")) {
JOptionPane.showMessageDialog(null, "每个输入框请输入一位数字!");
return;
}
}
if (times++ >= 8) {
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null,
"对不起,请再接再厉!\n是否继续?", "失败", JOptionPane.YES_NO_OPTION)) {
dispose();
new TestGuessNumber();
return;
} else {
dispose();
System.exit(0);
}
}
if (showResult()) {
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null,
"你用了" + times + "次,猜对了数字!\n是否继续?",
"成功", JOptionPane.YES_NO_OPTION)) {
dispose();
new TestGuessNumber();
} else {
dispose();
System.exit(0);
}
}
}
});
this.getContentPane().add(button);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
this.setVisible(true);
// System.out.println(number[0] + number[1] + number[2]);
}
private String[] getNumber() {
String[] num = new String[3];
num[0] = nums[(int) (Math.random() * 10)];
do {
num[1] = nums[(int) (Math.random() * 10)];
} while (num[1] == num[0]);
do {
num[2] = nums[(int) (Math.random() * 10)];
} while (num[2] == num[0] || num[2] == num[1]);
return num;
}
private boolean showResult() {
int A = 0, B = 0;
String num1 = text[0].getText().trim();
String num2 = text[1].getText().trim();
String num3 = text[2].getText().trim();
if (num1.equals(number[0])) {
A++;
} else if (num1.equals(number[1]) || num1.equals(number[2])) {
B++;
}
if (num2.equals(number[1])) {
A++;
} else if (num2.equals(number[0]) || num2.equals(number[2])) {
B++;
}
if (num3.equals(number[2])) {
A++;
} else if (num3.equals(number[0]) || num3.equals(number[1])) {
B++;
}
label.setText("第" + times + "次:" + A + "A" + B + "B");
return A == 3 ? true : false;
}
}分太少,没有现成的没人给你写,哎..................