Fork me on GitHub

java 经典程序 100 例

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 }
View Code

 

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 }
View Code

 

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 }
View Code

 

posted @ 2016-11-15 14:44  洋葱源码  阅读(16060)  评论(0编辑  收藏  举报