• 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; } }

    1、此代码中采用static调用自定义方法,如果不加static可以使用类名.成员名或者对象名.成员名调用。

    2、编写一个方法,使用纯随机数发生器算法生成指定数目(比如1000个)的随机整数。

    import javax.swing.JOptionPane;
    
    public class Testseed {
       public static void main( String args[] )
       {
          int value;
          String output = "";
    
          for ( int i = 1; i <= 100; i++ ) {
             value = 1 + (int) ( Math.random() * 100 );
             output += value + "  ";
             
             if ( i % 10== 0 )
                output += "
    ";
          }
    
          JOptionPane.showMessageDialog( null, output,
             "20 Random Numbers from 1 to 6",
             JOptionPane.INFORMATION_MESSAGE );
    
          System.exit( 0 );
       }
    }
    

    此程序输出100个随机数,但并未采用随机数发生器写,此程序存在问题。

    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("
    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;
    	}
    }

    这段代码自定义了两个方法,展示了java的方法重载,一个为int类型,一个为double类型,在输出时int 类型自动调用int类型的方法,double类型自动调用double类型的方法,同时,改动自定义方法的顺序并不影响结果,证明并不是先后顺序决定调用顺序,而是java自动识别参数类型然后找到对应的自定义方法进行计算。

    4、使用计算机计算组合数:

    (1)使用组合数公式利用n!来计算

    import java.util.Scanner;
    public class Zuheshu1 {
    	public static void main(String[]args){
    		System.out.println("请输入组合数的n和k:");
    		Scanner in1=new Scanner(System.in);
    		int n=in1.nextInt();
    		Scanner in2=new Scanner(System.in);
    		int k=in2.nextInt();
    		int result=jiechen(n)/(jiechen(k)*jiechen(n-k));
    		System.out.println("组合数结果为:"+result);
    		in1.close();
    		in2.close();
    	}
    	public static int jiechen(int n)
    	{
    		int jieguo=1;
    		if(n<0)
    		{
    			System.out.println("输入非法!");
    		}
    		else if(n==0||n==1)
    		{
    			jieguo=1;
    		}
    		else 
    		{
    			jieguo=jiechen(n-1)*n;
    		}
    		return jieguo;
    		
    	}
    
    }

    实验结果

    (2)使用递推的方法用杨辉三角形计算

    package Zuheshu2;
    
    import java.util.Scanner;
    
    public class Zuheshu2 {
    	public static void main(String[]args){
    		System.out.println("请输入组合数的n和k:");
    		Scanner in1=new Scanner(System.in);
    		int n=in1.nextInt();
    		Scanner in2=new Scanner(System.in);
    		int k=in2.nextInt();
    		System.out.println("组合数结果为:"+jieguo(n,k));
    		in1.close();
    		in2.close();
    	}
    	public static int jieguo(int n,int m)
    	{
    		if(m==0||n==m)
    			return 1;
    		int s=Math.min(m, n-m);
    		int f=1,f1=0;
    		for(int i=1;i<=s;i++)
    		{
    			f1=f*(n-i+1)/(i);
    			f=f1;
    		}
    		return f1;
    		}
    	}
    

    实验结果:(此算法借鉴百度)

    (3)使用递归的方法用组合数递推公式计算

    import java.util.Scanner;
    
    public class Zuheshu2 {
    	public static void main(String[]args){
    		System.out.println("请输入组合数的n和k:");
    		Scanner in1=new Scanner(System.in);
    		int n=in1.nextInt();
    		Scanner in2=new Scanner(System.in);
    		int k=in2.nextInt();
    		System.out.println("组合数结果为:"+jieguo(n,k));
    		in1.close();
    		in2.close();
    	}
    	public static int jieguo(int m,int n)
    	{
    		if(m<0||n<0||m<n)
    			return 0;
    		if(m==n)
    			return 1;
    		if(n==1)
    			return m;
    		return jieguo(m-1,n)+jieguo(m-1,n-1);
    	}
    }
    

      实验结果:

    5.递归编程解决汉诺塔问题。用Java实现

    public class TowersOfHanoi
    {
       public static void solveTowers( int disks, int sourcePeg, 
          int destinationPeg, int tempPeg )
       {
          if ( disks == 1 )
          {
             System.out.printf( "
    %d --> %d", sourcePeg, destinationPeg );
             return;
          }
          solveTowers( disks - 1, sourcePeg, tempPeg, destinationPeg );
          System.out.printf( "
    %d --> %d", sourcePeg, destinationPeg );
          solveTowers( disks - 1, tempPeg, destinationPeg, sourcePeg );
       } 
       public static void main( String[] args )
       {
          int startPeg = 1; 
          int endPeg = 3; 
          int tempPeg = 2; 
          int totalDisks = 3;
          solveTowers( totalDisks, startPeg, endPeg, tempPeg );
       } 
    } 
    

      实验结果:

    6.回文数

    import java.util.*;
    public class Palindrome {
    	public static void main(String[]args){
    			     //从键盘上输入一个字符串str
    			  String str="";
    			  System.out.println("请输入一个字符串:");
    			  Scanner in=new Scanner(System.in);
    			  str=in.nextLine();
    			 //根据字符串创建一个字符缓存类对象sb
    			  StringBuffer sb=new StringBuffer(str);
    			 //将字符缓存中的内容倒置
    			  sb.reverse();
    			 //计算出str与sb中对应位置字符相同的个数n
    			  int n=0;
    			  for(int i=0;i<str.length();i++){
    			   if(str.charAt(i)==sb.charAt(i))
    			    n++;
    			  }
    			 //如果所有字符都相等,即n的值等于str的长度,则str就是回文。
    			     if(n==str.length())
    			      System.out.println(str+"是回文!");
    			     else
    			      System.out.println(str+"不是回文!");
    			 }
    	}
    

     实验结果:

  • 相关阅读:
    时间选择器UIDatePicker的使用
    在app中屏蔽第三方键盘
    plist文件的相关操作
    查看mac上的隐藏文件
    设置ARC有效或者无效
    Linux 下源代码安装编译 ImageMagick6.8.48 且使其支持 JPEG
    Linux Netcat 命令—网络工具中的瑞士军刀
    Linux 好书、经典书籍推荐
    让你拥有超能力:程序员应该掌握的统计学公式
    shell 脚本实现的守护进程
  • 原文地址:https://www.cnblogs.com/1998lu/p/5964929.html
Copyright © 2020-2023  润新知