java编程运算几个具体实例
整理下这次考试的实例:
1、编写一个方法func2(),判断一个整数是否包含数字7.
import java.util.Scanner;
public class Test3 {
	public static void main(String[] args) {
    	Scanner sc =new Scanner(System.in);
    	System.out.println("请输入数值: ");
    	int num = sc.nextInt();
System.out.println(func2( num));
	}
    public static boolean func2(int num){
int n;
boolean flag=false;
while(num!=0){
n=num%10;
if(n==7){
flag=true;
break;
}
num /= 10;
}
return flag;
    }
}
2.编写一个类Demo,类里有一个方法metod2(),对输入的任意字符串“1,4,7,13,5,17,9”转换为数组元素是整数元素,需要保存在整形数组中且实现排序输出“1 4 5 7 9 13 17”
package oracle.zibo.exam;
import java.util.Arrays;
import java.util.Scanner;
public class Demo {
	public static void main(String[] args) {
//		String str =("1,4,7,13,5,17,9");
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入数字字符串,并用逗号隔开");
		String str = sc.next();
		metod2(str);
	}
    public static void metod2(String str){
    	String array[] = str.split(",");
    	int p[]=new int[array.length];
    	for(int i=0;i<array.length;i++){
    		p[i]=Integer.parseInt(array[i]);
    	}
    	//Arrays.sort(p);   	
    	for(int i=0;i<p.length-1;i++){
    		for(int j=0;j<p.length-1-i;j++){
    			if(p[j]>p[j+1]){
    				int temp =p[j];
    				p[j]=p[j+1];
    				p[j+1]=temp;
    			}
    		}
    	}
    	for(int i=0;i<p.length;i++){
    		System.out.print(p[i]+"  ");
    	}
    	
    }
}
3.假设有字符串“uyde87344USijnmWO987”,统计其中数字的个数、大写字母的个数、小写字母的个数.
package oracle.zibo.exam;
import java.util.Scanner;
public class Test7 {
	public static void main(String[] args) {
		 Scanner sc =new Scanner(System.in);
         System.out.println("请输入字符串 ");
         String str=sc.next();
         getnum(str);
	}
	public static void getnum(String str){
        int cU = 0 ;
        int cL = 0 ;
        int cN = 0 ;
        char c ;
        for(int i=0 ;i<str.length() ;i++) {
            c = str.charAt(i) ;
            if(c >='a' && c <= 'z') {
                cL ++ ;
            }
            else if(c >= 'A' && c <= 'Z') {
                cU ++ ;
            }
            else if(c>='0'&&c<='9'){
                cN ++ ;
            }
        }
        System.out.println("小写字母个数为:" + cL) ;
        System.out.println("大写字母个数为:" + cU) ;
        System.out.println("数字个数为:" + cN) ;
    }
}
4,编写一个方法,传入数值,打印出金字塔
 public static void print(int num){
    	for(int i=0;i<num;i++){
        	for(int k=0;k<num-i-1;k++){
        		System.out.print(" ");
        	}
        	for(int j=0;j<2*i+1;j++){
        		System.out.print("*");
        	}
            System.out.println();
        }
    }
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号