人机猜数字的java代码 人机猜数游戏的c程序设计

用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语言实现猜数字游戏java实现人机猜数字的java代码的简单猜数字游戏代码,通过随机数与逻辑判断来实现游戏功能 代码如下人机猜数字的java代码: import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { // 产生一个随机数 int n
java如何实现的简单猜数字游戏代码主要通过随机数与逻辑判断来实现游戏功能
实现代码如下:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 产生一个随机数
int number = (int) (Math.random() * 100)1;
// 加入count
int count = 0;
// 在这里加入最大值,和最小值
int max = 100;
int min = 1;
while (true) {
// 键盘录入数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要猜的数据:("min"~"max")");
try {
count;
int guessNumber = sc.nextInt();
// 判断
if (guessNumbernumber) {
max = guessNumber;
System.out.println("你猜大了");
} else if (guessNumbernumber) {
min = guessNumber;
System.out.println("你猜小了");
} else {
System.out.println("恭喜你,花了"count"次就猜中了");
// 问是否继续
System.out.println("请问还要继续吗?(yes)");
sc = new Scanner(System.in);
String str = sc.nextLine();
if ("yes".equals(str)) {
// 重写赋值随机数
number = (int) (Math.random() * 100)1;
count = 0;
max = 100;
min = 1;
} else {
break;
}
}
} catch (InputMismatchException e) {
System.out.println("你输入的数据有误");
}
}
}
简单的java猜数字代码public static void main(String [] args){
int[] num_list={1,3,5,7,9};
java.util.Scanner str=new java.util.Scanner(System.in);
int num = str.nextInt();
for(int i=0;inum_list.length;i){
if(num!=nullnum.equals("")){
if(num_list[i]==num){
System.out.println("恭喜人机猜数字的java代码你人机猜数字的java代码,猜对人机猜数字的java代码了!");
}else{
System.out.println("抱歉 , 你猜错了!");
}
}
}
}
用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(ba)
System.out.println("big");
else
if(ba)
System.out.println("small");
else
System.out.println("right");
4)
用户可根据提示信息再次输入猜测 , 直到提示信息是“猜对了” 。
while(b!=a){
if(ba)
System.out.println("big");
else
if(ba)
System.out.println("small");
System.out.println("give
me
a
new
number")
int
b
=
in.nextInt();
}
System.out.println("right");
3看懂以后,3,4可以合在一起,用4的回答.
【人机猜数字的java代码 人机猜数游戏的c程序设计】关于人机猜数字的java代码和人机猜数游戏的c程序设计的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读