03_方法(动手动脑和课后作业)

一、

MethodOverload.java

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

 

(1)方法名相同; (2)参数类型不同,参数个数不同,或者是参数类型的顺序不同

 二、

package java03kehou;

import java.util.Random;


public class Suijishu {

 

private static final int N = 200;

private static final int LEFT = 40;

private static final int RIGHT = 10000;

private static long x0 = 1L;

private long a = 1103515245L;

private long c = 12345L;

private long m = 2147483648L;

 

// 产生随机数

private long rand ( long r ){

 

r = ( r * a + c ) % m; //Xn+1=(aXn + c)mod m
return r;
  }

 

private long little1 ( int a, int b, long rand ){

return a+rand%(b-a+1);

  }

 

private void recursion ( int count, long rand ){

if (count >= N){return;}

rand = rand (rand);

long r = little1 (LEFT, RIGHT, rand);

System.out.print (r + " ");

recursion (++count, rand);

  }

 

private long little(int left2, int right2, long rand) {
// TODO Auto-generated method stub
return 0;
  }

 

public static void main ( String[] args ){

Suijishu recur = new Suijishu ();
recur.recursion (0, x0);

  }

}

 

 课后作业3

Palindrome.java

package java03kehou;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Palindrome {



public static void main(String[] args)throws Exception {

String str ="";
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in),256);

System.out.println("输入字符串:");

str = br.readLine();

System.out.println("是否为回文: " + huiwen(str,0,str.length()-1));
}
public static boolean huiwen(String s,int start,int end){

if(start == end) return true;
if(start > end)
{
System.out.println("error!");
return false;
}
if(s.charAt(start) == s.charAt(end))

{
return huiwen(s,start+1,end-1);
}
else {return false;}
}}

 

转载于:https://www.cnblogs.com/wwd1505-1/p/5962864.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值