本文目录一览:
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("时间到,竞猜失败");
}
}
}