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

java 01~~05

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

java 01~~05

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

前言Day01——从“Hello Would”开始吧Day02——基本算术操作

(四则运算)代码如下:运行结果 Day03——基本if语句

(求绝对值)代码如下:运行结果小结 Day04——if语句嵌套

(闰年的计算)小结 Day05——基本switch语句

(判断分数的对应等级)运行结果小结


前言
Day01——从“Hello Would”开始吧
public class HelloWould {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        System.out.println("Hello Would!");
	}// Of main
}// Of class HelloWould
Day02——基本算术操作 (四则运算)代码如下:
public class math {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        int tempFirstInt = 15,
            tempSecondInt = 4,
            tempResultInt;
        double tempFirstDouble= 1.2,
        	   tempSecondDouble = 3.5,
        	   tempResultDouble;
        //  +
        tempResultInt = tempFirstInt + tempSecondInt;
        tempResultDouble = tempFirstDouble + tempSecondDouble;
        
        System.out.println(tempFirstInt + " + " + tempSecondInt + " = " + tempResultInt);
		System.out.println(tempFirstDouble + " + " + tempSecondDouble + " = " + tempResultDouble);
        //  -
		tempResultInt = tempFirstInt - tempSecondInt;
        tempResultDouble = tempFirstDouble - tempSecondDouble;
        
        System.out.println(tempFirstInt + " - " + tempSecondInt + " = " + tempResultInt);
		System.out.println(tempFirstDouble + " - " + tempSecondDouble + " = " + tempResultDouble);
		//  *(乘)
		tempResultInt = tempFirstInt * tempSecondInt;
        tempResultDouble = tempFirstDouble * tempSecondDouble;
        
        System.out.println(tempFirstInt + " * " + tempSecondInt + " = " + tempResultInt);
		System.out.println(tempFirstDouble + " * " + tempSecondDouble + " = " + tempResultDouble);
		//  /(除)
		tempResultInt = tempFirstInt / tempSecondInt;
        tempResultDouble = tempFirstDouble / tempSecondDouble;
        
        System.out.println(tempFirstInt + " / " + tempSecondInt + " = " + tempResultInt);
		System.out.println(tempFirstDouble + " / " + tempSecondDouble + " = " + tempResultDouble);
		//  %(取余)
        tempResultInt = tempFirstInt % tempSecondInt;
		System.out.println(tempFirstInt + " % " + tempSecondInt + " = " + tempResultInt);
	}//Of main 

}//Of class math
运行结果


Day03——基本if语句 (求绝对值)代码如下:
public class HelloWould {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int tempNumber1 = 6;
		System.out.println("The absolute value of " + tempNumber1 + " is " + abs(tempNumber1));
		int tempNumber2 = -8;
		System.out.println("The absolute value of " + tempNumber2 + " is " + abs(tempNumber2));
	}// Of main
	
	public static int abs(int paraValue) {
		if (paraValue >= 0) {
			return paraValue;
		} else {
			return -paraValue;
		} // Of if
	}// Of abs
}

运行结果

小结

if-else语句在Java编程中较为常用,是常见的多分支语句。上段程序用于求一个整型数的绝对值,取绝对值的部分单独作为一个静态方法,通过引用方法传入实参,可让方法返回一个值,这个值即为所给数的绝对值。


Day04——if语句嵌套 (闰年的计算)
   //闰年的判断条件:可被4整除但不能同时被100整除,或者能被400整除
    public class LeapYear {
    public static void main(String args[]) {
    
   // Test isLeapYear
		int tempYear = 2021;
		System.out.print("" + tempYear + " is ");
		if (!isLeapYear(tempYear)) {
			System.out.print("NOT ");
		} // Of if
		System.out.println("a leap year.");
		
  // Test isLeapYearV2
		System.out.println("Now use the second version.");
		
		System.out.print("" + tempYear + " is ");
		if (!isLeapYearV2(tempYear)) {
			System.out.print("NOT ");
		} // Of if
		System.out.println("a leap year.");
		
  //下面是包含双分支if—-else的方法
    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
	
   //下面是包含多分支if-else的方法
      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
}
小结

1.在一个分支中又完整的嵌套了另一个完整的分支结构,里面的分支结构称为内层分支,外面的分支结构称为外层分支。
2.嵌套最好不要超过3层,否则可读性差

Day05——基本switch语句 (判断分数的对应等级)
package basic;


public class SwitchStatement {

	public static void main(String args[]) {
		scoreToLevelTest();
	}// Of main

	
	public static char scoreToLevel(int paraScore) {
		// E stands for error, and F stands for fail.
		char resultLevel = 'E';

		// Divide 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: " + scoreToLevel(tempScore));

		tempScore = 91;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

		tempScore = 82;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

		tempScore = 75;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

		tempScore = 66;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

		tempScore = 52;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

		tempScore = 8;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

		tempScore = 120;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
	}// Of scoreToLevelTest
}// Of class SwitchStatement

运行结果

小结

1.switch(表达式)中,表达式的数据类型应该和case后的常量类型一致,或者是可以自动转换成可以相互比较的类型,如常量为int,而输入是字符。
2.switch(表达式)中,表达式的返回值必须是:(byte,short,int,char,String,enum(枚举))
3.case子句中的值必须是常量,而不能是变量
4.可以没有default
5.break语句执行后跳出该switch。如果没有break,则程序会顺序执行到switch结尾

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

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

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