yes, it's a random problem.obviously four courses they have the equal randomness. every course has 25 percent opportunity of becoming the one ninety-ninth.at last sum up the occurrences of them all, among 99 course objects, how many times does each appear?generally it's a 25 percent random event.
楼主不妨看看我这个程序: import java.util.Random; /** * @author 不再悲伤 * Accomplish RandomCourse.java *You have 4 classes representing different courses: *=Accounting,Economics, Finance, and Informatics.You *need to creat RandomCourse class, then: *1. You randomly create n course objects (Here n = 99); *2. You output the exact numbers for different course *objects. */ public class RandomCourse { public static void main(String[] args) { int Accounting=0,Accounting1=0; int Economics=1,Economics1=0; int Finance=2,Finance1=0; int Informatics=3,Informatics1=0; int[] b=new int[100]; Random in=new Random(4); for(int i=0;i<100;i++) {b[i]=in.nextInt(3);} for(int i=0,j=i+1;i<100;i++) { if(b[j]==b[i]){Accounting1++;} if(b[j]==b[i]){Economics1++;} if(b[j]==b[i]){Finance1++;} if(b[j]==b[i]){Informatics1++;} j++; } System.out.println("Accounting"+Accounting1); System.out.println("Economics"+Economics1); System.out.println("Finance"+Finance1); System.out.println("Informatics"+Informatics1); } }
哦,楼主上面代码有问题,下面这个是对的 import java.util.Random; /** * @author 不再悲伤 * Accomplish RandomCourse.java *You have 4 classes representing different courses: *=Accounting,Economics, Finance, and Informatics.You *need to creat RandomCourse class, then: *1. You randomly create n course objects (Here n = 99); *2. You output the exact numbers for different course *objects. */ public class RandomCourse { public static void main(String[] args) { int Accounting=0,Accounting1=0; int Economics=1,Economics1=0; int Finance=2,Finance1=0; int Informatics=3,Informatics1=0; int[] b=new int[100]; Random in=new Random(4); for(int i=0;i<100;i++) {b[i]=in.nextInt(3);} for(int i=0;i<100;i++) { if(b[i]==0){Accounting1++;} if(b[i]==1){Economics1++;} if(b[i]==2){Finance1++;} if(b[i]==3){Informatics1++;} } System.out.println("Accounting"+Accounting1); System.out.println("Economics"+Economics1); System.out.println("Finance"+Finance1); System.out.println("Informatics"+Informatics1); } }
every course has 25 percent opportunity of becoming the one ninety-ninth.at last sum up the occurrences of them all, among 99 course objects, how many times does each appear?generally it's a 25 percent random event.
仅仅是个想法,LZ自己再想想吧。
import java.util.Random;
/**
* @author 不再悲伤
* Accomplish RandomCourse.java
*You have 4 classes representing different courses:
*=Accounting,Economics, Finance, and Informatics.You
*need to creat RandomCourse class, then:
*1. You randomly create n course objects (Here n = 99);
*2. You output the exact numbers for different course
*objects.
*/
public class RandomCourse
{
public static void main(String[] args)
{
int Accounting=0,Accounting1=0;
int Economics=1,Economics1=0;
int Finance=2,Finance1=0;
int Informatics=3,Informatics1=0;
int[] b=new int[100];
Random in=new Random(4);
for(int i=0;i<100;i++)
{b[i]=in.nextInt(3);}
for(int i=0,j=i+1;i<100;i++)
{
if(b[j]==b[i]){Accounting1++;}
if(b[j]==b[i]){Economics1++;}
if(b[j]==b[i]){Finance1++;}
if(b[j]==b[i]){Informatics1++;}
j++;
}
System.out.println("Accounting"+Accounting1);
System.out.println("Economics"+Economics1);
System.out.println("Finance"+Finance1);
System.out.println("Informatics"+Informatics1);
}
}
import java.util.Random;
/**
* @author 不再悲伤
* Accomplish RandomCourse.java
*You have 4 classes representing different courses:
*=Accounting,Economics, Finance, and Informatics.You
*need to creat RandomCourse class, then:
*1. You randomly create n course objects (Here n = 99);
*2. You output the exact numbers for different course
*objects.
*/
public class RandomCourse
{
public static void main(String[] args)
{
int Accounting=0,Accounting1=0;
int Economics=1,Economics1=0;
int Finance=2,Finance1=0;
int Informatics=3,Informatics1=0;
int[] b=new int[100];
Random in=new Random(4);
for(int i=0;i<100;i++)
{b[i]=in.nextInt(3);}
for(int i=0;i<100;i++)
{
if(b[i]==0){Accounting1++;}
if(b[i]==1){Economics1++;}
if(b[i]==2){Finance1++;}
if(b[i]==3){Informatics1++;}
}
System.out.println("Accounting"+Accounting1);
System.out.println("Economics"+Economics1);
System.out.println("Finance"+Finance1);
System.out.println("Informatics"+Informatics1);
}
}
在主类Shapes里,包含了一个static方法,名为randShape()。它的作用是在每次调用它时为某个随机选择的Shape对象生成一个句柄。请注意上溯造型是在每个return语句里发生的。这个语句取得指向一个Circle,Square或者Triangle的句柄,并将其作为返回类型Shape发给方法。所以无论什么时候调用这个方法,就绝对没机会了解它的具体类型到底是什么,因为肯定会获得一个单纯的Shape句柄。