導航:首頁 > 編程語言 > 猜字游戲java

猜字游戲java

發布時間:2022-08-21 21:57:31

A. 如何用java語言實現猜數字游戲

java實現的簡單猜數字游戲代碼,通過隨機數與邏輯判斷來實現游戲功能 代碼如下: import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { // 產生一個隨機數 int n

B. Java猜數字游戲

import java.util.Random;
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;
}

}

}
}

}

C. java猜數字游戲

import java.util.Random;

import java.util.Scanner;

/**

* @Author: Cool_Wu

* @Date: 2020-12-01 23:39

*/

public class GuessNumberGame {

static int count = 0;

static int answer = new Random().nextInt(100);

public static void main(String[] args) throws Exception {

System.out.println("猜數字游戲開始,該數字是一個0~100之間的整數");

compareNum();

}

public static void compareNum() throws Exception {

if (count >= 10){

System.out.println("正確答案是:" + answer);

System.out.println("你太笨了,下次再來吧!");

return;

}

count++;

int n = receiveNum();

if (n < 0){

throw new Exception("您輸入的數字不符合要求,請重新輸入!");

}

if (n > 99){

throw new Exception("輸入錯誤,請輸入正確的數字!");

}

if (n < answer){

System.out.println("太小了,再大一點!");

compareNum();

}

if (n > answer){

System.out.println("太大了,再小一點!");

compareNum();

}

if (n == answer){

System.out.println("恭喜你,猜對了!");

}

}

public static int receiveNum() {

System.out.println("請輸入您猜的數字:");

int n = new Scanner(System.in).nextInt();

return n;

}

}


運行結果

D. java猜字程序代碼怎麼寫

import java.util.Scanner;
package Ly;
public class Yanshi {
public static void main(String[] args) {
char[] c={8, 4, 2, 1, 23, 344, 12};
Scanner input=new Scanner(System.in);
System.out.print("請輸入一個整數: ");
int k= input.nextInt();
int f=0;
for(int i=0;i<=6;i++)
{
if(k==c[i])
{
f=1;
System.out.print("有此數!");}
}
if(f==0)
System.out.print("無此數!");

}
}

E. 用java編寫一個猜數字游戲,

packageday06;
importjava.util.Scanner;
//猜字元游戲
publicclassGuessingGame{
//主方法
publicstaticvoidmain(String[]args){
Scannerscan=newScanner(System.in);
intcount=0;//猜錯的次數
char[]chs=generate();//隨機生成的字元數組
System.out.println(chs);//作弊
while(true){//自造死循環
System.out.println("猜吧!");
Stringstr=scan.next().toUpperCase();//獲取用戶輸入的字元串
if(str.equals("EXIT")){//判斷str是否是EXIT
System.out.println("下次再來吧!");
break;
}
char[]input=str.toCharArray();//將字元串轉換為字元數組
int[]result=check(chs,input);//對比
if(result[0]==chs.length){//位置對為5
intscore=chs.length*100-count*10;//一個字元100分,錯一次減10分
System.out.println("恭喜你猜對了,得分:"+score);
break;//猜對時跳出循環
}else{//沒猜對
count++;//猜錯次數增1
System.out.println("字元對:"+result[1]+"個,位置對:"+result[0]+"個");
}
}
}
//隨機生成5個字元數組
publicstaticchar[]generate(){
char[]chs=newchar[5];
char[]letters={'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T','U','V',
'W','X','Y','Z'};
boolean[]flags=newboolean[letters.length];//1.
for(inti=0;i<chs.length;i++){
intindex;
do{
index=(int)(Math.random()*letters.length);//0到25
}while(flags[index]==true);//2.
chs[i]=letters[index];
flags[index]=true;//3.
}
returnchs;
}
//對比隨機數組與用戶輸入的數組
publicstaticint[]check(char[]chs,char[]input){
int[]result=newint[2];
for(inti=0;i<chs.length;i++){
for(intj=0;j<input.length;j++){
if(chs[i]==input[j]){//字元對
result[1]++;//字元對個數增1
if(i==j){//位置對
result[0]++;//位置對個數增1
}
break;
}
}
}
returnresult;
}
}

F. 用Java編寫一個猜數字的游戲

新手炒瀝青初期需要投入多少資金?
我這邊是3W,最低沒有門檻,你就是入兩千都可以玩,但問題是兩千隻能操作1手,而且沒有抗風險資金了,你買了之後只要點位瞬間波動一個點你就直接平倉了,沒法玩的,所以我都是入金五萬以上玩的。

G. 用JAVA語言編寫一個「猜數字游戲」的程序

int num = (int)(Math.random()*100)+1;

Scanner sc = new Scanner(System.in);

int guessNum = -1;

while (guessNum != num) {

System.out.println("請輸入1-100之間整數");

guessNum = sc.nextInt();

if (guessNum == num) {

System.out.println("中啦");

} elseif (guessNum < num) {

System.out.println("小啦");

} else {

System.out.println("大了");

}

}

(7)猜字游戲java擴展閱讀:

編寫思路

1、成1-100之間隨機數

(int)(Math.random()*100)+1;

