天天看點

實驗一 Java程式設計基礎複習

1.輸入三個整數x、y、z,對其進行排序,使得x<y<z。。

源程式:

import java.util.Scanner;
public class t1 {

	public static void main(String[] args) {
		Scanner a1 = new Scanner(System.in);
		System.out.println("請輸入三個數");
		int x = a1.nextInt();
		int y = a1.nextInt();
		int z = a1.nextInt();
		a1(x,y,z);
		
		
	}
	static void a1(int x,int y,int z) {
		int i = 0;
		if(x>y) {
			i=x;x=y;y=i;
		}
		if(y>z) {
			i=y;y=z;z=i;
		}
		System.out.println(x+"<"+y+"<"+z);
	}

}
           

2.輸入年月,顯示這個月的天數。

源程式:

import java.util.Scanner;
public class t2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s2 = new Scanner(System.in);
		System.out.println("請輸入年月");
		int a = s2.nextInt();
		int b = s2.nextInt();
		switch (b) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:	
			System.out.println("這個月有31天");
			break;
		case 4:
		case 6:	
		case 9:
		case 11:
			System.out.println("這個月有30天");
			break;
		case 2:
			if(a%4 ==0 & a%100 !=0)
				System.out.println("這個月有29天");
			else if(a%400 ==0)
				System.out.println("這個月有29天");
			else
				System.out.println("這個月有28天");
		}
	}

}
           

3.數字反轉

從鍵盤輸入一個三位整數n,将整數n的個位、十位、百位倒序生成一個新數字并進行輸出。例如輸入521,則輸出125。

源程式:

import java.util.Scanner;
public class t3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s3 = new Scanner(System.in);
		System.out.println("請輸入一個三位數");
		int a = s3.nextInt();
		int i1 = a%100%10;
		int i2 = a%100/10;
		int i3 = a/100;
		System.out.print(i1);
		System.out.print(i2);
		System.out.print(i3);
	}

}
           

4.猜數遊戲

編寫一個猜數字遊戲,将玩家猜測的數字與系統随機生成的數字(0~100之間)比較,在玩家猜測的過程中,給出“sorry,您猜大了!”或“sorry,您猜小了!”的提示,直到猜中為止。

源程式:

import java.util.Random;
import java.util.Scanner;
public class t4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a = (int)(Math.random()*100);
		System.out.println("請猜數");
		Scanner s4 = new Scanner(System.in);
		int b = s4.nextInt();
		while(a!=b) {
			if(a>b) 
				System.out.println("sorry,您猜小了!");
			else 
				System.out.println("sorry,您猜大了!");
			System.out.println("請猜數");
			b = s4.nextInt();
		}
		if(a==b) {
			System.out.println("恭喜您,猜對了!");
	}
	}
}
           

5.輸出九九乘法表,如下圖所示。

源程式:

public class t5 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for(int i =1;i<10;i++) {
			for(int j =1;j<i+1;j++) 
				System.out.print(j+"*"+i+"="+i*j+"   ");
			System.out.println();		
		}
	}
}

           

6.水仙花數是指個位、十位和百位3個數的立方和等于這個三位數本身的數,編寫程式求出所有的水仙花數。(借用自定義方法實作)

源程式:

public class t6 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for(int i = 100;i<1000;i++) {
			int a=i/100;
			int b=i%100/10;
			int c=i%100%10;
			if(a*a*a+b*b*b+c*c*c == i)
				System.out.println(i+"是水仙花數");
		}
	}
}
           

7.小明非常很喜歡數字9,包括那些數位上包含數字9 的數。如果一個數的數位包含數字9,小明将它稱為潔淨數。 請問在整數 1 至 n(n<1000) 中,潔淨數有多少個?

【輸入格式】 輸入為一行整數,即問題中的n。

【輸出格式】 輸出一行包含一個整數,表示答案。

【樣例輸入】

30

【樣例輸出】

3

【完整的輸入輸出樣例】

30 3

請嚴格按要求輸出,不要畫蛇添足地列印類似:“請您輸入…” 等多餘内容。

源程式:

import java.util.Scanner;
public class t7 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s7 = new Scanner(System.in);
		int a = s7.nextInt();
		int j = 0;
		for (int i = 1; i <= a; i++) {
			String b = String.valueOf(i);
			if (b.indexOf('9') >= 0) {
				j++;
			}

		}
		System.out.println(j);

	}

}
           

8.定義一個整型數組,随機産生15個0-100之間的整數存入該數組。任選一種排序算法對其進行升序排列,然後以每行5個資料的格式進行輸出。

源程式:

public class t8 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//建立數組
		int[] a = new int[15];
		for(int i=0;i<15;i++) {
			a[i] = (int)(Math.random()*100);
		}
		System.out.println("原始資料:");
		for(int i=0;i<15;i++)
			System.out.print(a[i]+"\t");
		//排序
		int t;
		for(int i=0;i<a.length-1;i++) {
			for(int j=i+1;j<a.length;j++) {
				if(a[i]>a[j]) {
					t=a[i];a[i]=a[j];a[j]=t;
				}
			}
		}
		System.out.println("\n"+"排序後:");
		for(int i=0;i<15;i++)
			System.out.print(a[i]+"\t");
	}	

}
           

9.顯示輸出楊輝三角形:

源程式:

import java.util.Scanner;
public class t9 {
	public static void main(String[] args) {
		System.out.println("輸入一個數");
		Scanner s9 = new Scanner(System.in);
		int b = s9.nextInt();
		int a[][] = new int[b][];
		for(int i=0;i<b;i++) {
			a[i]=new int[i+1];
		}
		for(int i=0;i<b;i++) {
			a[i][0]=1;
			a[i][i]=1;
		}
		for(int i=2;i<b;i++) {
			for(int j=1;j<i;j++)
				a[i][j]=a[i-1][j]+a[i-1][j-1];
		}
		for(int i=0;i<a.length;i++) {
			for(int j=0;j<a[i].length;j++)
				System.out.print(a[i][j]+"\t");
		System.out.println();
		}

			
	}
}