真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java猜四個(gè)數(shù)字源代碼 java中猜數(shù)字怎么編程

用JAVA寫一個(gè)程序 求解 題目:猜數(shù)字游戲 隨機(jī)生成4個(gè)0到9的整數(shù),組成一個(gè)序列

import?java.util.Random;

創(chuàng)新互聯(lián)公司,是成都地區(qū)的互聯(lián)網(wǎng)解決方案提供商,用心服務(wù)為企業(yè)提供網(wǎng)站建設(shè)、app軟件開發(fā)、小程序開發(fā)、系統(tǒng)定制網(wǎng)站開發(fā)和微信代運(yùn)營(yíng)服務(wù)。經(jīng)過(guò)數(shù)十年的沉淀與積累,沉淀的是技術(shù)和服務(wù),讓客戶少走彎路,踏實(shí)做事,誠(chéng)實(shí)做人,用情服務(wù),致力做一個(gè)負(fù)責(zé)任、受尊敬的企業(yè)。對(duì)客戶負(fù)責(zé),就是對(duì)自己負(fù)責(zé),對(duì)企業(yè)負(fù)責(zé)。

import?java.util.Scanner;

/*

*?游戲隨即給出一個(gè)0~99(包括0和99)的數(shù)字,然后讓你猜是什么數(shù)字。你可以隨便猜一個(gè)數(shù)字,游戲會(huì)提示太大還是太小,從而縮小結(jié)果范圍。經(jīng)過(guò)幾次猜測(cè)與提示后,最終退出答案。在游戲過(guò)程中。記錄你最終猜對(duì)時(shí)所需要的次數(shù)。游戲結(jié)束后公布結(jié)果。見下

次數(shù)?????????????????結(jié)果

1????????????????????你太有才了!

2~6??????????????????這么快就猜出來(lái)了,很聰明么!

大于7????????????????猜了半天才猜出來(lái),小同志,尚需努力??!

*/

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("請(qǐng)輸入一個(gè)數(shù)字");

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("這么快就猜出來(lái)了,很聰明么");

else?if(guessCorrectNum?7)

System.out.println("猜了半天才猜出來(lái),小同志,尚需努力啊!");

break;

}

else{

if?(guessCorrectNum?=20){

guessCorrectNum?=?guessCorrectNum?+?1;

num?=?sc.nextInt();?

}

else{

System.out.println("20次都猜不出來(lái)...,不讓你猜了");

break;

}

}

}

}

}

please tell me your q-number,so I can send it by q.

java編寫的猜4個(gè)數(shù)字游戲

界面類:

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("請(qǐng)輸入一個(gè)4位數(shù):");

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("結(jié)束");

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) {

/** 實(shí)現(xiàn)只能輸入4位數(shù)字,并且數(shù)字不能重復(fù) */

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(){

}

/** 產(chǎn)生隨機(jī)答案 */

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[] 第一位標(biāo)識(shí)是否猜對(duì),第二為A的數(shù)量,第三為B的數(shù)量.

*

*/

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;

}

}

java猜4個(gè)數(shù)字游戲

這位朋友,我給你寫出來(lái)了,我覺得猜數(shù)字挺好寫的,產(chǎn)生不同的數(shù)這個(gè)倒是有點(diǎn)難點(diǎn),我定義了一個(gè)填充數(shù)組的函數(shù),實(shí)現(xiàn)了這個(gè)功能。不過(guò)5分確實(shí)有點(diǎn)少,你自己看看吧。

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("請(qǐng)輸入四個(gè)不同數(shù)字:"),jl2=new JLabel(" ");

int[] right=new int[4];

int count=0,A,B;

public GuessNum(){

super("猜數(shù)字");

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){//給數(shù)組填充不同的1到b的數(shù)字

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是否在數(shù)組before的前n項(xiàng)內(nèi)

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("格式錯(cuò)誤");

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();

}

}


文章題目:java猜四個(gè)數(shù)字源代碼 java中猜數(shù)字怎么編程
當(dāng)前地址:http://weahome.cn/article/ddiojid.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部