Chinaunix首页 | 论坛 | 博客
  • 博客访问: 566772
  • 博文数量: 52
  • 博客积分: 2673
  • 博客等级: 少校
  • 技术积分: 1432
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-24 09:55
文章分类

全部博文(52)

文章存档

2011年(2)

2010年(20)

2009年(14)

2008年(16)

分类: Java

2010-01-19 13:57:23

初学者做习题
 
6.已知圆的面积Area = ∏×r×r,求半径r。
 
//已知圆的面积,求半径r
public class Xiti4q6 {
 final float PI=3.14159f; //定义一个用于表示圆周率的常量PI
 public  void getR(){
  double r;
  double Area=60;
  double M=Area/PI;
  r=Math.sqrt(M);
  System.out.println("圆的半径是" +r);
 }
 
  
public static void main(String[] args)
{
 Xiti4q6 banjin= new Xiti4q6();
 banjin.getR();
 
}
}
----------------------------------------------------
####################################################
7.使用Math类中的random()方法,模拟一个铜板在连续一百次的投掷中,出现正反面的次数。
 
//使用Math类中的random()方法,模拟一个铜板在连续一百次的投掷重
//出现反面的次数
public class Xiti4q7 {
 int a;
 int b;
 public void up_down() {
  for (int i = 1; i <= 100; i++) {
   int x = (int) (Math.random() * 10);
   {
    if ((x % 2) == 0)
     a = a + 1;
    else
     b = b + 1;
   }
//   System.out.println("suijishu是:" + x);
  }
  System.out.println("反面的次数是:" + b);
  System.out.println("正面的次数是:" + a);
 }
 public static void main(String[] args) {
  Xiti4q7 face_back = new Xiti4q7();
  face_back.up_down();
 }
}
-------------------------------------------
#############################################
8.模拟一个骰子在连续一万次的投掷中,一至六点出现的次数。
  此题如果有更好的方法请大侠贴出来
//模拟一个骰子在连续一万次的投掷中一至六点出现的次数
public class Xiti4q8 {
 public void shaizi() {
  int a = 0;
  int b = 0;
  int c = 0;
  int d = 0;
  int e = 0;
  int f = 0;
  for (int i = 1; i <= 10000; i++) {
   int x = (int) (Math.random() * 6+1);
   switch(x)
   {
   case 1: a=a+1; break;
   case 2: b=b+1; break;
   case 3: c=c+1; break;
   case 4: d=d+1; break;
   case 5: e=e+1; break;
   case 6: f=f+1; break;
   }
  }
// int s=a+b+c+d+e+f;
  System.out.println("1出现的次数是:" + a);
  System.out.println("2出现的次数是:" + b);
  System.out.println("3出现的次数是:" + c);
  System.out.println("4出现的次数是:" + d);
  System.out.println("5出现的次数是:" + e);
  System.out.println("6出现的次数是:" + f);
  System.out.println("zong出现的次数是:" + (a+b+c+d+e+f));
 }
 public static void main(String[] args){
  Xiti4q8 cishu = new Xiti4q8();
  cishu.shaizi();
 }
}
阅读(1550) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~