标签:
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 + "\n");
}
}
// 自定义求平方数的静态方法
public static int square(int y) {
return y * y;
}
}
1、如果不加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 += "\n"; } JOptionPane.showMessageDialog( null, output, "20 Random Numbers from 1 to 6", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); }}3.请看以下代码,你发现了有什么特殊之处吗?
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; }}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; }}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; } }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); }}
public class TowersOfHanoi
{ public static void solveTowers( int disks, int sourcePeg, int destinationPeg, int tempPeg ) { if ( disks == 1 ) { System.out.printf( "\n%d --> %d", sourcePeg, destinationPeg ); return; } solveTowers( disks - 1, sourcePeg, tempPeg, destinationPeg ); System.out.printf( "\n%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 ); }}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+"不是回文!"); } }
标签:
原文地址:http://www.cnblogs.com/ylx111/p/5966147.html