黑马程序员技术交流社区
标题:
猜数字的游戏,希望有更优秀的做法。
[打印本页]
作者:
chulangren2
时间:
2014-7-21 21:25
标题:
猜数字的游戏,希望有更优秀的做法。
/*
需求说明:
1.程序开始先为用户准备10个1——1000不重复的随机整数。
2.分别让用户猜,用户猜的同时记录猜的次数,并提示所
猜的数"高了"或"低了",直到猜中为止;猜中后要打印
猜的次数,并继续下一个数。
*/
作者:
韩天雷
时间:
2014-7-21 21:37
halfShearch
作者:
icris
时间:
2014-7-21 22:00
import java.util.*;
public class Guess {
public static void main(String[] args) {
ArrayList<Integer> arr = getNums();
guess(arr);
}
private static void guess(ArrayList<Integer> arr) {
Scanner scanner = new Scanner(System.in);
int count = 1;
int g = 0;
for (Integer i : arr) {
System.out.println("输入1到1000之间的数");
g = scanner.nextInt();
while (g != i) {
if (g < i) {
System.out.println("低了");
} else {
System.out.println("高了");
}
g = scanner.nextInt();
count++;
}
System.out.println("猜中了,本次猜了" + count + "次");
}
scanner.close();
}
private static ArrayList<Integer> getNums() {
// TODO Auto-generated method stub
ArrayList<Integer> arr = new ArrayList<Integer>();
Random random = new Random();
while (arr.size() < 10) {
Integer i = random.nextInt(1000) + 1;
if (arr.indexOf(i) == -1) {
arr.add(i);
System.out.println(i);
}
}
return arr;
}
}
复制代码
作者:
chulangren2
时间:
2014-7-23 11:18
谢谢大家给的代码,学到东西了。
作者:
jwx555
时间:
2014-7-23 13:55
别告诉我是黑马考试题
作者:
meibinlove
时间:
2014-7-23 15:51
基础测试题吧
作者:
高老强
时间:
2014-7-23 16:13
挺简单一个小程序
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2