首页 > 代码库 > 方法动手动脑及实验
方法动手动脑及实验
一。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;
}
}
结果截图:
二。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 = http://www.mamicode.com/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.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 );
}
}
结果截图:
四。编写一个算法,使用有以上算法生成指定数目(比如1000个)的随机整数。
五。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));
}
}
结果截图:
六。观察一下代码,你发现有什么特殊之处吗?
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;
}
}
特殊之处:方法名相同,返回值类型和参数类型不同,输入需要进运算的参数会执行其相应类型的运算。
七。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)));
}
}
结果截图:
方法动手动脑及实验