9.22日作业
一、
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;
}
}
二、
// 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 );
}
}
三、
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());
}
}
四、
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());
}
}
五、
// 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 );
}
}
六、
随机数:public class testradom {
public static void main(String[] args) {
long seed=System.currentTimeMillis();
int Multiplier=16807,j=0;
long random=(Multiplier*seed)%Integer.MAX_VALUE;
for(int i=0;i<1000;i++) {
random=(Multiplier*random)%Integer.MAX_VALUE;
System.out.print(random+" ");
j++;
if(j%10==0)System.out.println();
}
}
}
七、
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));
}
}
八、
// 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;
}
}
上述代码的特殊之处是关于方法的重载,是无法通过返回值的不同而进行区别的。
out是system的成员变量;out在System类里面是PrintStream类型的的一个静态顶级成员变量,可以使用PrintStream里面的println方法。
九、
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)));
}
}
bug:
由于计算机使用固定的位数来保存数值,因此,能处理的数值大小是有限的,当要处理的数值超过了这一范围时,计算机将会自动截断数值的二进制表示为它所能处理的最多位数,这将导致错误的处理结果。