java课java方法动手动脑
动手动脑:
import java.util.Scanner;
public class Random {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n;
System.out.println("输入随机数个数");
n=input.nextInt();
int seed=1;
int c=0;
int num=0;
for(int i=0;i<n;i++){
seed=(7^5*seed+c)%(2^100);
num++;
System.out.print(seed+" ");
if(num%10==0) {
System.out.println();
}
}
}
}
课后验证1:CalculateN,java
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)));
}
}
课后测试2:CompareFloatNumber.java
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");
}
}
}
课后测试3:MethodOverload.java
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;
}
}
课后测试4:RandomInt.java
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 );
}
}
课后测试5:RollDie.java
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 );
}
}
课后测试6:SquareInt.java
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;
}
}
课后测试7:TestRandom.java
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());
}
}
课后测试8:TestSeed.java
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());
}
}
课后测试9:VariableArgumentsTest.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
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));
}
}