這位朋友,我給你寫(xiě)出來(lái)了,我覺(jué)得猜數(shù)字挺好寫(xiě)的,產(chǎn)生不同的數(shù)這個(gè)倒是有點(diǎn)難點(diǎn),我定義了一個(gè)填充數(shù)組的函數(shù),實(shí)現(xiàn)了這個(gè)功能。不過(guò)5分確實(shí)有點(diǎn)少,你自己看看吧。
米東網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),米東網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為米東上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的米東做網(wǎng)站的公司定做!
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();
}
}
import?java.util.Random;
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é)果。見(jiàn)下
次數(shù)?????????????????結(jié)果
1????????????????????你太有才了!
2~6??????????????????這么快就猜出來(lái)了,很聰明么!
大于7????????????????猜了半天才猜出來(lái),小同志,尚需努力?。?/p>
*/
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.
int num = (int)(Math.random()*100)+1;
Scanner sc = new Scanner(System.in);? ?
int guessNum = -1;
while (guessNum != num) {
System.out.println("請(qǐng)輸入1-100之間整數(shù)");
guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
} elseif (guessNum num) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
}
擴(kuò)展資料:
編寫(xiě)思路
1、成1-100之間隨機(jī)數(shù)
(int)(Math.random()*100)+1;
提示用戶輸入數(shù)字,
Scanner??sc=new?Scanner(System.in);
int?guessNum?= sc.nextInt();
需要將隨機(jī)數(shù)和用戶輸入的數(shù)字進(jìn)行比較。
猜一次:
Scanner sc = new Scanner(System.in);
int num = (int)(Math.random()*100)+1;
System.out.println("請(qǐng)輸入0-100之間整數(shù)");
int guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
}?elseif?(guessNum num) {
System.out.println("小啦");
}?else?{
System.out.println("大了");
}
二、使用while循環(huán)
publicstaticvoid main(String[] args) {
int num = (int)(Math.random()*100)+1;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("請(qǐng)輸入1-100之間整數(shù)");
int guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
} elseif (guessNum num) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
}
}
三、最后用while() 括號(hào)中的條件表達(dá)式,當(dāng)用戶猜測(cè)的數(shù)和系統(tǒng)生成的數(shù)字不相等時(shí),就需要繼續(xù)循環(huán)。
java實(shí)現(xiàn)的簡(jiǎn)單猜數(shù)字游戲代碼,通過(guò)隨機(jī)數(shù)與邏輯判斷來(lái)實(shí)現(xiàn)游戲功能 代碼如下: import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { // 產(chǎn)生一個(gè)隨機(jī)數(shù) int n
主要通過(guò)隨機(jī)數(shù)與邏輯判斷來(lái)實(shí)現(xiàn)游戲功能
實(shí)現(xiàn)代碼如下:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 產(chǎn)生一個(gè)隨機(jī)數(shù)
int number = (int) (Math.random() * 100) + 1;
// 加入count
int count = 0;
// 在這里加入最大值,和最小值
int max = 100;
int min = 1;
while (true) {
// 鍵盤(pán)錄入數(shù)據(jù)
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入你要猜的數(shù)據(jù):(" + 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 + "次就猜中了");
// 問(wèn)是否繼續(xù)
System.out.println("請(qǐng)問(wèn)還要繼續(xù)嗎?(yes)");
sc = new Scanner(System.in);
String str = sc.nextLine();
if ("yes".equals(str)) {
// 重寫(xiě)賦值隨機(jī)數(shù)
number = (int) (Math.random() * 100) + 1;
count = 0;
max = 100;
min = 1;
} else {
break;
}
}
} catch (InputMismatchException e) {
System.out.println("你輸入的數(shù)據(jù)有誤");
}
}
}
int gameNum = num();// 系統(tǒng)生成的四位不重復(fù)數(shù)字
while(true){
Scanner sc = new Scanner(System.in);// 鍵盤(pán)輸入
int userNum = sc.nextInt();
if (gameNum == userNum) {// 如果用戶輸入等于系統(tǒng)生成,執(zhí)行下面的代碼
System.out.println("恭喜你猜對(duì)了O(∩_∩)O哈哈~");
num();// 繼續(xù)猜下一個(gè)游戲
} else if (userNum gameNum) {
System.out.println("數(shù)字有點(diǎn)大噢/(ㄒoㄒ)/~~");
} else if (userNum gameNum) {
System.out.println("數(shù)字小了點(diǎn)噢(*^__^*) 嘻嘻……");
}
}
}
public static int num() {
ListInteger list = new ArrayListInteger();// 首先建立個(gè)list集合
String str = "";
list.add(0);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(8);
list.add(9);
Random rand = new Random();
int a = rand.nextInt(list.size());// 隨機(jī)獲取list下標(biāo)
for (int i = 0; i list.size(); i++) {// for循環(huán)
if (list.get(a) == list.get(i)) {
str = str + list.get(i);// 拼接字符串
list.remove(i);
}
}
int b = rand.nextInt(list.size() - 1);
for (int i = 0; i list.size(); i++) {
if (list.get(b) == list.get(i)) {
str = str + list.get(i);
list.remove(i);
}
}
int c = rand.nextInt(list.size() - 1);
for (int i = 0; i list.size(); i++) {
if (list.get(c) == list.get(i)) {
str = str + list.get(i);
list.remove(i);
}
}
int d = rand.nextInt(list.size() - 1);
for (int i = 0; i list.size(); i++) {
if (list.get(d) == list.get(i)) {
str = str + list.get(i);
list.remove(i);
}
}
int num = Integer.valueOf(str);// String轉(zhuǎn)化成int
return num;
}
文章名稱(chēng):猜四個(gè)數(shù)字java代碼,四個(gè)數(shù)字的代碼
文章來(lái)源:http://jinyejixie.com/article4/hsicoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、Google、定制網(wǎng)站、面包屑導(dǎo)航、網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)