1,編寫程序,判斷給定的某個年份是否是閏年。
閏年的判斷規則如下:
( 1)若某個年份能被 4 整除但不能被 100 整除,則是閏年。
( 2)若某個年份能被 400 整除,則也是閏年。
1 import java.util.Scanner; 2 3 public class isLearpYear { 4 public void is_leap(){ 5 Scanner input = new Scanner(System.in); 6 while (true) { 7 System.out.print("請輸入年份:"); 8 int year = input.nextInt(); 9 if ((year%4==0) && (year %100 != 0) || (year % 400==0)) { 10 System.out.println(year+"是閏年"); 11 break; 12 }else { 13 System.out.println(year+"不是閏年"); 14 15 } 16 } 17 } 18 19 public static void main(String[] args) { 20 isLearpYear years = new isLearpYear(); 21 years.is_leap(); 22 23 } 24 }
2,給定一個百分制的分數,輸出相應的等級。
90 分以上 A 級
80~89 B 級
70~79 C 級
60~69 D 級
60 分以下 E 級
1 import java.util.Scanner; 2 public class scoreLevel{ 3 Scanner input = new Scanner(System.in); 4 public void macthLeve(){ 5 System.out.print("請輸入你的分數:"); 6 int score = input.nextInt(); 7 if (score>90) { 8 System.out.print("A級 \n"); 9 }else if (score>80) { 10 System.out.print("B級 \n"); 11 }else if (score>70) { 12 System.out.print("C級 \n"); 13 }else if (score>60) { 14 System.out.print("D級 \n"); 15 }else { 16 System.out.print("E級 \n"); 17 } 18 19 } 20 21 22 23 public static void main(String[] args) { 24 scoreLevel score = new scoreLevel(); 25 score.macthLeve(); 26 27 } 28 }
3,編寫程序求 1+3+5+7+……+99 的和值。
1 public class sum{ 2 public void sum_99(){ 3 int i = 1; 4 int sum=0; 5 while (i < 100) { 6 sum = sum +i; 7 i +=2; 8 } 9 System.out.println("1+3+5+7+……+99 的和: "+sum); 10 } 11 12 13 public static void main(String[] args) { 14 sum s = new sum(); 15 s.sum_99(); 16 } 17 }
