日撸Java三百行

Java重开第一周(01-06)

day01:环境搭建和Hello World

day02:基本算术操作

day03:基本if语句和函数调用

day04:if应用-闰年判断

day05:Switch操作

day06:For循环操作

day01:环境搭建和Hello World

在这里插入图片描述

day02:基本算术操作

在这里插入图片描述
关于Java变量命名:
一般将方法和变量的标识符按照小驼峰来写,如变量tempFirsInt;
类名的标识符一般用大驼峰来写,如BasicOperations;
关于注释:
代码区域包括数据声明,数据初始化,数据操作三个部分,且用注释来说明功能。
例如://Modulus表示取余操作
//Of main 表示main方法结束

day03:基本if语句和函数调用

在这里插入图片描述
运行结果
在这里插入图片描述
本题中将常用的取绝对值方法单独取出来,并且实现成一个方法。该方法用static修饰,表示该方法与类同时产生,此时不能通过对象的方式来调用此方法(在本类中中还没有new出一个对象),这里其实用到了封装的思想。
在这里插入图片描述

day04:if应用-闰年判断

package basic;

public class LeapYear {
    /*
    The entrance of the program.
    @param args Not used now.
     */
    public static void main(String[] args) {
        //Test is LeapYear
        int tempYear = 2021;
        System.out.println("" + tempYear + " is ");
        if (!isLeapYear(tempYear)) {
            System.out.println("NOT");
        }//Of if
        System.out.println("a leap year.");
        tempYear = 2000;
        System.out.println("" + tempYear + " is ");
        if (!isLeapYear(tempYear)) {
            System.out.println("NOT");
        }//Of if
        System.out.println("a leap year.");
        tempYear = 2100;
        System.out.println("" + tempYear + " is ");
        if (!isLeapYear(tempYear)) {
            System.out.println("NOT");
        }//Of if
        System.out.println("a leap year.");
        tempYear = 2004;
        System.out.println("" + tempYear + " is ");
        if (!isLeapYear(tempYear)) {
            System.out.println("NOT");
        }//Of if
        System.out.println("a leap year.");
        //Test isLeapYearV2
        System.out.println("Now we use the second version.");
        tempYear = 2021;
        System.out.println("" + tempYear + " is ");
        if (!isLeapYearV2(tempYear)) {
            System.out.println("NOT");
        }//Of if
        System.out.println("a leap year.");
        tempYear = 2000;
        System.out.println("" + tempYear + " is ");
        if (!isLeapYearV2(tempYear)) {
            System.out.println("NOT");
        }//Of if
        System.out.println("a leap year.");
        tempYear = 2100;
        System.out.println("" + tempYear + " is ");
        if (!isLeapYearV2(tempYear)) {
            System.out.println("NOT");
        }//Of if
        System.out.println("a leap year.");
        tempYear = 2004;
        System.out.println("" + tempYear + " is ");
        if (!isLeapYearV2(tempYear)) {
            System.out.println("NOT");
        }//Of if
        System.out.println("a leap year.");
    }//Of main
    /*
    Is the given year leap?
    @param paraYear The given year
     */
    public static boolean isLeapYear(int paraYear)
    {
        if((paraYear%4==0&&paraYear%100!=0)||paraYear%400==0)
        {
            return true;
        }
        else{
            return false;
        }//Of if
    }//Of isLeapYear
    /*
    Is the given year leap? Replace the complex condition with a number of if
    @param paraYear The given year.
     */
    public static boolean isLeapYearV2(int paraYear)
    {
        if(paraYear%4==0){
            return false;
        }else if(paraYear %400==0){
            return true;
        }else if(paraYear%100==0)
        {
            return false;
        }
        else{
            return true;
        }//Of if
    }//Of isLeapYearV2
}//Of class LeapYear

根据百度:非整百年:能被四整除的年是闰年
整百年:能被四百整除的是闰年
根据条件进行if逻辑条件抽象
方式一:if((paraYear%40&&paraYear%100!=0)||paraYear%4000)
{
return true;
}
else{
return false;
}//Of if
方式二: if(paraYear%40){
return false;
}else if(paraYear %400
0){
return true;
}else if(paraYear%100==0)
{
return false;
}
else{
return true;
}//Of if
运行结果:

在这里插入图片描述

day05:Switch操作

package basic;

public class SwitchStatement {
    /*
    The entrance of the program.
    @param args Not used now.
     */
    public static void main(String[] args) {
        scoreTolevelTest();
    }//Of main

