當前位置:首頁 » 課程大全 » 猜字游戲java課程設計

猜字游戲java課程設計

發布時間: 2021-02-26 02:04:38

A. java課程設計程序-猜數游戲

Newload()
{
jf1=new JFrame("猜數游戲");
jf2=new JFrame("猜數游戲");
jf3=new JFrame("猜數游戲");
jf1_title=new JLabel("猜數游戲-歡迎進入");
jf1_title.setFont(new Font("仿宋體",Font.BOLD,40));//設置字體大小,及文字字體
jf1_title.setHorizontalAlignment(JLabel.CENTER);
JLabel jf2title=new JLabel("猜數游戲");
jf2title.setFont(new Font("仿宋體",Font.BOLD,40));//設置字體大小,及文字字體
jf2title.setHorizontalAlignment(JLabel.CENTER);
jf1_username=new JLabel("用戶名");
jf1_userpass=new JLabel("密碼");
jf2_question=new JLabel("There is question which needs you to guess!");
jf2_question.setFont(new Font("仿宋體",Font.BOLD,20));//設置字體大小,及文字字體
jf2_question.setHorizontalAlignment(JLabel.CENTER);
jf2_rightface=new JLabel(iron1);
jf2_wrongface=new JLabel(iron2);
jf2_rightface.setVisible(false);
jf2_wrongface.setVisible(false);
jf2_reelresult=new JLabel();
jf3_pinyu=new JLabel("your result is");
jf1_usernameT=new JTextField(6);
jf2_anwser=new JTextField(6);
jf2_anwser.addActionListener(this);
jf1_password=new JPasswordField(6);
jf1_password.addActionListener(this);
jf1_ok=new JButton("確定");
jf1_ok.addActionListener(this);
jf1_quit=new JButton("退出");
jf1_quit.addActionListener(this);
jf2_newgame=new JButton("新游戲(k)");
jf2_newgame.setMnemonic(KeyEvent.VK_K);
jf2_newgame.addActionListener(this);
jf2_ok=new JButton("確定");
jf2_ok.addActionListener(this);
jf1.setLayout(new BorderLayout());
jf2.setLayout(new BorderLayout());
JPanel jf1p1=new JPanel(),jf2p1=new JPanel(),jf2p2=new JPanel(),jf2p3=new JPanel();
jf2p1.setLayout(new BorderLayout());
jf1p1.setLayout(new FlowLayout());
jf2p2.setLayout(new FlowLayout());
jf2p3.setLayout(new FlowLayout());
jf1.add(jf1_title,"Center");
jf1p1.add(jf1_username);jf1p1.add(jf1_usernameT);
jf1p1.add(jf1_userpass);jf1p1.add(jf1_password);
jf1p1.add(jf1_ok);jf1p1.add(jf1_quit);
jf1.add(jf1p1,"South");
jf2p2.add(jf2_rightface);
jf2p2.add(jf2_wrongface);
jf2p2.add(jf2_reelresult);
jf2p1.add(jf2p2,"South");
jf2p1.add(jf2_question);
jf2.add(jf2title,"North");
jf2.add(jf2p1,"Center");
jf2p3.add(jf2_ans);jf2p3.add(jf2_anwser);jf2p3.add(jf2_ok);jf2p3.add(jf2_newgame);
jf2.add(jf2p3,"South");
jf3.add(jf3_pinyu);
jf1.setSize(700,400);
jf2.setSize(700,400);
jf3.setSize(700,400);
jf1.setLocation(300,150);
jf2.setLocation(300,150);
jf3.setLocation(300,150);
jf1.setVisible(true);
jf2.setVisible(false);
jf3.setVisible(false);
jf1.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jf2.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {

if(e.getSource()==jf1_ok||e.getSource()==jf1_password)
{char[] a=jf1_password.getPassword();String paas="";
for(int i=0;i<a.length;i++)//JPasswordField是一種特殊的類只能得到char數組,將其轉成String
paas=paas+a[i];
if(jf1_usernameT.getText().equals("user")&&paas.equals("pass"))
{jf2.setVisible(true);jf1.setVisible(false);
number=returnquestion();
jf2_anwser.requestFocus();
}
else
JOptionPane.showMessageDialog(null,"用戶名不正確或密碼錯誤!");
}
if(e.getSource()==jf1_quit)
{
System.exit(0);
}
if(e.getSource()==jf2_ok||e.getSource()==jf2_anwser)
{
if(times<=5){
if(Integer.parseInt(jf2_anwser.getText())==number)
{
jf2_rightface.setVisible(true);
jf2_wrongface.setVisible(false);
jf2_reelresult.setText("you are right! and your have used "+times+" times!"
+((times<=3)?"very good!":"pleas do more work for it"));
}
else
if(Integer.parseInt(jf2_anwser.getText())>number)
{times++;
jf2_wrongface.setVisible(true);
jf2_rightface.setVisible(false);
jf2_reelresult.setText("your answer is bigger than the one proced by computer!"
+"and your have used "+times+" times!");
}
else
if(Integer.parseInt(jf2_anwser.getText())<number)
{times++;
jf2_wrongface.setVisible(true);
jf2_rightface.setVisible(false);
jf2_reelresult.setText("your answer is smaller than the one proced by computer!"
+"and your have used "+times+" times!");
}
}
else
{JOptionPane.showMessageDialog(null,"你已經超過六次了,請重新開始吧!");}
jf2_anwser.requestFocus();
jf2_anwser.setText("");

}
if(e.getSource()==jf2_newgame)
{
number=returnquestion();
times=0;
jf2_rightface.setVisible(false);
jf2_wrongface.setVisible(false);
jf2_anwser.setText("");
jf2_reelresult.setText("");
jf2_anwser.requestFocus();
}

}

public static void main(String args[])
{
new Newload();
}
int returnquestion()
{
double db=Math.random()*100;
return (int)db;
}
}

