大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.util.Random;
创新互联公司,是成都地区的互联网解决方案提供商,用心服务为企业提供网站建设、app软件开发、小程序开发、系统定制网站开发和微信代运营服务。经过数十年的沉淀与积累,沉淀的是技术和服务,让客户少走弯路,踏实做事,诚实做人,用情服务,致力做一个负责任、受尊敬的企业。对客户负责,就是对自己负责,对企业负责。
import java.util.Scanner;
/*
* 游戏随即给出一个0~99(包括0和99)的数字,然后让你猜是什么数字。你可以随便猜一个数字,游戏会提示太大还是太小,从而缩小结果范围。经过几次猜测与提示后,最终退出答案。在游戏过程中。记录你最终猜对时所需要的次数。游戏结束后公布结果。见下
次数 结果
1 你太有才了!
2~6 这么快就猜出来了,很聪明么!
大于7 猜了半天才猜出来,小同志,尚需努力啊!
*/
public class guessGame {
/**
* @param args
*/
public static void main(String[] args) {
int gameValue = (int)(Math.random()()*(100-1)+1);
System.out.println("Rand:"+gameValue);
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字");
int num = sc.nextInt();
int guessCorrectNum=1;
while(true){
if(num==gameValue){
if(guessCorrectNum == 1)
System.out.println("你太有才了!");
else if((guessCorrectNum =2) (guessCorrectNum=6))
System.out.println("这么快就猜出来了,很聪明么");
else if(guessCorrectNum 7)
System.out.println("猜了半天才猜出来,小同志,尚需努力啊!");
break;
}
else{
if (guessCorrectNum =20){
guessCorrectNum = guessCorrectNum + 1;
num = sc.nextInt();
}
else{
System.out.println("20次都猜不出来...,不让你猜了");
break;
}
}
}
}
}
please tell me your q-number,so I can send it by q.
界面类:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
private JLabel lb1;
private JButton btnEnter;
private JButton btnCancel;
private JTextArea taDispaly;
private JTextField txtEnter;
private Oprator oprator;
private JButton btnReplay;
String tx;
private JButton btnEnd;
public static void main(String[] args) {
new MainFrame();
}
public MainFrame() {
oprator = new Oprator();
oprator.randAnswer();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
this.setContentPane(con);
con.setLayout(null);
con.setSize(467, 237);
lb1 = new JLabel("请输入一个4位数:");
con.add(lb1);
lb1.setBounds(44, 24, 140, 17);
txtEnter = new JTextField();
con.add(txtEnter);
txtEnter.setBounds(47, 64, 76, 24);
txtEnter.addKeyListener(this);
taDispaly = new JTextArea();
con.add(taDispaly);
taDispaly.setBounds(249, 9, 225, 203);
taDispaly.setBorder(BorderFactory.createBevelBorder(1));
btnEnter = new JButton();
btnEnter.setEnabled(false);
con.add(btnEnter);
btnEnter.setText("确定");
btnEnter.setBounds(30, 105, 60, 24);
btnEnter.addActionListener(this);
btnCancel = new JButton();
con.add(btnCancel);
btnCancel.setText("取消");
btnCancel.setBounds(107, 105, 60, 24);
btnCancel.addActionListener(this);
btnReplay = new JButton();
con.add(btnReplay);
btnReplay.setText("重玩");
btnReplay.setBounds(28, 148, 60, 24);
btnEnd = new JButton();
con.add(btnEnd);
btnEnd.setText("结束");
btnEnd.setBounds(107, 148, 60, 24);
this.setSize(494, 257);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if (btn == btnCancel) {
btnEnter.setEnabled(false);
txtEnter.setText("");
}
if (!oprator.isEnd()) {
if (btn == btnEnter) {
oprator.setCount(oprator.getCount() + 1);
int[] result = oprator.check(txtEnter.getText());
if (result[0] == 1) {
oprator.setEnd(true);
taDispaly.append("猜中了!!\n");
} else if (oprator.getCount() != 10) {
taDispaly.append(txtEnter.getText() + " "
+ result[1] + "A" + result[2] + "B\n");
} else if (oprator.getCount() == 11) {
taDispaly.append("失败!!\n");
}
btnEnter.setEnabled(false);
txtEnter.setText("");
}
}
if(btn == btnEnd){
this.dispose();
}
if(btn == btnReplay){
oprator.randAnswer();
taDispaly.setText("");
txtEnter.setText("");
txtEnter.setEnabled(false);
}
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
/** 实现只能输入4位数字,并且数字不能重复 */
JTextField text = (JTextField) e.getSource();
tx = text.getText();
int b = e.getKeyChar();
if (b 48 || b 57) {
e.setKeyChar('\0');
}
if (tx.indexOf(b) != -1) {
e.setKeyChar('\0');
}
if (tx.length() == 4) {
e.setKeyChar('\0');
}
if (tx.length() == 3) {
btnEnter.setEnabled(true);
}
}
}
操作类:
import java.util.*;
public class Oprator {
private int[] answer;
private int count;
private boolean isEnd = false;
public Oprator(){
}
/** 产生随机答案 */
public void randAnswer(){
answer = new int[4];
int[] digits = {0,1,2,3,4,5,6,7,8,9};
Random rand = new Random((new Date()).getTime());
for(int i=0;idigits.length;i++){
int ptr = Math.abs(rand.nextInt())%10;
int temp = digits[ptr];
digits[ptr] = digits[i];
digits[i] = temp;
}
for(int i=0;i4;i++){
answer[i] = digits[i];
}
}
/**
* 检查答案
* @param guessNum
* @return int[] 第一位标识是否猜对,第二为A的数量,第三为B的数量.
*
*/
public int[] check(String guessNum){
int[] guess = new int[4];
for(int i=0;i4;i++){
guess[i] = Integer.parseInt(guessNum.substring(i, i+1));
}
int a=0,b=0,c=0;
for(int i = 0;i4;i++){
for(int j=0;j4;j++){
if(answer[i]==guess[j]i==j){
a++;
}else if(answer[i]==guess[j]){
b++;
}
}
}
if(a==4){
c=1;
}
return new int[] {c,a,b};
}
public static void main(String[] args) {
Oprator op = new Oprator();
op.randAnswer();
for(int i=0;iop.answer.length;i++){
System.out.print(op.answer[i]);
}
System.out.println();
Scanner in = new Scanner(System.in);
while(true){
op.check(in.next());
}
}
public int[] getAnser(){
return this.answer;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public boolean isEnd() {
return isEnd;
}
public void setEnd(boolean isEnd) {
this.isEnd = isEnd;
}
}
这位朋友,我给你写出来了,我觉得猜数字挺好写的,产生不同的数这个倒是有点难点,我定义了一个填充数组的函数,实现了这个功能。不过5分确实有点少,你自己看看吧。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Arrays;
public class GuessNum extends JFrame implements ActionListener{
JTextField jtf=new JTextField(16);
JButton b1=new JButton("确定"),b2=new JButton("重玩一局"),b3=new JButton("看答案");
JLabel jl1=new JLabel("请输入四个不同数字:"),jl2=new JLabel(" ");
int[] right=new int[4];
int count=0,A,B;
public GuessNum(){
super("猜数字");
fill(right,9);
setLayout(new FlowLayout(FlowLayout.CENTER));
add(jl1);add(jtf);add(jl2);add(b1);add(b3);add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(330,250);
setVisible(true);
}
public void fill(int[] a,int b){//给数组填充不同的1到b的数字
for(int i=0;ia.length;i++){
a[i]=(int)(Math.random()*b+1);
while(true){
if(in_it(a[i],a,i))a[i]=(int)(Math.random()*b+1);
else break;
}
}
}
public boolean in_it(int num,int[] before,int n){//判断num是否在数组before的前n项内
for(int i=0;in;i++)
if(num==before[i])return true;
return false;
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
count++;
String s=jtf.getText();
if(s.length()!=4)jl2.setText("格式错误");
else {
A=0;B=0;
for(int i=0;i4;i++)
for(int j=0;j4;j++)
if((s.charAt(i)-'0')==right[j])
{
if(i==j)A++;
else B++;
}
if(A==4){jl2.setText("RIGHT!猜了"+count+"次");
count=0;
}
else jl2.setText("提示:"+A+"A"+B+"B");
}
}
if(e.getSource()==b2){
fill(right,9);
jtf.setText("");
jl2.setText(" ");
}
if(e.getSource()==b3){
jl2.setText(Arrays.toString(right));
}
}
public static void main (String[] args) {
new GuessNum();
}
}