请教两道问题?

bj518 2007-09-21 10:35:38
/* 创建一个整数数组,数组大小随机产生,数组里面的整数也是随机产生的
1-1000之间的整数。*/

//这道题我怎么也不知道应该如何获得随机数,希望大家多多指点,最好加点注释

public class suijishu {
public static void main(String[] args){
int[] random = new int[];
for(int i=0;i<random.length;i++){
random[i] = (int)(Math.random()*1000) + 1;
System.out.print(random[i] + " ");
}
System.out.println();
}
}

/* 编写一个猜数字的程序,先随机产生一个1-100之间的整数,接着请用户输入猜的数字,如果猜大了就显示:“大了!”,猜小了就显示“小了!”,然后继续猜,直到猜对为止。 */

// 这道题如何让它猜对就截止

mport java.util.Scanner;
public class caidaxiao {
public static void main(String[] args){
int i;
int shu;
int[] random;
do{
System.out.print("请输入数字:");
Scanner scan = new Scanner(System.in);
shu = scan.nextInt();
random = new int[100];
for(i = 0;i < random.length;i++){
random[i] = (int)(Math.random()*100) + 1;
if(shu < random[i]){
System.out.println("小了!");
break;
}
else if(shu > random[i]){
System.out.println("大了!");
break;
}
}
}while(shu == random[i]);
}
}
...全文
240 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bj518 2007-09-25
  • 打赏
  • 举报
回复
为什么没有正确答案?
chb865 2007-09-25
  • 打赏
  • 举报
回复
第一道题:数组大小和数组里的值都是随机的
int n = (int) (Math.random() * 100);
System.out.println(n);
int[] random = new int[n];
for (int i = 0; i < n; i++) {
random[i] = (int) (Math.random() * 1000) + 1;
System.out.print(random[i] + " ");
}
}

第二道题:直到你猜对为止
int count=(int) (Math.random() * 1000);
System.out.print("请输入您要猜的数字:");
for(;;){
Scanner scan = new Scanner(System.in);
int shu = scan.nextInt();
if(shu>count){
System.out.print("您猜大了,请重新输入你要猜的数字:");
continue;
}else if(shu<count){
System.out.print("您猜小了,请重新输入你要猜的数字:");
continue;
}else{
System.out.print("恭喜您,猜对了");
break;
}
}
IhaveGotYou 2007-09-23
  • 打赏
  • 举报
回复
让电脑傻一傻也可以....

package sunflowerbbs.oicp.net;

import java.io.*;

public class Guess {
public static void main(String[] args) {
int numGiveByPerson = 0;
String str;
boolean error = true;
while (error)
try {
System.out
.println("type a num,which between 1 and 1024.Then Let me have a guess!");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
str = br.readLine();
numGiveByPerson = Integer.parseInt(str);
if (numGiveByPerson >= 1 && numGiveByPerson <= 1024) {
error = false;
} else
System.out.println("the num should be between 1 and 1024!");
} catch (NumberFormatException e) {
System.out.println("invalid format!");
} catch (Exception e) {
e.printStackTrace();
}
int top = 1;
int bottom = 1024;
int mid = (top + bottom) / 2;
int s = 1;
do {
System.out.println(mid + "!big or small?");
try {
str = new BufferedReader(new InputStreamReader(System.in))
.readLine();
if (str.toLowerCase().charAt(0) == 'b')
bottom = mid - 1;
else
top = mid + 1;
mid = (top + bottom) / 2;
s = s * 2;
if (s > 1024) {
System.out.println("play a joke on me! I'll Exit!");
return;
}
} catch (Exception e) {

}
} while (mid != numGiveByPerson);
System.out.println(mid + "? HaHa...");
System.out.println("you are right.how clever you are!");

}

}
manbaum 2007-09-22
  • 打赏
  • 举报
回复
让用户输入一个数.让电脑猜
------
楼上的,你把数都输入电脑了,电脑还用猜么?它也不傻哦。哈哈!
IhaveGotYou 2007-09-22
  • 打赏
  • 举报
回复
上面写错了,是让用户输入一个数.让电脑猜
IhaveGotYou 2007-09-22
  • 打赏
  • 举报
回复
第二题改成产生一个随机数,让电脑模拟人的思维去猜.其中数据的范围是人和电脑都知道的.
用二分查找...
storm_zone 2007-09-22
  • 打赏
  • 举报
回复
1:随即生成一个K int[] arr = new[k]; 就行了
2:随即生成一个数后, 如果猜的数等于这个数就显示答对了 退出 很简单呀

random = new int[XX];你会了 完全能作出来.
bj518 2007-09-22
  • 打赏
  • 举报
回复
希望大家多多帮忙!!!
linzhanghui 2007-09-21
  • 打赏
  • 举报
回复
Math.random()产生随机数
random中文意思就有随机的意思
第二道题 你把它运行起来 自己猜猜看体会

java路还长着 :)
  • 打赏
  • 举报
回复
第一题中 int 数组没有初始化大小,改为:int[] random = new int[4]; 这样就可以运行了。

第二题为什么要用 100 个随机数让别人去猜,这样就算每个都猜对了都要猜 100 次,建议生成一个随机数让人家去猜就可以了。
bj518 2007-09-21
  • 打赏
  • 举报
回复
这两道题有这么难吗?怎么没有人帮帮忙!

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