首页 > 代码库 > 03_方法(动手动脑和课后作业)
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;}
}}
03_方法(动手动脑和课后作业)