类与对象

一.利用线性同余法生成随机数

  


特殊之处:方法名一样,但是输出结果不一样!
方法名相同区分的方式(用square方法为例)
·参数类型不同,square(int x),square(double y)
·参数个数,square(int x,int y)square(int x)
·参数类型顺序不同,square(int x,double y)square(double x,int y)
二.在SquareIntTest中,把Square 方法中的static删了会有什么情况
·程序无法运行,并报错
·在main方法中,用类SquareIntTest定义一个新的类名字即SquareIntTest s=new SquareIntTest();这样,在调用方法既可以类名.方法名来调用方法,即s.square(x);

课后作业

一.组合数问题

二.课后作业2递归编程解决汉诺塔问题。用Java实现

import java.io.*;
public class TowersOfHanoi{
 public static void main( String[] args ) throws IOException{
  System.out.println("输入盘子的个数:");
  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  String readnum = reader.readLine();
  int n = Integer.parseInt(readnum);
  solveTowers(n,'A','B','C');
   }

   public static void solveTowers(int n,char a,char b,char c){
    if (n == 1)
     System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
    else {
     solveTowers(n - 1, a, c, b);
     System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
     solveTowers(n - 1, b, a, c);
       }
   }
}


三.课后作业3使用递归方式判断某个字串是否是回文( palindrome )
import java.io.*;
public class Huiwen {
 public static void main(String[] args)throws IOException {
 System.out.println("请输入一个字符串:");
 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 String str=reader.readLine();
 if(huijudge(str)==1)
 System.out.println(str+"是回文!");
 else
 System.out.println(str+"不是回文!");
 }
 public static int huijudge(String str){
  int judge=1;
  int length=str.length();
  //charAT()是把字符串拆分获取其中的某个字符,返回指定位置的字符。  
  char f=str.charAt(0);
  char l=str.charAt(length-1);
  if(length==0||length==1)
   return judge=1;
  if(f!=l)
   return judge=0;
  //substring() 方法用于提取字符串中介于两个指定下标之间的字符。
  return huijudge(str.substring(1,length-1));
 }
}

原文地址:https://www.cnblogs.com/shenghuizhang/p/5984867.html