• 方法动手动脑及实验


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

      // 自定义求平方数的静态方法
      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 += " ";
      }

      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 Frequency" +
      " 1 " + frequency1 +
      " 2 " + frequency2 +
      " 3 " + frequency3 +
      " 4 " + frequency4 +
      " 5 " + frequency5 +
      " 6 " + frequency6 );

      JOptionPane.showMessageDialog( null, outputArea,
      "Rolling a Die 6000 Times",
      JOptionPane.INFORMATION_MESSAGE );
      System.exit( 0 );
      }
      }

      结果截图:

    四。编写一个算法,使用有以上算法生成指定数目(比如1000个)的随机整数。

      代码:

    import java.util.*;
    public class TextRandom {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner in = new Scanner(System.in);
            
            int a = (int)Math.pow(7,5);
            int m = (int)Math.pow(2, 31) - 1;
            int c = 0;
            
            System.out.print("请输入要产生随机数的个数:");
            int n = in.nextInt();
            for(int i = 0;i<n;i++)
            {
                Random x1 = new Random();
                    int x3 = x1.nextInt(10);
                    int x2 = (a* x3+c)%m;
                   System.out.println(x2);
            }
        }
    
    }

    五。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(" The 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)));
      }
      }

      结果截图:

    八。用阶乘来求组合数

      1.代码:

      

      import java.util.*;
      public class JC {

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      Scanner in = new Scanner(System.in);
      System.out.print("请输入n:");
      int n = in.nextInt();
      System.out.print("请输入k:");
      int k = in.nextInt();
      int C = Jc(n)/(Jc(k)*Jc(n - k));
      System.out.println("C(n,k) = "+C);
      }
      public static int Jc(int n)//递归法计算阶乘
      {
      int s = 0;
      if(n < 0)
      System.out.println("Error!");
      else if(n == 1||n == 0)
      s = 1;
      else
      s = n * Jc(n -1);
      return s;
      }
      }

       2.结果截图:

            

    九。使用递推的方法使用杨辉三角形计算

      1.代码:

      

      import java.util.Scanner;

      public class YH {

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      Scanner in = new Scanner(System.in);
      System.out.print("请输入n:");
      int n = in.nextInt();
      System.out.print("请输入k:");
      int k = in.nextInt();

      int C = Yh(n,k);
      System.out.println("C(n,k) = "+C);
      }
      public static int Yh(int n,int k)
      {
      if(k == 0||n == k) return 1;
      int s=Math.min(k,n-k);
      int p = 1,q = 0;

      for(int i = 1; i <= s; i++)
      {
      q = p * (n-i+1)/(i);
      p = q;
      }

      return q;
      }
      }

      2.实验截图:

            

    十。用组合数递推公式

      1.代码:

      

      import java.util.Scanner;

      public class DT {

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      Scanner in = new Scanner(System.in);
      System.out.print("请输入n:");
      int n = in.nextInt();
      System.out.print("请输入k:");
      int k = in.nextInt();

      int C = Dt(n,k);
      System.out.println("C(n,k) = "+C);
      }
      public static int Dt(int n,int k)
      {
      int c = 0;
      if(n == 1||k == 0||n == k)
      c = 1;
      else
      c = Dt(n - 1,k - 1) + Dt(n - 1,k);
      return c;
      }
      }

      2.结果截图:

        

    十一。汉诺塔问题

      1.代码:

      

      import javax.swing.JOptionPane;
      public class T3 {

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      String str = JOptionPane.showInputDialog("输入汉诺塔层数:");
      int n = Integer.parseInt(str);

      JOptionPane.showMessageDialog(null, "挪动次数为"+fun(n));
      }
      public static int fun(int n){
      int count = 0;
      if(n == 1){
      count = 1;
      }
      else if(n == 2){
      count = 3;
      }

      else

      {
      count = 2*fun(n-1)+1;
      }
      return count;
      }
      }

      2.结果截图:

      

      

    十二。判断字符串是否为回文数

      1.代码:

      import java.util.*;
      public class HW {

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      String temp = "";
      char[] a;
      Scanner in = new Scanner(System.in);
      System.out.print("请输入要判断的字符串:");
      String str = in.next();
      a = str.toCharArray();//将字符串对象中的字符转换为一个字符数组
      for(int i = a.length - 1;i >= 0;i--)//将字符串逆置
      {
      temp += a[i];
      }
      if(str.equals(temp))
      System.out.println(str+"是回文数!");
      else
      System.out.println(str+"不是回文数!");
      }
      }

      2.结果截图:

              

  • 相关阅读:
    Netty 心跳处理
    Netty 搭建 WebSocket 服务端
    Spring Boot 集成 MQTT
    Spring Boot 上传文件
    在 CentOS 7 安装 Tomcat
    神坑之 6666 端口 (默认非安全端口)
    MongoTemplate 移除 _class 字段
    在 CentOS 7 安装 RabbitMQ
    MongoDB 分片集群配置
    tensorflow学习(一)
  • 原文地址:https://www.cnblogs.com/xieshiyu/p/5958282.html
Copyright © 2020-2023  润新知