三十個Java基礎練習題


題目:http://javaroad.blog.51cto.com/661972/646073

1.將1 到1000 之間的奇數打印出來

package com.guet;

//1到1000的奇數打印出來
public class JiShu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int number = 0;
		int now = 1;
		for (int i = 1; i <= 1000; i++) {
			if (now++ % 2 == 1)
				number++;					
		}
		System.out.print("1到1000的奇數有"+number+"個");
	}
}

 4-30第二次

public class One {// 將1到1000之間的奇數打印出來

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i;
		for (i = 1; i < 1001; i++) {
			if (i % 2 == 1) {
				System.out.println(i);
			}
		}
	}
}

 

2.判斷一個數能否同時被3和5 整除

package com.guet;

import java.util.Scanner;

//判斷一個數能否同時被3和5 整除
public class ZhengChu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int number;
		System.out.print("輸入一個數:");
		Scanner numScanner=new Scanner(System.in);
		number=numScanner.nextInt();
		if(number%3==0&&number%5==0)
			System.out.println(number+"可以 同時被3和5 整除");
		else
			System.out.println(number+"不能同時被3和5 整除");
	}

}

 4-30

import java.util.Scanner;

public class Two {// 判斷一個數能否同時被3和5整除

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int x = scanner.nextInt();
		if (x % 3 == 0 && x % 5 == 0) {
			System.out.print("可以同時被3和5整除");
		} else {
			System.out.print("不可以同時被3和5整除");
		}

	}

}

 

3.輸入10個數,找出最大一個數,並打印出來。

package com.guet;

import java.util.Scanner;

//輸入10個數,找出最大一個數,並打印出來。
public class Max10 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int number;
		int temp = 0;
		Scanner in = new Scanner(System.in);
		for (int i = 1; i <= 10; i++) {
			System.out.print("輸入第" + i + "個數:");
			number = in.nextInt();
			if (number > temp)
				temp = number;
		}
		System.out.print("這10個數中最大的是:" + temp);
	}
}

 4.給出一百分制成績,要求輸出成績等級’A’,’B’,’C’,’D’,’E’。90 分以上為’A’,80~89 分為’B’,70~79 分為’C’,60~69 分為’D’,60分以下為’E’

package com.guet;

import java.util.Scanner;

//給出一百分制成績,要求輸出成績等級’A’,’B’,’C’,’D’,’E’。
//90 分以上為’A’,80~89 分為’B’,70~79 分為’C’,60~69 分為’D’,60分以下為’E’
public class GradeLevel {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int grade ,level;
		System.out.print("輸入你的百分制成績:");
		Scanner inScanner=new Scanner(System.in);
		grade=inScanner.nextInt();
		if(grade>100||grade<0){
			System.out.println("你輸入的成績有誤");
			System.exit(0);
		}
		switch (grade%10){	
		case 10:
		case 9:level=65;break;//A
		case 8:level=66;break;//B
		case 7:level=67;break;//C
		case 6:level=68;break;
		default :level=69;
		}
      if(grade==100)
level=65;
		System.out.println("你的成績是"+(char)level);
	}
}

 

 5.把一個正整數分解質因數。!注:此方法存在很大問題,因為我還沒想好怎么解決質數遞增的問題。當前代碼運行后,不會自動停止。

package com.google;

import java.util.Scanner;

//把一個正整數分解質因數
public class Five {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int number , i=2;
		Scanner numScanner = new Scanner(System.in);
		System.out.print("輸入一個正整數:");
		number = numScanner.nextInt();
		System.out.print(number+"分解出的質因數有:");
		do{
			if(number%i==0){
				number=number/2;
				System.out.print(i+" ");
			}else{
				i++;
			}
		}while (true);
	}

}

  6.打印出菱形

package com.google;
//打印出如下圖案(菱形)
//   *
//  ***
// *****
//*******
// *****
//  ***
//   *
public class Diamond {
	public static void main(String[] args) {
		// TODO 自動生成的方法存根
		int x, y;
		for (x = 1; x < 5; x++) {
			for (y = 1; y < (4 + x); y++) {

				if (y <= (4 - x))// 原始if(y<=(3+x-(2*x-1)))
					System.out.print(" ");
				else
					System.out.print("*");

			}
			System.out.println();
		}
		for (x = 5; x < 8; x++) {
			for (y = 1; y < 7; y++) {
				if ((x - 4) < y && y < (12 - x))
					System.out.print("*");
				else if(y<=x-4)
					System.out.print(" ");
			}
			System.out.println();
		}

	}
}

  7.將1至7 的數字轉換為星期日到星期六的字符串

package com.google;

import java.util.Scanner;

//將1至7 的數字轉換為星期日到星期六的字符串
//這個題目目前不是很理解
public class SevenDays {

