9.22 课后作业
- CalculateN
源代码:
import java.math.BigInteger;
import java.util.Scanner;
public class CalculateN {
/**
* @param args
*/
public static void main(String[] args) {
System.out.print("������N��");
Scanner scanner=new Scanner(System.in);
int number=scanner.nextInt();
System.out.println(number+"!="+calculateN2(number));
}
public static long calculateN(int n) {
if(n==1 || n==0){
return 1;
}
return n*calculateN(n-1);
}
public static BigInteger calculateN2(int n) {
if(n==1 || n==0){
return BigInteger.valueOf(1);
}
return BigInteger.valueOf(n).multiply(calculateN2((n-1)));
}
}
运行结果:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=5401:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 CalculateN
������N��
5
5!=120
Process finished with exit code 0
- CompareFloatNumber:
源代码:
public class CompareFloatNumber {
/**
* @param args
*/
public static void main(String[] args) {
//compare();
compare2();
}
private static void compare() {
double i = 0.0001;
double j = 0.00010000000000000001;
System.out.println(i==j); //�����true
}
private static void compare2() {
double i = 0.0001;
double j = 0.00010000000000000001;
if(Math.abs(i-j)<1e-10){
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
运行结果:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=5629:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 CompareFloatNumber
true
Process finished with exit code 0
- MethodOverload
源代码:
// MethodOverload.java
// Using overloaded methods
public class MethodOverload {
public static void main(String[] args) {
System.out.println("The square of integer 7 is " + square(7));
System.out.println("\nThe square of double 7.5 is " + square(7.5));
}
public static int square(int x) {
return x * x;
}
public static double square(double y) {
return y * y;
}
}
运行结果:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=5715:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 MethodOverload
The square of integer 7 is 49
The square of double 7.5 is 56.25
Process finished with exit code 0
- RandomInt
源代码:
// RandomInt.java
// Shifted, scaled random integers
import javax.swing.JOptionPane;
public class RandomInt {
public static void main( String args[] )
{
int value;
String output = "";
for ( int i = 1; i <= 20; i++ ) {
value = 1 + (int) ( Math.random() * 6 );
output += value + "
";
if ( i % 5 == 0 )
output += "\n";
}
JOptionPane.showMessageDialog(
null, output,
"20 Random Numbers from 1 to 6",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
运行结果:
- RollDie
源代码:
// RollDie.java
// Roll a six-sided die 6000 times
import javax.swing.*;
public class RollDie {
public static void main( String args[] )
{
int frequency1 = 0, frequency2 = 0,
frequency3 = 0, frequency4 = 0,
frequency5 = 0, frequency6 = 0, face;
// summarize results
for ( int roll = 1; roll <= 6000; roll++ ) {
face = 1 + (int) ( Math.random() * 6 );
switch ( face ) {
case 1:
++frequency1;
break;
case 2:
++frequency2;
break;
case 3:
++frequency3;
break;
case 4:
++frequency4;
break;
case 5:
++frequency5;
break;
case 6:
++frequency6;
break;
}
}
JTextArea outputArea = new JTextArea( 7, 10 );
outputArea.setText(
"Face\tFrequency" +
"\n1\t" + frequency1 +
"\n2\t" + frequency2 +
"\n3\t" + frequency3 +
"\n4\t" + frequency4 +
"\n5\t" + frequency5 +
"\n6\t" + frequency6 );
JOptionPane.showMessageDialog( null, outputArea,
"Rolling a Die 6000 Times",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
运行结果:
- SquareInt
源代码:
public class SquareInt {
public static void main(String[] args) {
int result;
for (int x = 1; x <= 10; x++) {
result = square(x);
// Math����Ҳ�ṩ����ƽ�����ķ���
// result=(int)Math.pow(x,2);
System.out.println("The square of " + x + " is " + result + "\n");
}
}
// �Զ�����ƽ�����ľ�̬����
public static int square(int y) {
return y * y;
}
}
运行结果:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=6029:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 SquareInt
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49
The square of 8 is 64
The square of 9 is 81
The square of 10 is 100
Process finished with exit code 0
- TestMath
源代码:
public class TestMath {
public static void main(String[] args) {
int result;
for (int x = 1; x <= 10; x++) {
result = square(x);
// Math����Ҳ�ṩ����ƽ�����ķ���
// result=(int)Math.pow(x,2);
System.out.println("The square of " + x + " is " + result + "\n");
}
}
// �Զ�����ƽ�����ľ�̬����
public static int square(int y) {
return y * y;
}
}
运行结果:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=6096:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 TestMath
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49
The square of 8 is 64
The square of 9 is 81
The square of 10 is 100
Process finished with exit code 0
- TestRandom:
源代码:
import java.util.*;
public class TestRandom
{
public static void main(String[] args)
{
Random rand = new Random();
System.out.println("rand.nextBoolean()��" +
rand.nextBoolean());
byte[] buffer = new byte[16];
rand.nextBytes(buffer);
System.out.println(Arrays.toString(buffer));
//����0.0~1.0֮���α���double��
System.out.println("rand.nextDouble()��" +
rand.nextDouble());
//����0.0~1.0֮���α���float��
System.out.println("rand.nextFloat()��" +
rand.nextFloat());
//����ƽ��ֵ�� 0.0�������� 1.0��α��˹��
System.out.println("rand.nextGaussian()��" +
rand.nextGaussian());
//����һ������long����ȡֵ��Χ��α�������
System.out.println("rand.nextInt()��" +
rand.nextInt());
//����0~26֮���α�������
System.out.println("rand.nextInt(26)��" + rand.nextInt(26));
//����һ������long����ȡֵ��Χ��α�������
System.out.println("rand.nextLong()��" + rand.nextLong());
}
}
运行结果:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=6160:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 TestRandom
rand.nextBoolean()��false
[94, -109, 52, -2, 57, 117, -70, 123, -36, 24, -119, 75, -22, 13, -57, 113]
rand.nextDouble()��0.45637749845199027
rand.nextFloat()��0.48870128
rand.nextGaussian()��-0.3014148748267904
rand.nextInt()��-1936491288
rand.nextInt(26)��23
rand.nextLong()��5193037278871421307
Process finished with exit code 0
- TestSeed
源代码:
import java.util.Random;
public class TestSeed
{
public static void main(String[] args)
{
Random r1 = new Random(50);
System.out.println("��һ������Ϊ50��Random����");
System.out.println("r1.nextBoolean():\t" + r1.nextBoolean());
System.out.println("r1.nextInt():\t\t" + r1.nextInt());
System.out.println("r1.nextDouble():\t" + r1.nextDouble());
System.out.println("r1.nextGaussian():\t" + r1.nextGaussian());
System.out.println("---------------------------");
Random r2 = new Random(50);
System.out.println("�ڶ�������Ϊ50��Random����");
System.out.println("r2.nextBoolean():\t" + r2.nextBoolean());
System.out.println("r2.nextInt():\t\t" + r2.nextInt());
System.out.println("r2.nextDouble():\t" + r2.nextDouble());
System.out.println("r2.nextGaussian():\t" + r2.nextGaussian());
System.out.println("---------------------------");
Random r3 = new Random(100);
System.out.println("����Ϊ100��Random����");
System.out.println("r3.nextBoolean():\t" + r3.nextBoolean());
System.out.println("r3.nextInt():\t\t" + r3.nextInt());
System.out.println("r3.nextDouble():\t" + r3.nextDouble());
System.out.println("r3.nextGaussian():\t" + r3.nextGaussian());
Random r4 = new Random(System.currentTimeMillis());
System.out.println("�Ե�ǰʱ��Ϊ���ӵ�Random����");
System.out.println("r3.nextBoolean():\t" + r4.nextBoolean());
System.out.println("r3.nextInt():\t\t" + r4.nextInt());
System.out.println("r3.nextDouble():\t" + r4.nextDouble());
System.out.println("r3.nextGaussian():\t" + r4.nextGaussian());
}
}
运行结果:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=6224:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 TestSeed
��һ������Ϊ50��Random����
r1.nextBoolean(): true
r1.nextInt(): -1727040520
r1.nextDouble(): 0.6141579720626675
r1.nextGaussian(): 2.377650302287946
---------------------------
�ڶ�������Ϊ50��Random����
r2.nextBoolean(): true
r2.nextInt(): -1727040520
r2.nextDouble(): 0.6141579720626675
r2.nextGaussian(): 2.377650302287946
---------------------------
����Ϊ100��Random����
r3.nextBoolean(): true
r3.nextInt(): -1139614796
r3.nextDouble(): 0.19497605734770518
r3.nextGaussian(): 0.6762208162903859
�Ե�ǰʱ��Ϊ���ӵ�Random����
r3.nextBoolean(): true
r3.nextInt(): -240417621
r3.nextDouble(): 0.8432567576867095
r3.nextGaussian(): 2.383733724521367
Process finished with exit code 0
- VariableArgumentsTest
源代码:
public class VariableArgumentsTest {
public static double max(double...values)
{
double largest=Double.MIN_VALUE;
for (double v:values)
if(v>largest) largest=v;
return largest;
}
public static void main(String args[]) {
System.out.println("Max:"+max(1,11,300,2,3));
}
}
运行结果:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=6288:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 VariableArgumentsTest
Max:300.0
Process finished with exit code 012