本文目錄一覽:
java猜數字,如果猜對了,繼續猜,一共猜5次,求代碼。
import java.util.Random;
import java.util.Scanner;
public class caishu {
static int i = 0;
static int p;
public static void main(String[] args) {
// TODO Auto-generated method stub
p = (int) (Math.random() * 100);
//System.out.println(p);
while (i 5) {
try {
System.out.println(“請輸入你要猜的數:”);
Scanner scanner = new Scanner(System.in);
int s = scanner.nextInt();
if (p != s) {
if (p s) {
System.out.println(“你猜得數小了”);
} else {
System.out.println(“你猜得數大了”);
}
} else {
System.out.println(“恭喜你猜對了!”);
return;
}
i++;
} catch (Exception ex) {
System.out.println(“輸入的數據有誤!”);
}
}
System.out.println(“你的猜數次數已經用完了!”);
}
}
運行結果
用java編寫一個猜數字遊戲,
package day06;
import java.util.Scanner;
//猜字符遊戲
public class GuessingGame {
//主方法
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = 0; //猜錯的次數
char[] chs = generate(); //隨機生成的字符數組
System.out.println(chs); //作弊
while(true){ //自造死循環
System.out.println(“猜吧!”);
String str = 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
int score = chs.length*100 – count*10; //一個字符100分,錯一次減10分
System.out.println(“恭喜你猜對了,得分:” + score);
break; //猜對時跳出循環
}else{ //沒猜對
count++; //猜錯次數增1
System.out.println(“字符對:”+result[1]+”個,位置對:”+result[0]+”個”);
}
}
}
//隨機生成5個字符數組
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;ichs.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;
}
//對比隨機數組與用戶輸入的數組
public static int[] check(char[] chs,char[] input){
int[] result = new int[2];
for(int i=0;ichs.length;i++){
for(int j=0;jinput.length;j++){
if(chs[i]==input[j]){ //字符對
result[1]++; //字符對個數增1
if(i==j){ //位置對
result[0]++; //位置對個數增1
}
break;
}
}
}
return result;
}
}
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;
}
}
運行結果
java猜數字小遊戲。用eclipse寫的
import java.util.Scanner;
/**
* Java命令行版 猜數字遊戲
* @author kaifang
*/
public class GuessNum {
public static void main(String[] args)
{
System.out.println(“======猜數字遊戲======\n”);
int answer = (int)(Math.random() * 200 + 1);
Scanner sr = new Scanner(System.in);
while(true) {
System.out.print(“請輸入你猜的數字(1-200):”);
int in = sr.nextInt();
if (in answer) {
System.out.println(“猜大了!\n”);
} else if(in answer){
System.out.println(“猜小了!\n”);
} else {
System.out.println(“恭喜你,才猜對了!!!\n”);
break;
}
}
sr.close();
}
}
Java猜數字遊戲
public static void main(String[] args) {
// TODO 自動生成方法存根
System.out.println(“歡迎進入猜數字遊戲!您只有10次機會!猜的數字在0到100之間”);
Random r = new Random();
int num = r.nextInt(100);
Scanner input = new Scanner(System.in);
int cai;
for (int i = 0; i 10; i++) {
System.out.print(“輸入競猜數字:”);
cai = input.nextInt();
if (cai 0 || cai 100) {
System.out.println(“數字在0到100之間”);
continue;
}
if (cai == num) {
System.out.println(“猜中數字,勝利了”);
break;
} else {
System.out.println(“沒有猜中”);
}
if (i == 9) {
System.out.println(“時間到,競猜失敗”);
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/248805.html