	public static void main(String[] args) {
		// TODO 自動生成的方法存根

		int x;
		String xString = new String();
		System.out.print("輸入1到7其中的一個數字:");
		Scanner num = new Scanner(System.in);
		x = num.nextInt();
		switch (x) {
		case 1: {
			xString = "星期日"+x;
			
		}
			break;
		case 2: {
			xString = "星期一";
			xString = Integer.toString(x);

		}
			break;
		case 3: {
			xString = "星期二";
			xString = Integer.toString(x);

		}
			break;
		case 4: {
			xString = "星期三";
			xString = Integer.toString(x);

		}
			break;
		case 5: {
			xString = "星期四";
			xString = Integer.toString(x);

		}
			break;
		case 6: {
			xString = "星期五";
			xString = Integer.toString(x);

		}
			break;
		case 7: {
			xString = "星期六";
			xString = Integer.toString(x);

		}
			break;
		}
		System.out.print("結果:"+xString);
	}

}

  8.有任意三個整數a,b,c,請輸出其中最大的

import java.util.Scanner;

//有任意三個整數a,b,c,請輸出其中最大的
public class Eight {

    public static void main(String[] args) {
        // TODO 自動生成的方法存根
        int a, b, c;

        Scanner in = new Scanner(System.in);
        System.out.print("輸入第一個數a:");
        a = in.nextInt();
        System.out.print("輸入第二個數b:");
        b = in.nextInt();
        System.out.print("輸入第三個數c:");
        c = in.nextInt();
        if (a > b) {
            if (a > c) {
                System.out.print("a最大");
            } else {
                System.out.print("c最大");
            }
        } else {
            if (b > c) {
                System.out.print("b最大");
            } else {
                System.out.print("c最大");
            }
        }
    }

}

 9.將任意三個整數a,b,c按從小到大的順序輸出

import java.util.Scanner;

//將任意三個整數a,b,c按從小到大的順序輸出
public class Nine {

    public static void main(String[] args) {
        // TODO 自動生成的方法存根

        int a, b, c;
        Scanner in = new Scanner(System.in);
        System.out.print("輸入a:");
        a = in.nextInt();
        System.out.print("輸入b:");
        b = in.nextInt();
        System.out.print("輸入c:");
        c = in.nextInt();
        if (a == b || a == c || b == c) {
            System.out.print("你輸入的數字中有相等的,程序結束。");
            System.exit(0);
        }
        if (a > b) {
            if (a > c) {
                // ************* a>c *************
                if (b > c) {
                    System.out.print("a>b>c");
                } else {
                    System.out.print("a>c>b");
                }
                // ************* a>c *************
            } else {
                System.out.print("c>a>b");
            }
        } else {

            if (b > c) {
                if (a > c) {
                    System.out.print("b>a>c");
                } else {
                    System.out.print("b>c>a");
                }
            } else {
                System.out.print("c > b > a");
            }

        }

    }

}

 10.水仙花數:用程序找出每位數的立方和等於該數本身值的所有的3位數

import java.util.Scanner;

//用程序找出每位數的立方和等於該數本身值的所有的3位數
public class Ten {

	public static void main(String[] args) {
		// TODO 自動生成的方法存根

		Scanner inScanner = new Scanner(System.in);
		int number, number_cal, a, b, c;
		// 先 確定該數的位數,可代碼實現,為了方便可手動從控制台輸入
		System.out.print("輸入一個三位數數字:");
		number_cal = number = inScanner.nextInt();
		a = number_cal / 100;
		number_cal = number_cal - a * 100;
		b = number_cal / 10;
		c = number_cal - b * 10;
		if ((a * a * a + b * b * b + c * c * c) == number) {
			System.out.print(number + "是水仙花數");
		} else {
			System.out.print(number + "不是水仙花數");
		}
	}
}

11.計算1累加到n(n>=2的整數)的和

import java.util.Scanner;

//計算1 加到n ( n>=2的整數)的總和
public class Eleven {

	public static void main(String[] args) {
		// TODO 自動生成的方法存根
		int number, sum = 0;
		Scanner in = new Scanner(System.in);
		System.out.print("輸入一個>=2的整數:");
		number = in.nextInt();
		for (int i = 1; i <= number; i++) {
			sum = sum + i;
		}
		System.out.print("結果是" + sum);
	}
}

12.得到一個整數的絕對值

import java.util.Scanner;

//得到一個整數的絕對值
public class Twelve {

	public static void main(String[] args) {
		// TODO 自動生成的方法存根
		int number;
		Scanner inScanner = new Scanner(System.in);
		System.out.print("輸入一個整數:");
		number = inScanner.nextInt();
		if (number < 0) {
			number = 0 - number;
		}
		System.out.print("絕對值是:" + number);
	}

}

  

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM