114培訓網(wǎng)歡迎您來到游戲設計交流中心!

400-850-8622

全國統(tǒng)一學習專線 9:00-21:00

用java寫猜字母的小游戲;隨機生成幾個字母,玩家輸入幾個數(shù)與隨機生成的數(shù)進行比較。

這個行不 ????
package zhouhu12;
import java.util.Arrays;
import java.util.Scanner;
/**
* 1.難度選項選擇;
* 2.隨機生成相應數(shù)量的字符,并裝入數(shù)組;
* 3。接受控制臺輸入的字符并加以比較。輸出比較的結果。
* 轉(zhuǎn)換為大寫字母 轉(zhuǎn)換為小寫。
*/
public class GuessABC {
public static void main(String[] args) {
int num;
do{
guessGame(generate());
System.out.println("1. 重新開始游戲,2.退出游戲");
Scanner sc=new Scanner(System.in);
num=sc.nextInt();
}while(num==1);
System.out.print("游戲結束,歡迎下次來玩。");
}
public static char[] generate(){
Scanner sc=new Scanner(System.in);
int g;
do{
System.out.println("歡迎進入猜字母游戲,請選擇游戲難度等級:1.初級 2.中級 3.高級");
g=sc.nextInt();
}while(g<1 || g>4);
char arr[]=new char[g];
for(int i=0;i<g;i++){
arr[i]=(char)(int)(Math.random()*26+97);
}
return arr;
}
public static void guessGame(char arr[]){
int count=0;
char arr1[]=new char[arr.length];
do{
System.out.println("請輸入"+arr.length+"個字母:(輸入 0 退出)");
Scanner sca=new Scanner(System.in);
String A=sca.next();
System.out.println(A);
char arr2[]=A.();
arr1=Arrays.copyOf(arr2,3);
if(arr1[0]=='0')
break;
System.out.print("答案:");
for(int i=0;i<arr.length;i++){
if(arr[i]==arr1[i]){
count++;
System.out.print(arr[i]);
}
else{
System.out.print("*");
}
}
System.out.println();
if(count==arr.length){
System.out.println("恭喜你猜對了");
break;
}
else{
System.out.println("猜對"+count+"個,繼續(xù)加油。");
}
count=0;
}while(arr1[0]!='0');
}
}

如何用Java語言實現(xiàn)猜數(shù)字游戲

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

用java編寫一個猜數(shù)字游戲,