    /*
    Score to level.
    @ param  paraScore From 0 to 100.
    @ param The level A to F.
     */
    public static char scoreTolevelTest(int paraScore) {
        //E stands for error ,and F stands for fail.
        char resultLevel = 'E';
        //Divided by 10, the result ranges from 0 to 10
        int tempDigitalLevel = paraScore / 10;
        //The use of break is important.
        switch (tempDigitalLevel) {
            case 10:
            case 9:
                resultLevel = 'A';
                break;
            case 8:
                resultLevel = 'B';
                break;
            case 7:
                resultLevel = 'C';
                break;
            case 6:
                resultLevel = 'D';
                break;
            case 5:
            case 4:
            case 3:
            case 2:
            case 1:
            case 0:
                resultLevel = 'F';
                break;
            default:
                resultLevel = 'E';
        }//Of switch
        return resultLevel;
    }//OF scoreToLevel

    /*
    Method unit test.
     */
    public static void scoreTolevelTest() {
        int tempScore = 100;
        System.out.println("Score "+tempScore+"to level is: "+scoreTolevelTest(tempScore));
        tempScore=91;
        System.out.println("Score "+tempScore+"to level is:"+scoreTolevelTest(tempScore));
        tempScore=82;
        System.out.println("Score "+tempScore+"to level is:"+scoreTolevelTest(tempScore));
        tempScore=75;
        System.out.println("Score "+tempScore+"to level is:"+scoreTolevelTest(tempScore));
        tempScore=66;
        System.out.println("Score "+tempScore+"to level is:"+scoreTolevelTest(tempScore));
        tempScore=52;
        System.out.println("Score "+tempScore+"to level is:"+scoreTolevelTest(tempScore));
        tempScore=8;
        System.out.println("Score "+tempScore+"to level is:"+scoreTolevelTest(tempScore));
        tempScore=120;
        System.out.println("Score "+tempScore+"to level is:"+scoreTolevelTest(tempScore));
    }//OF scoretoLevelTest
}//of class SwitchStatement


switch相当与多个if else的作用,可以实现单个信息的多段区分判断,常用于分数评级等题目中,若分段比较多,使用switch比较方便。
switch用法重点:当case条件语句匹配后只有遇到break才会跳出switch作用域,
default是所有case的条件都不匹配的时候,执行此语句。

day06:For循环操作

package basic;
/**
 * @author Donghao Xu
 */
public class ForStatement {
    /**The entrane of the program.
     *
     * @param args Not used now.
     */
    public static void main(String[] args) {
        forStatementTest();
    }//Of main
    /*
    Method unit test
     */
    public  static void forStatementTest(){
        int temN=10;
        System.out.println("1 add to "+temN+" is "+addToN(temN));
        temN=0;
        System.out.println("1 add to "+temN+" is "+addToN(temN));
        int tempStepLength=1;
        temN=10;
        System.out.println("1 add to "+temN+" with step length "+tempStepLength+" is:"+addToWithStepLength(temN,tempStepLength));
        tempStepLength=2;
        System.out.println("1 add to"+temN+" with step length "+tempStepLength+" is:"+addToWithStepLength(temN,tempStepLength));
    }//Of forStatementTest
    /*
    Add from 1 to N.
    @param paraN The given upper bound.
    @return The sum.
     */
   public static int addToN(int paraN) {
       int resultSum = 0;
       for (int i = 1; i <= paraN; i++) {
           resultSum += i;
       }//Of  for i
       return resultSum;
   }//Of addToN
    /*
         Add from 1 to N with a step length.
         @param paraN The given upper bound.
         @param paraStepLength The given step length.
         @ return The sum.
     */
    public static  int addToWithStepLength(int paraN,int paraStepLength)
    {
        int resultSum=0;
        for(int i=1;i<=paraN;i+=paraStepLength)
        {
            resultSum+=i;
        }//Of for i
        return resultSum;
    }//Of addToWithsStepLength
}//of class ForStatement.



运行结果

在这里插入图片描述

for(初始化; 布尔表达式; 更新) {
    //代码语句
}

注意两个方法的运算不相同
addToN():从1到N,步长为1进行计算
addToWithStepLength():从1到N,步长为指定的参数,即for循环里的更新操作不同。
Java增强for循环

for(数据类型  变量名 : 数组或者集合对象){
		//循环体,变量即元素
}
int[] numArray = { 1, 2, 3, 4, 5, 6 };
for (int i : numArray) {
    System.out.print(i);
}
//结果会是123456 

关于增强for循环的使用
link.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值