B. 用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是否是
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;
}
}

C. java猜字游戲課程設計報告

你給多少分?

D. 用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("大了");

}

}

(4)猜字游戲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() 括弧中的條件表達式,當用戶猜測的數和系統生成的數字不相等時,就需要繼續循環。

E. 急求.......用java編寫一個猜字游戲 要代碼

隨機數函數用來生成數。
條件判斷方法直接放到按鈕事件里就可以了

F. 用java怎麼編寫猜字游戲

用隨機函數再加上一點邏輯應該可以實現,至於存儲,可以用Reader..不懂的慢慢查api吧...

G. 用java編寫一個猜字游戲程序,答案不限。急!!!

publicstaticvoidmain(String[]args){

Randomr=newRandom();
Scannersc=newScanner(System.in);
//100以內
intnum=r.nextInt(100);
System.out.println(num);
intgsNum=-1;
do{
System.out.println("請輸入要猜的數字:");
gsNum=sc.nextInt();
if(num>gsNum){
System.out.println("太小啦");
}elseif(num<gsNum){
System.out.println("太大啦");
}
}while(num!=gsNum);
System.out.println("正確");
}

H. java課程設計——猜數字游戲

代碼如下:

importjava.util.Scanner;

publicclassRandomT{
intsum=0;
publicstaticvoidmain(String[]args){
intnumber=(int)(Math.random()*100+1);
inttemp=number;
System.out.println("請猜一個100以內的數:");
RandomTrt=newRandomT();
intin=rt.Sn();
Booleanyn=rt.Compare(temp,in);
while(!yn){
System.out.println("請重新輸入:");
intin1=rt.Sn();
yn=rt.Compare(temp,in1);
}
System.out.println("正確數字是:"+number);
}
publicBooleanCompare(inttemp,intnumber){
Booleanyesno=null;
if(temp==number){
if(sum==1){
System.out.println("Beautiful");
}elseif(2<=sum||sum<=4){
System.out.println("還是不錯的!");
}else{
System.out.println("唉,總算對了!");
}
yesno=true;
}elseif(number<temp){
System.out.println("猜小了");
yesno=false;
}elseif(number>temp){
System.out.println("猜大了");
yesno=false;
}
returnyesno;
}
publicintSn(){
Scannersc=newScanner(System.in);
intin=sc.nextInt();
sum+=1;
returnin;
}
}