提示用戶輸入數字,

Scannersc=newScanner(System.in);

intguessNum= sc.nextInt();

需要將隨機數和用戶輸入的數字進行比較。

猜一次:

Scanner sc = new Scanner(System.in);

int num = (int)(Math.random()*100)+1;

System.out.println("請輸入0-100之間整數");

int guessNum = sc.nextInt();

if (guessNum == num) {

System.out.println("中啦");

}elseif(guessNum < num) {

System.out.println("小啦");

}else{

System.out.println("大了");

}

二、使用while循環

publicstaticvoid main(String[] args) {

int num = (int)(Math.random()*100)+1;

Scanner sc = new Scanner(System.in);

while (true) {

System.out.println("請輸入1-100之間整數");

int guessNum = sc.nextInt();

if (guessNum == num) {

System.out.println("中啦");

} elseif (guessNum < num) {

System.out.println("小啦");

} else {

System.out.println("大了");

}

}

}

三、最後用while() 括弧中的條件表達式,當用戶猜測的數和系統生成的數字不相等時,就需要繼續循環。

H. 用JAVA編猜數字游戲

1)
程序隨機分配給客戶一個1—100之間的整數
Random
gen
=
new
Random();
int
a
=
gen.nextInt(100)+1;
//不加1是0到99
2)
用戶在輸入對話框中輸入自己的猜測
Scanner
in
=
new
Scanner(System.in);
System.out.println("give
me
a
number")
int
b
=
in.nextInt();
3)
程序返回提示信息,提示信息分別是:「猜大了」、「猜小了」和「猜對了」。
if(b>a)
System.out.println("big");
else
if(b<a)
System.out.println("small");
else
System.out.println("right");
4)
用戶可根據提示信息再次輸入猜測,直到提示信息是「猜對了」。
while(b!=a){
if(b>a)
System.out.println("big");
else
if(b<a)
System.out.println("small");
System.out.println("give
me
a
new
number")
int
b
=
in.nextInt();
}
System.out.println("right");
3看懂以後,3,4可以合在一起,用4的回答.

I. 用java來寫一個猜數字游戲,要用到界面

packagep1;

importjava.awt.Color;
importjava.awt.Container;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.BoxLayout;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importjavax.swing.WindowConstants;


{
=1L;
intrandom=0;

publicstaticvoidmain(String[]args)
{
newMyFrame();
}

publicMyFrame()
{
Containercontainer=getContentPane();
container.setLayout(newBoxLayout(container,BoxLayout.Y_AXIS|BoxLayout.LINE_AXIS));
container.setBackground(Color.pink);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);
setResizable(false);
setTitle("輸入1到10的數字猜");
JPanelp=newJPanel();
finalJTextFieldjtf=newJTextField(20);
jtf.setToolTipText("輸入1到10的數字猜");
p.add(jtf);
container.add(p);
JPanelpanel=newJPanel();
finalJButtonbl=newJButton("開始");
bl.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
random=(int)(Math.random()*10+1);
bl.setEnabled(false);
}
});
panel.add(bl);
JButtonbl1=newJButton("確定");
bl1.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
Stringnum=jtf.getText().trim();
if(!num.matches("^[1-9]|10$"))
{
JOptionPane.showConfirmDialog(null,"請填入一個1~10的整數!","友情提示",JOptionPane.CLOSED_OPTION,
JOptionPane.ERROR_MESSAGE);
}
else
{
if(random==Integer.parseInt(num))
{
JOptionPane.showConfirmDialog(null,"恭喜你,猜對了!","友情提示",JOptionPane.CLOSED_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showConfirmDialog(null,"很遺憾,你猜錯了!隨機數是:"+random,"友情提示",
JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);
}
}
jtf.requestFocus();
bl.setEnabled(true);
}
});
panel.add(bl1);
JButtonbl2=newJButton("退出");
bl2.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
System.exit(0);
}
});
panel.add(bl2);
container.add(panel);
JLabeljl=newJLabel();
container.add(jl);
setVisible(true);
}
}

閱讀全文

與猜字游戲java相關的資料

熱點內容
度人經pdf 瀏覽:898
怎麼配置android遠程伺服器地址 瀏覽:956
java程序員看哪些書 瀏覽:939
什麼app可以免費和外國人聊天 瀏覽:793
pdf手寫筆 瀏覽:178
別永遠傷在童年pdf 瀏覽:986
愛上北斗星男友在哪個app上看 瀏覽:419
主力散戶派發源碼 瀏覽:669
linux如何修復伺服器時間 瀏覽:59
榮縣優途網約車app叫什麼 瀏覽:477
百姓網app截圖是什麼意思 瀏覽:226
php如何嵌入html 瀏覽:814
解壓專家怎麼傳輸 瀏覽:745
如何共享伺服器的網路連接 瀏覽:134
程序員簡易表白代碼 瀏覽:168
什麼是無線加密狗 瀏覽:64
國家反詐中心app為什麼會彈出 瀏覽:69
cad壓縮圖列印 瀏覽:104
網頁打開速度與伺服器有什麼關系 瀏覽:865
android開發技術文檔 瀏覽:65