码迷,mamicode.com
首页 > 其他好文 > 详细

方法动手动脑及实验

时间:2016-10-13 23:29:31      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

一。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 = 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)));
  }
  }

  结果截图:技术分享技术分享

 

方法动手动脑及实验

标签:

原文地址:http://www.cnblogs.com/xieshiyu/p/5958282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!