读书人

请教这两道题有办法解决吗

发布时间: 2011-12-28 22:45:21 作者: rapoo

请问这两道题有办法解决吗?
//打印菱形
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]+ "次 ");
}
}
}

这道题我实在想不出,希望大家指点一二.

[解决办法]
第一题..
把一个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);
}
}

}

[解决办法]
第二个...不知道是不是这个意思
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();
}
}
[解决办法]
/**
* 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);
}

}

[解决办法]
//打印菱形 用一个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();
}
}
}

读书人网 >J2SE开发

热点推荐