如果滿意請採納!

I. 用java編寫一個猜字游戲程序,答案不限

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
int guessTime = 8;
boolean restart = true;
while (restart) {
System.out.println("請選擇下列選項!");
System.out.println("1 ----開始猜數");
System.out.println("2 ----游戲參數設置");
System.out.println("9 ----退出");
boolean flag = true;
String choose = null;
Scanner in = null;
while (flag) {
in = new Scanner(System.in);
choose = in.next();
if (!choose.matches("[129]")) {
System.out.println("輸入錯誤,請重新輸入");
continue;
} else {
break;
}
}
if ("2".equals(choose)) {
boolean chooseLevel = false;
System.out.println("請選擇游戲難度!");
System.out.println("1 ----難(4次)");
System.out.println("2 ----一般(6次)");
System.out.println("3 ----容易(8次)");
while (!chooseLevel) {
String s = in.next();
if (s.matches("[123]")) {
switch (Integer.parseInt(s)) {
case 1:
guessTime = 4;
chooseLevel = true;
break;
case 2:
guessTime = 6;
chooseLevel = true;
break;
case 3:
guessTime = 8;
chooseLevel = true;
break;
default:
System.out.println("你輸入的選擇不存在(請輸入1、2、3)");
break;
}

} else {
System.out.println("你輸入的選擇不存在(請輸入1、2、3)");
}
}
continue;
}
if ("9".equals(choose)) {
System.exit(0);
}
if ("1".equals(choose)) {
String number = String.valueOf(Math.round(Math.random() * 100) + 1);
System.out.println(number);

boolean getIt = false;
int count = 0;
while (!getIt && ++count <= guessTime) {
System.out.println("請輸入你猜的值(1-100)");
String s = in.next();
if (s.matches("\\d+")) {
try {
if (Integer.parseInt(s) == Integer.parseInt(number)) {
getIt = true;
break;
} else if (Integer.parseInt(s) > Integer.parseInt(number)) {
System.out.println("你輸入的數字大了!");
} else {
System.out.println("你輸入的數字小了!");
}
} catch (NumberFormatException e) {
System.out.println("你輸入的數據超過Integer的最大范圍!");
continue;
}
} else {
System.out.println("你輸入的數據不合法!");
continue;
}
}
if (getIt) {
System.out.println("恭喜你猜對了,你的戰鬥力是" + Math.round((1 - count * 1.0 / guessTime) * 100) + "%");
} else {
System.out.println("超過次數,尚需努力");
}
in.nextLine();
in.nextLine();
}
}

}
}

J. 用java編寫猜字游戲

關鍵是給提示,
首先你給定一個值,

邏輯是輸入的值,如果大了,提示猜大了;
如果輸入的值是小了就提示猜小了,

熱點內容
武漢大學學生會輔導員寄語 發布:2021-03-16 21:44:16 瀏覽:612
七年級學生作文輔導學案 發布:2021-03-16 21:42:09 瀏覽:1
不屑弟高考成績 發布:2021-03-16 21:40:59 瀏覽:754
大學畢業證會有成績單 發布:2021-03-16 21:40:07 瀏覽:756
2017信陽學院輔導員招聘名單 發布:2021-03-16 21:40:02 瀏覽:800
查詢重慶2018中考成績查詢 發布:2021-03-16 21:39:58 瀏覽:21
結業考試成績怎麼查詢 發布:2021-03-16 21:28:40 瀏覽:679
14中醫醫師資格筆試考試成績查分 發布:2021-03-16 21:28:39 瀏覽:655
名著賞析課程標准 發布:2021-03-16 21:27:57 瀏覽:881
北京大學商業領袖高端培訓課程 發布:2021-03-16 21:27:41 瀏覽:919