/* 创建一个整数数组,数组大小随机产生,数组里面的整数也是随机产生的
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]);
}
}

解决方案 »

  1.   

    第一题中 int 数组没有初始化大小,改为:int[] random = new int[4]; 这样就可以运行了。第二题为什么要用 100 个随机数让别人去猜,这样就算每个都猜对了都要猜 100 次,建议生成一个随机数让人家去猜就可以了。
      

  2.   

    Math.random()产生随机数
    random中文意思就有随机的意思
    第二道题 你把它运行起来 自己猜猜看体会java路还长着  :)
      

  3.   

    1:随即生成一个K int[] arr = new[k]; 就行了
    2:随即生成一个数后, 如果猜的数等于这个数就显示答对了 退出 很简单呀random = new int[XX];你会了 完全能作出来.
      

  4.   

    让电脑傻一傻也可以....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!"); }}
      

  5.   

    第一道题:数组大小和数组里的值都是随机的
        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;
                }
            }