JAVA练习题


●已知圆的面积Area=π*r*r,求半径r。
import javax.swing.JOptionPane;
public class T6 {
 public static void main(String args[]) {
  String s = JOptionPane.showInputDialog("请输入圆面积:");
  double Area = Double.parseDouble(s);
  double r;
  r = Math.sqrt(Area / Math.PI);
  JOptionPane.showMessageDialog(null, "该圆半径为:" + r, "半径值",
    JOptionPane.PLAIN_MESSAGE);
  System.exit(0);
 }
}

 

●使用Math类中的random()方法,模拟一个铜板在连续一百次的投掷中,出现正反面次数。
public class T7 {
 public static void main(String args[]) {
  int zm = 0;
  int fm = 0;
  for (int k = 0; k < 100; k++) {
   if (Math.random() > 0.5) {
    zm++;
   } else {
    fm++;
   }
  }
  System.out.println("反面次数为:" + fm + "/n正面次数为:" + zm);
 }
}

 

●模拟一个骰子在连续一万次的投掷中,一至六点出现次数。
import java.util.Random;
public class T8 {
 public static void main(String args[]){
  int[] t = new int[6];
       Random rand = new Random();
       for(int i=0;i<10000;i++)
       {
       int r = rand.nextInt(6) +1 ;//生成1到6的整数
       switch(r){
       case 1: t[0]++;break;
       case 2: t[1]++;break;
       case 3: t[2]++;break;
       case 4: t[3]++;break;
       case 5: t[4]++;break;
       default : t[5]++;
       }
       }      
       for(int i=0;i<t.length;i++)
       {
        System.out.println(i+1+"点出现次数:"+t[i]);
       }
     }
 }

 

●某人向银行贷款100万,以复利计算,利率8厘,若每月摊还2万元,请问几个月才可还清。
public class T12new {
 public static void main(String args[]) {
  int i = 0;
  double n = 1000000;
  do {
   n = (n * (1 + 0.008)) - 20000;
   i = i + 1;
   System.out.println(n);
  } while (n > 0);
  System.out.println("还清贷款的时间是" + i + "月");
 }
}

 

●输入一个正整数n,输出其所有因数。
import javax.swing.JOptionPane;
public class T14 {
 public static void main(String args[]) {
  String s = JOptionPane.showInputDialog("请输入一个整数:");
  int n = Integer.parseInt(s);
  System.out.print("n=" + n + ",它的因数有:");
  for (int i = 1; i <= n; i++)
   if (n % i == 0) {
    System.out.print(i);
    if (i < n)
     System.out.print(",");
    else
     System.out.print("。");
   }

 }
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值