返回顶部

一缕半夏微光

温柔半两,从容一生

导航

动手动脑集合二

题目一:【求平方根】

代码如下:

 1 public class SquareInt {
 2 
 3     public static void main(String[] args) 
 4     {
 5         int result;
 6 
 7         for (int x = 1; x <= 10; x++) 
 8         {
 9             result = square(x);
10             // Math库中也提供了求平方数的方法
11             // result=(int)Math.pow(x,2);
12             System.out.println("The square of " + x + " is " + result + "\n");
13         }
14     }
15 
16     // 自定义求平方数的静态方法
17     public static int square(int y) 
18     {
19         return y * y;
20     }
21 }

 

运行截图如下:

 

题目二:【随机数】

代码如下:

 1 import javax.swing.JOptionPane;
 2 public class RandomInt {
 3        public static void main( String args[] )
 4        {
 5           int value;
 6           String output = "";
 7 
 8           for ( int i = 1; i <= 20; i++ ) {
 9              value = 1 + (int) ( Math.random() * 6 );
10              output += value + "  ";
11              
12              if ( i % 5 == 0 )
13                 output += "\n";
14           }
15 
16           JOptionPane.showMessageDialog( null, output,
17              "20 Random Numbers from 1 to 6",
18              JOptionPane.INFORMATION_MESSAGE );
19 
20           System.exit( 0 );
21        }
22 }

 

运行截图如下:

 

题目三:【利用随机数来模拟骰子滚动的统计结果】

代码如下:

 1 import javax.swing.*;
 2 public class RollDie {
 3     public static void main(String[] args) {
 4         int frequency1=0,frequency2=0,frequency3=0,frequency4=0,frequency5=0,frequency6=0,face;
 5         
 6         //summarize results
 7         for(int roll=1;roll<=6000;roll++) {
 8             face=1+(int)(Math.random()*6);
 9             
10             switch(face) {
11             case 1:
12                 ++frequency1;
13                 break;
14             case 2:
15                 ++frequency2;
16                 break;
17             case 3:
18                 ++frequency3;
19                 break;
20             case 4:
21                 ++frequency4;
22                 break;
23             case 5:
24                 ++frequency5;
25                 break;
26             case 6:
27                 ++frequency6;
28                 break;
29             }
30         }
31         
32         JTextArea outputArea=new JTextArea(7,10);
33         
34         outputArea.setText("Face\tFrequency"+"\n1\t"+frequency1+"\n2\t"+frequency2+"\n3\t"+frequency3+"\n4\t"+frequency4+"\n5\t"+frequency5+"\n6\t"+frequency6);
35         
36         JOptionPane.showMessageDialog(null, outputArea,"Rolling a Die 6000 Times",JOptionPane.INFORMATION_MESSAGE);
37         System.exit(0);
38     }
39 }

 

运行截图如下:

 

题目四:【编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数】

代码如下:

 1 import java.util.Random;
 2 import java.util.Scanner;
 3 
 4 public class Test1 {
 5     public static void main(String[] args) {
 6         Random r=new Random(System.currentTimeMillis());
 7         System.out.println("请问你需要生成多少个随机数字");
 8         Scanner scanner=new Scanner(System.in);
 9         int m=scanner.nextInt();
10         for(int i=0;i<m;i++) {
11             System.out.println(r.nextInt());
12         }
13     }
14 }

 

运行截图如下:

 

题目五:【请看以下代码,你发现了有什么特殊之处吗?】

代码如下:

 1 public class MethodOverload {
 2     public static void main(String[] args) 
 3     {
 4         System.out.println("The square of integer 7 is " + square(7));
 5         System.out.println("\nThe square of double 7.5 is " + square(7.5));
 6     }
 7 
 8     public static int square(int x) 
 9     {
10         return x * x;
11     }
12 
13     public static double square(double y) 
14     {
15         return y * y;
16     }
17 }

 

运行截图如下:

 

特殊之处:

我认为此段代码的特殊之处在于square里面的参数又是int又是double。

posted on 2020-10-09 00:33  一缕半夏微光  阅读(100)  评论(0编辑  收藏  举报