java学习4

首先,通过看一些java入门教程对于JAVA有一定的了解之后,我要看一些java学习中的一些简单的例子,现在和大家分享几个实例。

1,编写程序,判断给定的某个年份是否是闰年。
      闰年的判断规则如下:
      (1)若某个年份能被4整除但不能被100整除,则是闰年。
      (2)若某个年份能被400整除,则也是闰年。

import java.util.Scanner;
class Bissextile{
    public static void main(String[] arge){
        System.out.print("请输入年份");
    int year;    //定义输入的年份名字为“year”
    Scanner scanner = new Scanner(System.in);
    year = scanner.nextInt();
    if (year<0||year>3000){
        System.out.println("年份有误,程序退出!");
        System.exit(0);
        }
    if ((year%4==0)&&(year%100!=0)||(year%400==0))
        System.out.println(year+" is bissextile");
    else
        System.out.println(year+" is not bissextile ");
    }
}

 

2,给定一个百分制的分数,输出相应的等级。
        90分以上        A级
        80~89          B级
        70~79          C级
        60~69          D级
        60分以下        E级

import java.util.Scanner;
class Mark{
    public static void main(String[] args){
        System.out.println("请输入一个分数");
        //定义输入的分数为“mark”,且分数会有小数
        double mark;
        Scanner scanner = new Scanner(System.in);
        mark = scanner.nextDouble();

        //判断是否有输入错误。
        if(mark<0||mark>100){
           System.out.println("输入有误! ");
           System.exit(0);
        }
        /*判断分数的等级
        90分以上者A级, 80~89分者 B级,70~79分者 C级, 60~69者 D级,60分以下 E级 */
        if (mark>=90) System.out.println("this mark is grade \'A\' ");
        else if (mark>=80) System.out.println("this mark is grade \'B\' ");
        else if (mark>=70) System.out.println("this mark is grade \'C\' ");
        else if (mark>=60) System.out.println("this mark is grade \'D\' ");
        else  System.out.println("this mark is grade \'E\' ");
    }
}

 

3,编写程序求 1+3+5+7+……+99 的和值。

class he{
    public static void main(String[] args){
        int number = 1;  //初始值1,以后再+2递增上去
        int sum = 0;
        for ( ; number <100; number+=2 ){ sum += number; }
        System.out.println("1+3+5+7+……+99= " +sum);
    }
}

 

4、利用for循环打印 9*9  表?
1*1=1
1*2=2  2*2=4
1*3=3  2*3=6  3*3=9
1*4=4  2*4=8  3*4=12  4*4=16
1*5=5  2*5=10  3*5=15  4*5=20  5*5=25
1*6=6  2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
1*7=7  2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
1*8=8  2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
1*9=9  2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81
 
//循环嵌套,打印九九乘法表
public class NineNine{
    public static void main(String[]args){
    System.out.println();
    for (int j=1;j<10;j++){
        for(int k=1;k<10;k++) {   //老师的做法,判断语句里的 k<=j,省去下列的if语句。
            if (k>j) break;       //此处用 continue也可以,只是效率低一点
            System.out.print(" "+k+"X"+j+"="+j*k);
         }
        System.out.println();
        }
    }
}

java学习书籍中会有很多会有很例子分析,讲解会比较明确,也是可以买来看看的,现在列举的例子可以自己来研究一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值