软工第二周作业

一、求平均数

(1)源码

 1 public class SquareInt {
 2 
 3     public static void main(String[] args) {
 4         int result;
 5 
 6         for (int x = 1; x <= 10; x++) {
 7             result = square(x);
 8              9             // result=(int)Math.pow(x,2);
10             System.out.println("The square of " + x + " is " + result + "\n");
11         }
12     }
13 
14     
15     public static int square(int y) {
16         return y * y;
17     }
18 }

(2)运行结果

 

二、生成随机数

(1)源码

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

(2)运行结果

 三、使用Random类生成随机数

(1)源码

 1 import java.util.*;
 2 
 3 public class TestRandom
 4 {
 5     public static void main(String[] args) 
 6     {
 7         Random rand = new Random();
 8         System.out.println("rand.nextBoolean()" + rand.nextBoolean());
 9         byte[] buffer = new byte[16];
10         rand.nextBytes(buffer);
11         System.out.println(Arrays.toString(buffer));
12         
13         System.out.println("rand.nextDouble()" + rand.nextDouble());
14         
15         System.out.println("rand.nextFloat()" + rand.nextFloat());
16         /
17         System.out.println("rand.nextGaussian()" + rand.nextGaussian());
18         //
19         System.out.println("rand.nextInt()" + rand.nextInt());
20         
21         System.out.println("rand.nextInt(26)" + rand.nextInt(26));
22         //
23         System.out.println("rand.nextLong()" +  rand.nextLong());
24     }
25 }

(2)运行结果

 四、随机数应用案例

(1)

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

(2)运行结果

 五、参数可变的方法

(1)源码

 1 import java.awt.*;
 2 import java.awt.event.*;
 3 import java.util.*;
 4 
 5 
 6 public class VariableArgumentsTest{
 7     
 8     public static double max(double...values)
 9     {
10         double largest=Double.MIN_VALUE;
11         for (double v:values)
12             if(v>largest) largest=v;
13         return largest;
14     }
15 
16     public static void main(String args[]) {
17     
18          System.out.println("Max:"+max(1,11,300,2,3));
19             
20     }
21 }

(2)运行结果

 

六、函数重载

(1)源码

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

(2)运行结果

 

(3)总结

①有重载的方法会根据传入数据的类型来选择不同的方法

②方法重载条件

1.方法名相同

2.参数类型不同,参数个数不同,或者是参数类型的顺序不同

七、递归求n的阶乘

(1)源码

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 
 5 public class CalculateN {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         
12         Scanner scanner=new Scanner(System.in);
13         int number=scanner.nextInt();
14         System.out.println(number+"!="+calculateN2(number));
15         
16     }
17     
18     public static long calculateN(int n) {
19         if(n==1 || n==0){
20             return 1;
21         }
22         
23         return n*calculateN(n-1);
24     }
25 
26     public static BigInteger calculateN2(int n) {
27         if(n==1 || n==0){
28             return BigInteger.valueOf(1);
29         }
30         return BigInteger.valueOf(n).multiply(calculateN2((n-1)));
31     }
32 }

(2)运行结果

posted @ 2023-09-22 10:26  连师傅只会helloword  阅读(2)  评论(0编辑  收藏  举报