
import javaio;
import javautilRandom;
public class test3 {
public static void main(String args[]) throws javaioIOException {
int randNum = 0;
int inputNum = 0;
int counta = 0; // 记录第一个人的猜对次数
int countb = 0; // 记录第二个人的猜对次数
Boolean flaga = true; // 判断第一个人是否答对
Boolean flagb = true; // 判断第二个人是否答对
Random rand = new Random();
randNum = randnextInt(100); //为第一个人设置随机数
InputStreamReader aaa = new InputStreamReader(Systemin);
BufferedReader dis = new BufferedReader(aaa);
Systemoutprint("现在轮到第一个人猜:\n");
while (flaga) {
Systemoutprint("输入0到100之间的数字(例如:45):");
inputNum = IntegerparseInt(disreadLine());
if (inputNum >= 0 && inputNum <= 100) {
counta = counta + 1;
if (inputNum == randNum) {
flaga = false;
check(counta);
}
else if (inputNum < randNum) {
Systemoutprintln(inputNum + "结果小了");
} else {
Systemoutprintln(inputNum + "结果大了");
}
} else {
Systemoutprintln("错误");
}
}
randNum = randnextInt(100); //为第二个人设置随机数
Systemoutprint("现在轮到第二个人猜:\n");
while (flagb) {
Systemoutprint("输入0到100之间的数字(例如:45):");
inputNum = IntegerparseInt(disreadLine());
if (inputNum >= 0 && inputNum <= 100) {
countb = countb + 1;
if (inputNum == randNum) {
flagb = false;
check(countb);
}
else if (inputNum < randNum) {
Systemoutprintln(inputNum + "结果小了");
} else {
Systemoutprintln(inputNum + "结果大了");
}
} else {
Systemoutprintln("错误");
}
}
Systemoutprintln("第一个人使用了" +counta + "次猜对");
Systemoutprintln("第二个人使用了" +countb + "次猜对");
if(counta<countb) Systemoutprintln("第一个人获胜");
if(counta==countb) Systemoutprintln("平手");
if(counta>countb) Systemoutprintln("第二个人获胜");
}
public static void check(int count) {
Systemoutprintln(count + "使用次数");
if (count < 3) {
Systemoutprintln("very lucky day");
} else if (count < 5) {
Systemoutprintln("excellent day");
} else if (count < 7) {
Systemoutprintln("good day");
} else if (count < 10) {
Systemoutprintln("so so day");
} else if (count > 10) {
Systemoutprintln("bad day");
}
}
}
public static void main(String[] args) {
// TODO 自动生成方法存根
Systemoutprintln("欢迎进入猜数字游戏!您只有10次机会!猜的数字在0到100之间");
Random r = new Random();
int num = rnextInt(100);
Scanner input = new Scanner(Systemin);
int cai;
for (int i = 0; i < 10; i++) {
Systemoutprint("输入竞猜数字:");
cai = inputnextInt();
if (cai < 0 || cai > 100) {
Systemoutprintln("数字在0到100之间");
continue;
}
if (cai == num) {
Systemoutprintln("猜中数字,胜利了");
break;
} else {
Systemoutprintln("没有猜中");
}
if (i == 9) {
Systemoutprintln("时间到,竞猜失败");
}
}
}
package comzuxiacgguest;
import javautilRandom;
import javautilScanner;
/
猜数游戏 系统自动生成4个0-9的不重复数 用户猜 数字和系统生成的数是一样且位置相同就在数那个位置输出a,数相同但位置不同,则在数那个位置输出b
其他数字不变
@author student
/
public class test {
/
产生不重复的随机数
@return 一个数组
/
public int[] rand() {
int[] array = new int[4];
Random rd = new Random();
for (int i = 0; i < 4; i++) {
array[i] = rdnextInt(10);
for (int j = 0; j < i; j++) {
if (array[j] == array[i]) {
i--;
break;
}
}
}
return array;
}
/
对产生的随机数组和用户的输入进行比较
对于数组位置和数字都一样的用‘a’代替,对于数组位置不同的但数字一样的用‘b’代替,并统计‘a’,‘b’的数量
@param array
@return 一个字符串
/
public String get(int[] array) {
for (int i = 0; i < 4; i++) {
Systemoutprintln(array[i]); //输出系统生成的随机数
}
int[] list = new int[4];
Scanner scan = new Scanner(Systemin);
Systemoutprint("请输入:");
for (int i = 0; i < 4; i++) {
list[i] = scannextInt();
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (array[i] == list[j]) {
if (i == j) {
list[j] = 'a';
} else
list[j] = 'b';
}
}
}
int m = 0;
int n = 0;
for (int i = 0; i < 4; i++) {
if (list[i] == 'a') {
m++;
} else if (list[i] == 'b') {
n++;
}
}
n = m + n;
Systemoutprint("\n" + m + "A" + n + "B\n");
return m + "A" + n + "B";
}
public static void main(String[] args) {
test test = new test();
int[] array = testrand();
if (testget(array)equals("4A4B")) {
} else {
while (!testget(array)equals("4A4B")) {
testget(array);
}
}
}
}
以上就是关于关于编写java 猜数字程序(2人用)的问题,高手请进全部的内容,包括:关于编写java 猜数字程序(2人用)的问题,高手请进、Java猜数字游戏、求教Java达人:用java编写一个猜数字游戏等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)