栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

日撸Java三百行

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

日撸Java三百行

Java重开第一周(01-06)

基本语法

Java重开第一周(01-06) day01:环境搭建和Hello Worldday02:基本算术操作day03:基本if语句和函数调用day04:if应用-闰年判断day05:Switch操作day06:For循环操作day01:环境搭建和Hello Worldday02:基本算术操作day03:基本if语句和函数调用day04:if应用-闰年判断day05:Switch操作day06:For循环操作

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 {
    
    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
    
    public static boolean isLeapYear(int paraYear)
    {
        if((paraYear%4==0&¶Year%100!=0)||paraYear%400==0)
        {
            return true;
        }
        else{
            return false;
        }//Of if
    }//Of isLeapYear
    
    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&¶Year%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 {
    
    public static void main(String[] args) {
        scoreTolevelTest();
    }//Of main

    
    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

    
    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;

public class ForStatement {
    
    public static void main(String[] args) {
        forStatementTest();
    }//Of main
    
    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
    
   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
    
    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.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/780772.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号