2017年3月Java编程题整理

1.打印菱形

public class T1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int leng = 6;  
		int leng1 = leng-1;  
		System.out.println("空心菱形:");  
		for(int k = 1;k <= leng;k++){  
		    for(int i = 1;i <= (leng - k);i++){  
		        System.out.print(" ");  
		    }  
		    System.out.print("*");  
		    for(int i = 1;i <= ( (k - 2) * 2 + 1);i++){  
		        System.out.print(" ");  
		    }  
		    if(k != 1){  
		        System.out.print("*");    
		    }  
		    System.out.println();  
		}  
		for(int k = leng1;k >=1;k--){  
		    for(int i = 1;i <= (leng - k);i++){  
		        System.out.print(" ");  
		    }  
		    System.out.print("*");  
		    for(int i = 1;i <= ( (k - 2) * 2 + 1);i++){  
		        System.out.print(" ");  
		    }  
		    if(k != 1){  
		        System.out.print("*");    
		    }  
		    System.out.println();  
		}  
		  
		int rowNum=11;  
		System.out.println("实心菱形:");  
		for(int i = 1;i <= rowNum;i++){  
		    if(i <= rowNum/2 + 1){  
		        for(int k = 1;k <= rowNum/2 + 1- i;k++){  
		            System.out.print(" ");  
		        }  
		        for(int k = 1;k <= i;k++){  
		            System.out.print("* ");  
		        }  
		            System.out.println();  
		    }else{  
		        for(int k =1;k <= (i -(rowNum/2 + 1));k++){  
		            System.out.print(" ");  
		        }  
		        for(int k = 1;k <= (2 *(rowNum/2 + 1) - i);k++){  
		            System.out.print("* ");  
		        }  
		        System.out.println();  
		    }     
		}  
	}

}

输出结果:

空心菱形:
     *
    * *
   *   *
  *     *
 *       *
*         *
 *       *
  *     *
   *   *
    * *
     *
实心菱形:
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 

2.将数字金额转换为大写金额

import java.util.Scanner;

public class ChangeNum {

	public static void main(String[] args) {
		//String arr[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖","拾"};
		String arr[]={"零","一","二","三","四","五","六","七","八","九","拾"};
		//String a="101";
		System.out.println("请输入:");
		Scanner input=new Scanner(System.in);
		String a=input.next();
		input.close();
		String ar[]=a.split("");
		String stb="";
		
		for(int i=0;i<ar.length;i++){
			stb=stb+arr[Integer.parseInt(ar[i])];
		}
		String stc[]=stb.split("");
		switch(ar.length){
		case 1:
			System.out.println(stc[0]);
			break;
		case 2:
			if(Integer.parseInt(ar[1])==0){
				System.out.println(stc[0]+"拾");
			}
			else{
				System.out.println(stc[0]+"拾"+stc[1]);
			};
			break;
		case 3:
			if(Integer.parseInt(ar[1])==0){
				System.out.println(stc[0]+"佰"+stc[1]+stc[2]);
			}
			else{
				System.out.println(stc[0]+"佰"+stc[1]+"拾"+stc[2]);
			};
			break;
		case 4:
			if(Integer.parseInt(ar[1])==0&&Integer.parseInt(ar[2])==0&&Integer.parseInt(ar[3])==0){
				System.out.println(stc[0]+"千");
			}
			else if(Integer.parseInt(ar[1])==0&&Integer.parseInt(ar[2])==0){
				System.out.println(stc[0]+"千"+"零"+stc[3]);
			}
			else if(Integer.parseInt(ar[1])==0&&Integer.parseInt(ar[3])==0){
				System.out.println(stc[0]+"千"+stc[1]+stc[2]+"拾");
			}
			else if(Integer.parseInt(ar[2])==0&&Integer.parseInt(ar[3])==0){
				System.out.println(stc[0]+"千"+stc[1]+"佰");
			}
			else if(Integer.parseInt(ar[1])==0){
				System.out.println(stc[0]+"千"+stc[1]+stc[2]+"拾"+stc[3]);
			}
			else if(Integer.parseInt(ar[3])==0){
				System.out.println(stc[0]+"千"+stc[1]+"佰"+stc[2]+"拾");
			}
			else{
				System.out.println(stc[0]+"千"+stc[1]+"佰"+stc[2]+"拾"+stc[3]);
			};
			break;	
		default:
			break;
		}
	}

}

输出结果:

请输入:
1010
一千零一拾

3.将字符串中的汉字、数字和字母分别统计

public class DivideStr {

	public static void main(String[] args) {
		String str="12ab中文56";
		String regex1="[0-9]";
		String regex2="[a-zA-Z]";
		int count1=0,count2=0,count3=0;
		String str1="",str2="",str3="";
		String arr[]=str.split("");
		for(int i=0;i<arr.length;i++){
			if(arr[i].matches(regex1)){
				str1=str1+arr[i];
				count1++;
			}
			else if(arr[i].matches(regex2)){
				str2=str2+arr[i];
				count2++;
			}
			else{
				str3=str3+arr[i];
				count3++;
			}
		}
		System.out.println("数字有:"+str1+" 共:"+count1+"个");
		System.out.println("字母有:"+str2+" 共:"+count2+"个");
		System.out.println("汉字有:"+str3+" 共:"+count3+"个");
	}

}


输出结果:

数字有:1256 共:4个
字母有:ab 共:2个
汉字有:中文 共:2个

4.计算1+1/(2!)+1/(3!)+…..1/(10!)

public class CountNum {

	public static void main(String[] args) {
		double sum=0;
		double count=1.0;
		for(int i=1;i<11;i++){
			count=count*i;
			sum=sum+1/(count);
		}
		System.out.println(sum);
	}

}

输出结果:

1.7182818011463847

5.台阶问题。青蛙上50级台阶,每次跳一个或者两个台阶。

public class CountTest{
	
	public static long count(int i){
		if(i==1){
			return 1;
		}
		if(i==2){
			return 2;
		}
		else{
			return count(i-1)+count(i-2);
		}
	}
	public static void main(String args[]){
		int n = 50;
		long total = count(n);
		System.out.println(total);
	}
	
}

输出结果:

20365011074




猜你喜欢

转载自blog.csdn.net/sagitarioo/article/details/65654980