//打印菱形
public class lingxing {
public static void main(String[] args){
for(int i=1;i<=11;i++){
for(int j=1;j<=9-i && j<=11-2*i;j++)
System.out.print(" ");
for(int j=1;j<=i && j<=12-i;j++)
System.out.print('*');
System.out.println();
}
}
}中间要求有9个星星,并且只能用一个for循环./*5、 声明一个数组50个元素,利用随机数填充数组1-10之间的数字。 
定义一个类,将填充好的数组作为参数构造这个类的对象,
然后调用对象的方法来计算该数组中各个数字出现的机率,
方法返回字符串数组并显示出来,提示是否继续填充。。*/
public class shuzu {
public static void main(String[] args){
int r = 0;
int[] random = new int[10];
for(int i=0;i<random.length;i++){
random[r] = (int)(Math.random()*100)+1;
System.out.print(random[r]+" ");
random[i]++;
}
for(int i=0;i<10;i++){
System.out.println(r+"数出现了"+random[i]+"次");
}
}
}这道题我实在想不出,希望大家指点一二.

解决方案 »

  1.   

    第一题..
    把一个for拆两半算一个for吧...
    public class lingxing {
    public static void main(String[] args) {
    int n = 5;
    int width = 2 * n - 1;
    char[] c = new char[width];
    for (int i = 0; i < width; i++)
    c[i] = ' ';
    int big = n - 1;
    int small = n - 1;
    for (int i = 0; i < n; i++) {
    c[big] = '*';
    c[small] = '*';
    System.out.println(c);
    big++;
    small--;
    }
    for (int i = n; i < 2 * n - 1; i++) {
    big--;
    small++;
    c[big] = ' ';
    c[small] = ' ';
    System.out.println(c);
    }
    }}
      

  2.   

    第二个...不知道是不是这个意思
    import java.util.*;
    public class shuzu {
    public static void main(String[] args) {
    Random r = new Random();
    int[] ai = new int[50];
    for(int i=0; i<ai.length; i++)
    ai[i] = r.nextInt(10) + 1;
    Probability p = new Probability(ai);
    System.out.println(p.calculate());
    }
    }class Probability {
    int[] ai;
    Probability(int[] ai) {
    this.ai = ai;
    }
    public String calculate() {
    Map<Integer, Integer> m = new TreeMap<Integer, Integer>();
    for(int i=0; i<ai.length; i++) {
    if(m.containsKey(ai[i]))
    m.put(ai[i], m.get(ai[i])+1);
    else 
    m.put(ai[i], 1);
    }
    StringBuilder sb = new StringBuilder("{");
    Iterator it = m.entrySet().iterator();
    while(it.hasNext()) {
    Map.Entry me = (Map.Entry)it.next();
    sb.append("(");
    sb.append(me.getKey());
    sb.append(", ");
    double i = (Integer)me.getValue();
    sb.append(i / ai.length);
    sb.append(") ");
    }
    sb.append("}");
    return sb.toString();
    }
    }
      

  3.   

    又是算法题,呵呵!我就看见过变态用一个while 写出了九九乘法表,我想第一题应该可以实现!也许if多点!
      

  4.   

    感谢zephyr_cc() ,不过我还没有学到这么深,不过还是谢谢了,请放心我一定会给分的.
      

  5.   

    /**
     * project_name: Test
     * package_name: netsource
     * package_declaration: package netsource;
     * filename: Many_Question.java
     * author: yuhaiming
     * date: 2007-9-17
     */
    package netsource;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.math.*;
    class count {
    public int[] cur_num;
    public count(int[] num){
    this.cur_num = num;
    for(int j=0;j<50;j++){
    System.out.print(cur_num[j]);
    }
    System.out.println("");
    }
    public String dispocess(){
    for(int j=0;j<50;j++){
    System.out.print(cur_num[j]);
    }
    System.out.println("");
    String returnstr = "";
    int[] str = {0,0,0,0,0,0,0,0,0,0};
    String strerr = "";
    for(int i=0;i<50;i++){
    switch(this.cur_num[i]){
    case 0: str[0]++; break;
    case 1: str[1]++; break;
    case 2: str[2]++; break;
    case 3: str[3]++; break;
    case 4: str[4]++; break;
    case 5: str[5]++; break;
    case 6: str[6]++; break;
    case 7: str[7]++; break;
    case 8: str[8]++; break;
    case 9: str[9]++; break;
    default: strerr = "错误"; break;
    }
    }
    for(int j=0;j<10;j++){
    returnstr +=j+"出现的概率是:"+(str[j]/0.5)+"%"+"\n";
    }
    return returnstr;

    }

    }
    public class Many_Question { /**
     * @param args
     */
    public static void main(String[] args) {
    //int num = 33;
    //System.out.println(num >> 32);

    /*
    int j = 0;
    for (int i = 0; i < 100; i++) {
    j = j++;
    System.out.println(j);
    }
    System.out.println(j);
    */

    //boolean b = false?false:true == false?true:false;
    //System.out.println(b);
    /*
    List list = new ArrayList();
    list.add("Happy");
    list.add("birthday");
    list.add("to");
    list.add("you.");
    for(Iterator i = list.iterator(); i.hasNext(); ) {
    String s = (String)i.next();
    System.out.println(s);
    }
    */

    /*5、声明一个数组50个元素,利用随机数填充数组1-10之间的数字。 
    定义一个类,将填充好的数组作为参数构造这个类的对象,
    然后调用对象的方法来计算该数组中各个数字出现的机率,
    方法返回字符串数组并显示出来,提示是否继续填充。。*/
    int num[] = new int[50];
    for(int i=0;i<50;i++){
    int rannum = (int)(Math.random()*10);
    if(rannum==0){ 
    rannum = 1;
    num[i] = rannum;
    }
    else num[i] =rannum;
    }
    for(int j=0;j<50;j++){
    //System.out.println(num[j]);
    }
    count newcount = new count(num);
    String str = newcount.dispocess();
    System.out.println(str);
    }}
      

  6.   

    非常感谢sea_force() ,不过您的这个方法更难理解,因为我还没有学到这么深,不过还是谢谢.
      

  7.   

    //打印菱形 用一个for再用一个递归
    public class StringDaYing {
    public void daying(int n, String str) {
            if (n == 0) {
                return;
            } else if (n == 1) {
                System.out.print(str);
                return;
            } else {
                System.out.print(str);
                daying(n - 1, str);
            }
        }public static void main(String[] args) {
            StringDaYing sf = new StringDaYing();
            int n = -1;
            int count = 2;
            for (int i = 1; i < 10; i++) {
                if (i > 5)
                    count = -2;
                n = n + count;
                int m = (9 - n) / 2;
                sf.daying(m, " ");
                sf.daying(n, "*");
                sf.daying(m, " ");
                System.out.println();
            }
        }
    }
      

  8.   

    简化递归
    public void daying(int n, String str) {
            if (n == 0) {
                return;
            } else {
                System.out.print(str);
                daying(n - 1, str);
            }
        }
      

  9.   

    第一题,仅使用 1 个 for 的话(不用递归)挺难的,因为除了需要输出“*”,还得输出空格。zephyr_cc 的回复里好像有两个 for!
      

  10.   

    第一题:只用一个for循环:
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    for (int i = 1; i <= 11; i++) {
    if(i<6){
    System.out.print("            ".substring(0, 11-2*i));
    System.out.print("******".substring(0,i));
    }
    else
    System.out.print("******".substring(0,12-i));
    System.out.println();
    }
    }其实里面的数字可以用变量代替,勉强不算投机取巧吧。
      

  11.   

    不过,用substring取代了循环
    也算投机取巧了
      

  12.   

    冤枉啊~第一个初始化char数组可以拿个字符串代替~
    后面两个for加起来才9~
      

  13.   

    早上以为这样不算,现在看到别人贴了,我页把我的贴出来。public class lingxing { public static void main(String[] args) {
    String str="      ";//当中是6个空格,按道理该用for循环初始化的。
    int ia=str.length();
    for(int i=0;i<2*ia-1;i++){
    str+=i<ia?"*":" ";
    System.out.println(str.substring(i+1));
    }
    }
    }