package?day06;
import?java.util.Scanner;
//猜字符游戲
public?class??{
//主方法
public?static?void?main(String[]?args)?{
Scanner?scan?=?new?Scanner(System.in);
int?count?=?0;?//猜錯的次數(shù)
char[]?chs?=?generate();?//隨機生成的字符數(shù)組
System.out.println(chs);?//作弊
while(true){?//自造死循環(huán)
System.out.println("猜吧!");
String?str?=?scan.next().();?//獲取用戶輸入的字符串
if(str.equals("EXIT")){?//判斷str是否是EXIT
System.out.println("下次再來吧!");
break;
}
char[]?input?=?str.();?//將字符串轉(zhuǎn)換為字符數(shù)組
int[]?result?=?check(chs,input);??//對比
if(result[0]==chs.length){?//位置對為5
int?score?=?chs.length*100?-?count*10;?//一個字符100分,錯一次減10分
System.out.println("恭喜你猜對了,得分:"?+?score);
break;?//猜對時跳出循環(huán)
}else{?//沒猜對
count++;?//猜錯次數(shù)增1
System.out.println("字符對:"+result[1]+"個,位置對:"+result[0]+"個");
}
}
}
//隨機生成5個字符數(shù)組
public?static?char[]?generate(){
char[]?chs?=?new?char[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?=?new?boolean[letters.length];?//1.
for(int?i=0;i<chs.length;i++){
int?index;
do{
index?=?(int)(Math.random()*letters.length);?//0到25
}while(flags[index]==true);?//2.
chs[i]?=?letters[index];
flags[index]?=?true;?//3.
}
return?chs;
}
//對比隨機數(shù)組與用戶輸入的數(shù)組
public?static?int[]?check(char[]?chs,char[]?input){
int[]?result?=?new?int[2];
for(int?i=0;i<chs.length;i++){
for(int?j=0;j<input.length;j++){
if(chs[i]==input[j]){?//字符對
result[1]++;?//字符對個數(shù)增1
if(i==j){?//位置對
result[0]++;?//位置對個數(shù)增1
}
break;
}
}
}
return?result;
}
}

用Java編程實現(xiàn)一個猜數(shù)字的游戲:系統(tǒng)隨機產(chǎn)生一個1~100的數(shù)字,然后讓玩家猜測這個數(shù)字,如果玩家猜錯,

1.int num = (int)(Math.random()*100+1);//隨機得出一個1~100的數(shù)
2.用一個變量去接收玩家輸入的數(shù),例如int i;
3.用if語句,當玩家輸入的數(shù)與隨機數(shù)相等時(i==num),則輸出語句(恭喜你猜對了).
4.用else寫出猜錯是的語句.
大致思路就是這樣.建議你自己去寫,只有自己寫過才會懂.如有不懂的M我,本人也在學習中,大家可以相互討論討論.另外你可以把數(shù)字設置成1~10;這樣的話比較容易出現(xiàn)猜對的情況,方便你驗證程序是否正確,然后再改成1~100;
*在說下Math.random(),這個方法得到是一個0~1之間的隨機小數(shù)(不包括1但包括0),所以(0~1)*100=0~100(但是不包括100),然后+1就得出1~100了;因為Math.random()是個小數(shù)所以用int強轉(zhuǎn).

JAVAWEB猜數(shù)字游戲1到100(這是老師給的思路*按照老師給的思路來)

public class Test {
public static void main(String[] args) {

int b = (int) (Math.random()*100 + 1);
while (true) {
try {
Scanner in=new Scanner(System.in);
int a=in.nextInt();
if(a < 1 || a > 100) {
System.out.println("范圍不合法");
} else if(a > b) {
System.out.println(a + "太大");
} else if (a < b) {
System.out.println(a + "太小");
} else {
System.out.println("你終于猜對了");
break;
}
} catch(Exception e) {
System.out.println("數(shù)字格式不合法");
continue;
}
}
}
}

JAVA猜字母游戲

public?class??{
????public?static?char[]?generate(int?level){
???? 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'};
???? char[]?chs?=?new?char[level];
???? boolean[]?flag??=?new?boolean[26];
???? for(int?i=0;i<chs.length;i++){
???? int?index;
???? do{
???? index?=?(int)(Math.random()*26);
???? }while(flag[index]);
???? chs[i]?=?letters[index];
???? flag[index]?=?true;
???? }
???? return?chs;
????}
????public?static?int[]?check(char[]?chs,char[]?input){
???? int[]?result=?new?int[2];
???? for(int?i=0;i<chs.length;i++){
???? for(int?j=0;j<input.length;j++){
???? if(chs[i]==input[j]){
???? result[0]++;
???? if(i==j){
???? result[1]++;
???? }
???? break;
???? }
???? }
???? }
???? return?result;
????}
public?static?void?main(String[]?args)?{
int?level;
int?score;
int?count=0;
Scanner?scanner?=?new?Scanner(System.in);
System.out.println("請選擇游戲等級");
level?=?scanner.nextInt();
????????char[]?chs?=?generate(level);
????????System.out.println(Arrays.toString(chs)+"n猜吧");
????????while(true){
???????? String?inputStr?=?scanner.next().trim().();
???????? char[]?input?=?inputStr.();
???????? do?{
???????? ?System.out.println("個數(shù)不對,請重新輸入");
???????? ?inputStr?=?scanner.next().trim().();
????????????? ?input?=?inputStr.();
???????? ?}while(input.length!=level);
???????? int[]?result?=?check(chs,input);
???????? if("EXIT".equals(inputStr)){
???????? System.out.println("謝謝");
???????? break;
???????? }
???????? if(result[1]==level){
???????? score?=?500-count*10;
???????? System.out.println("恭喜全對!得分"+score);
???????? break;
???????? }else{
???????? count++;
???????? System.out.println("總次數(shù)"+count+"猜對字母個數(shù)"+result[0]+"猜對位置個數(shù)"+result[1]);
???????? }
????????
????????}
}
}

用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 ----開始猜數(shù)");
System.out.println("2 ----游戲參數(shù)設置");
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 = false;
System.out.println("請選擇游戲難度!");
System.out.println("1 ----難(4次)");
System.out.println("2 ----一般(6次)");
System.out.println("3 ----容易(8次)");
while (!) {
String s = in.next();
if (s.matches("[123]")) {
switch (Integer.parseInt(s)) {
case 1:
guessTime = 4;
= true;
break;
case 2:
guessTime = 6;
= true;
break;
case 3:
guessTime = 8;
= 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("你輸入的數(shù)字大了!");
} else {
System.out.println("你輸入的數(shù)字小了!");
}
} catch ( e) {
System.out.println("你輸入的數(shù)據(jù)超過Integer的*范圍!");
continue;
}
} else {
System.out.println("你輸入的數(shù)據(jù)不合法!");
continue;
}
}
if (getIt) {
System.out.println("恭喜你猜對了,你的戰(zhàn)斗力是" + Math.round((1 - count * 1.0 / guessTime) * 100) + "%");
} else {
System.out.println("超過次數(shù),尚需努力");
}
in.nextLine();
in.nextLine();
}
}

}
}

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

界面類:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame , {
private static final long = 1L;
private JLabel lb1;
private JButton btnEnter;
private JButton btnCancel;
private JTextArea taDispaly;
private 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.();
this.(JFrame.EXIT_ON_CLOSE);
Container con = this.();
this.(con);
con.setLayout(null);
con.setSize(467, 237);
lb1 = new JLabel("請輸入一個4位數(shù):");
con.add(lb1);
lb1.setBounds(44, 24, 140, 17);
txtEnter = new ();
con.add(txtEnter);
txtEnter.setBounds(47, 64, 76, 24);
txtEnter.(this);
taDispaly = new JTextArea();
con.add(taDispaly);
taDispaly.setBounds(249, 9, 225, 203);
taDispaly.setBorder(.(1));
btnEnter = new JButton();
btnEnter.(false);
con.add(btnEnter);
btnEnter.setText("確定");
btnEnter.setBounds(30, 105, 60, 24);
btnEnter.(this);
btnCancel = new JButton();
con.add(btnCancel);
btnCancel.setText("取消");
btnCancel.setBounds(107, 105, 60, 24);
btnCancel.(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.(true);
}
public void ( e) {
JButton btn = (JButton) e.getSource();
if (btn == btnCancel) {
btnEnter.(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] + "Bn");
} else if (oprator.getCount() == 11) {
taDispaly.append("失敗!!n");
}
btnEnter.(false);
txtEnter.setText("");
}
}
if(btn == btnEnd){
this.dispose();
}
if(btn == btnReplay){
oprator.();
taDispaly.setText("");
txtEnter.setText("");
txtEnter.(false);
}
}
public void (KeyEvent e) {
}
public void (KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
/** 實現(xiàn)只能輸入4位數(shù)字,并且數(shù)字不能重復 */
text = () e.getSource();
tx = text.getText();
int b = e.();
if (b < 48 || b > 57) {
e.('