java简介

Java 是由 Sun Microsystems 在 1995 年首先发布的编程语言和计算平台。有许多应用程序和 Web 站点只有在安装 Java 后才能正常工作,而且这样的应用程序和 Web 站点日益增多。Java 快速、安全、可靠。从笔记本电脑到数据中心,从游戏控制台到科学超级计算机,从手机到互联网,Java 无处不在!
编程环境
Java概念图JDK(Java Development Kit)称为Java开发包或Java开发工具,是一个编写Java的Applet小程序和应用程序的程序开发环境。JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一些Java工具和Java的核心类库(Java API)。不论什么Java应用服务器实质都是内置了某个版本的JDK。主流的JDK是Sun公司发布的JDK,除了Sun之外,还有很多公司和组织都开发了自己的JDK,例如,IBM公司开发的JDK,BEA公司的Jrocket,还有GNU组织开发的JDK。 另外,可以把Java API类库中的Java SE API子集和Java虚拟机这两部分统称为JRE(JAVA Runtime Environment),JRE是支持Java程序运行的标准环境。 JRE是个运行环境,JDK是个开发环境。因此写Java程序的时候需要JDK,而运行Java程序的时候就需要JRE。而JDK里面已经包含了JRE,因此只要安装了JDK,就可以编辑Java程序,也可以正常运行Java程序。但由于JDK包含了许多与运行无关的内容,占用的空间较大,因此运行普通的Java程序无须安装JDK,而只需要安装JRE即可。

第二章 Java基本语法(下):程序流程控制

  • 流程控制语句是用来控制程序中各语句执行顺序的语句,可以把语句组合成能完成一定功能的小逻辑模块。
  • 其流程控制方式采用结构化程序设计中规定的三种基本流程结构,即:顺序结构,分支结构,循环结构
顺序结构:程序从上到下逐行地执行,中间没有任何判断和跳转;

分支结构:根据条件,选择性的执行某段代码;有if…else和switch-case两种分支语句。

循环结构:根据循环条件,重复性的执行某段代码;有while、do…while、for三种循环语句;注:JDK1.5提供了foreach循环,方便遍历集合、数组元素。

练习:

class OperatorTest{
     public static void main(String[] args) {
         boolean x = true;
         boolean y = false;
         short z = 40;
         if ((z++ == 40) && (y = true)){
             z++;
         }
         if((x = false) || (++z == 43)){
             z++;
         }
         System.out.println("z = " + z);
     }
 }结果为:44

3.定义三个整型数据,使用if–else语句比较其中最大值,并输出

class NumTest{
	public static void main(String[] args) {
		int num1 = 55;
		int num2 = -110;
		int num3 = 93; 
		int max; 
		if (num1 >= num2 && num1 >= num3){
			max = num1; 
		}else if(num2 >= num1 && num2 >=num3){
			max = num2; 
		}else{ 
			max = num3; 
		} 
		System.out.println("三个数中最大的为:" + max);
	}
 }

4.声明两个double型变量并赋值,判断第一个数大于10.0,且第二个数大于20.0,打印两数之和,否则打印两数的积。

class NumTest{
     public static void main(String[] args){
                double n1 = 12.3;
		double n2 = 25.4; 
		if (n1 > 10.0 && n2 > 20.0){
			System.out.println("两个数的和为:" + (n1 + n2)); 
		}else{ 
			System.out.println("两个数的积为:" + (n1 * n2)); 
		}
      }
}

5.交换两个变量值的代码实现。
方法一:

class TurnTest{
     public static void main(String[] args){
          String n1 = "北京";
          String n2 = "南京";
          String temp;
          temp = n1;
          n1 = n2;
          n2 = temp;
          System.out.println("n1:" + n1 );
          System.out.println("n2:" + n2 );
      }
 }

方法二:

class TurnTest{
     public static void main(String[] args){
          int n1 = 10;
          int n2 = 30;
          n1 = n1 + n2;
          n2 = n1 - n2;
          n1 = n1 - n2;
          System.out.println("n1:" + n1 );
          System.out.println("n2:" + n2 );
     }
 }

方法三:

class TurnTest{
     public static void main(String[] args){
          int n1 = 10;
          int n2 = 30;
          n1 = n1 ^ n2;
          n2 = n1 ^ n2;
          n1 = n1 ^ n2;
          System.out.println("n1:" + n1 );
          System.out.println("n2:" + n2 );
     }
 }

//最快计算8

class ChenjiTest{
     public static void main(String[] args){
          int num = 1;
          System.out.println(num <<< 3);
     }
 }





例题 2

import java.util.Scanner;
class  IfTest2{
	//从小到大排列
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入第一个数:");
		int num1 = scan.nextInt();
		System.out.println("请输入第二个数:");
		int num2 = scan.nextInt();
		System.out.println("请输入第三个数:");
		int num3 = scan.nextInt();
		
		if (num1 >= num2){
			if(num3 >= num1){
				System.out.println(num2 + "," + num1 + "," +num3);
			}else if (num2 >= num3){
				System.out.println(num3 + "," + num2 + "," +num1);
			}else{
				System.out.println(num2 + "," + num3 + "," +num1);
			}
		}else{
			if(num3 >= num2){
				System.out.println(num1 + "," + num2 + "," +num3);
			}else if (num3 >= num1){
				System.out.println(num3 + "," + num1 + "," +num2);
			}else{
				System.out.println(num1 + "," + num3 + "," +num2);
			}
		}
	}
}
1)
class PrintHello{
     public static void main(String[] args) {
         int a = 25;
         int b = 30;
         if ((a + b) >= 50){
             System.out.println("Hello World!");
         }
         System.out.println(a + b);
     }
 }
class AndPrint{
     public static void main(String[] args) {
         double a = 15.3;
         double b = 12.4;
         if (a > 10.0 && b < 20.0){
             System.out.println(a + b);
         }else{
             System.out.println(a * b);
         }
     }
 }
//输入狗狗年龄查询
import java.util.Scanner;
 class DogAge{
     public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in);
         System.out.println("请输入狗狗年龄:");
         double dogage = scanner.nextDouble();
         if (dogage > 0 && dogage <= 2){
             System.out.println("狗狗年龄相当于人类的年龄:" + (dogage * 10.5) +  "岁" );
         }else if (dogage >2 && dogage < 25){ 
            System.out.println("狗狗年龄相当于人类的年龄:" + ((2 * 10.5) + (dogage-2) * 4) +  "岁" );
         }else if (dogage < 0 || dogage >= 25){
             System.out.println("狗狗年龄有误!");
         }
     }
}

练习:获取 10 – 99之间的随机整数。

public class GetRandom{
     public static void main(String[] args) {
         int value = (int)(Math.random() * 90 + 10);
         System.out.println(value);
     }
 }
import java.util.Scanner;
class TestCaiPiao {
	public static void main(String[] args) {
		//1.产生一个两位数
		int number=(int)(Math.random()*90+10);
		int numberShi = number/10;
		int numberGe = number%10;

		//2.用户输入一个两位数
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个两位数");
		int guess = input.nextInt();
		int guessShi = guess/10;
		int guessGe = guess%10;

		if(number==guess){
			System.out.println("奖金一万美金!");
		}else if(numberShi == guessGe && numberGe == guessShi){
			System.out.println("奖金五千美金!");
		}else if (numberShi == guessShi || numberGe == guessGe){
			System.out.println("奖金一千美金!");
		}else if (numberShi == guessGe || numberGe == guessShi){
			System.out.println("奖金五百美金!");
		}else{
			System.out.println("很遗憾,您没有中奖!");
		}
		System.out.println("中奖号码是:"+ number);
	}
}

import java.util.Scanner;
class  IfExer1{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入你的身高:(cm)");
		int height = scan.nextInt();
		System.out.println("请输入你的财富:(千万)");
		double wealth = scan.nextDouble();
	/*	System.out.println("请输入你是否帅:(true/false)");
		boolean isHandsome = scan.nextBoolean();

		if (height >=180 && wealth >= 1 && isHandsome){
			System.out.println("我一定要嫁给他!!!");
		}else if (height >=180 || wealth >= 1 || isHandsome){
			System.out.println("嫁吧,比上不足比下有余!");
		}else{
			System.out.println("不嫁!");
		}
         */
         System.out.println("请输入你是否帅:(是/否)");
		String isHandsome = scan.next();

		if (height >=180 && wealth >= 1 && isHandsome.equals("是")){
			System.out.println("我一定要嫁给他!!!");
		}else if (height >=180 || wealth >= 1 || isHandsome.equals("是")){
			System.out.println("嫁吧,比上不足比下有余!");
		}else{
			System.out.println("不嫁!");
		}
	}
}

 2-5-3分支语句2:switch-case结构

class SwithcCaseTest{
	public static void main(String[] args) {
		int number = 3;
		switch(number){
			case 0:
				System.out.println("zero");
				break;
			case 1:
				System.out.println("one");
				break;
			case 2:
				System.out.println("two");
				break;
			case 3:
				System.out.println("three");
				break;
			default:
				System.out.println("other");
		}		
	}
}
class SwithCaseTest1{
	public static void main(String[] args) {
		String season = "summer";
		switch(season){
			case "spring":
				System.out.println("春暖花开");
				break;
			case "summer":
				System.out.println("夏日炎炎");
				break;
			case "autumn":
				System.out.println("秋高气爽");
				break;
			case "winter":
				System.out.println("冬雪皑皑");
				break;
			default:
				System.out.println("季节输入有误!");
		}		
	}
}

import java.util.Scanner;
class SwitchCaseTest2 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入想要转为大写的字母:(a~e)");
		String word = scan.next();
		//获取字符串中的某个字节
		char c = word.charAt(0);
		switch (c){
		case 'a':
			System.out.println("A");
			break;
		case 'b':
			System.out.println("B");
			break;
		case 'c':
			System.out.println("C");
			break;
		case 'd':
			System.out.println("D");
			break;
		case 'e':
			System.out.println("E");
			break;
		default :
			System.out.println("Other");
		}
	}
}
import java.util.Scanner;
class SwitchCaseTest3 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入你的成绩:");
		int score = input.nextInt();
		//相同的执行语句,可以合并
	/*	switch(score / 10){
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
				System.out.println("成绩不合格!");
				break;
			case 6:
			case 7:
			case 8:
			case 9:
			case 10:
				System.out.println("成绩合格!");
				break;
		}
	*/
	//代码优化为
		switch(score / 60){
			case 0:
				System.out.println("成绩不合格!");
				break;
			case 1:
				System.out.println("成绩合格!");
				break;
		}
	}
}
import java.util.Scanner;
class SwitchCaseTest4 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入月份:");
		int mon = input.nextInt();
		
		switch(mon){
			case 12:				
			case 1:
			case 2:
				System.out.println("现在是冬季");
				break;
			case 3:
			case 4:
			case 5:
				System.out.println("现在是春季");
				break;
			case 6:
			case 7:
			case 8:
				System.out.println("现在是夏季");
				break;
			case 9:
			case 10:
			case 11:
				System.out.println("现在是秋季");
				break;
			default:
				System.out.println("月份输入有误");
				break;
		}
	}
}
import java.util.Scanner;
class SwitchCaseTest6 {
	//计算2020年中某月某日是当年的第几天,暂时不考虑日期超范围错误
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入年份:");
		int year = input.nextInt();
		System.out.println("请输入月份:");
		int month = input.nextInt();
		System.out.println("请输入号数:");
		int day = input.nextInt();
		//定义变量接收天数
		int sumDays = 0;

		switch (month){
			case 12:
				sumDays +=30;
			case 11:
				sumDays +=31;
			case 10:
				sumDays +=30;
			case 9:
				sumDays +=31;
			case 8:
				sumDays +=31;
			case 7:
				sumDays +=30;
			case 6:
				sumDays +=31;
			case 5:
				sumDays +=30;
			case 4:
				sumDays +=31;
			case 3:
				if ((year % 4 ==0 && year % 100 != 0) || year % 400 ==0){
					sumDays += 29;
				}else{
					sumDays += 28;
				}
			case 2:
				sumDays +=31;
			case 1:
				sumDays += day;
				break;
			default:
				System.out.println("输入有误!");
				break;
		}
		System.out.println(year + "年" + month + "月" + day+ "日,是当年的第" + sumDays + "天!");
	}
}

class SwitchCaseTest7{
	public static void main(String[] args) {
		int a = 0;
		int x = 100;
		switch (a){
		case 1:
			x += 5;
		case 2:
			x += 10;
		case 3:
			x += 16;
		default:
			x += 34;		
		}
		System.out.println(x);
	}
}

import java.util.Scanner;
class SwitchCaseTest8 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入成绩:");
		int score = input.nextInt();	
		switch (score / 10){
			case 10:	
			case 9:
				System.out.println("A");
				break;
			case 8:
			case 7:
				System.out.println("B");
				break;
			case 6:
				System.out.println("C");
				break;
			case 5:				
			case 4:		
			case 3:
			case 2:
			case 1:
			case 0:
				System.out.println("D");
				break;
			default:
				System.out.println("输入有误!");
				break;
		}
	}
}

import java.util.Scanner;
class SwitchCaseTest9 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入年份:");
		int year = input.nextInt();	
		switch (year % 12){
			case 4:
				System.out.print("是(rat)鼠年");
				break;
			case 5:
				System.out.print("是(ox)牛年");
				break;
			case 6:
				System.out.print("是(tiger)虎年");
				break;
			case 7:
				System.out.print("是(rabbit)兔年");
				break;
			case 8:
				System.out.print("是(dragon)龙年");
				break;
			case 9:
				System.out.print("是(snake)蛇年");
				break;
			case 10:
				System.out.print("是(horse)马年");
				break;
			case 11:
				System.out.print("是(sheep)羊年");
				break;		
			case 12:
			case 0:
				System.out.print("是(monkey)猴年");
				break;
			case 1:
				System.out.print("是(rooster)鸡年");
				break;
			case 2:
				System.out.print("是(dog)狗年");
				break;
			case 3:
				System.out.print("是(pig)猪年");
				break;
			default:
				System.out.println("输入的年份有误!");
				break;
		}
	}
}

程序流程控制:循环结构

 循环结构:在某些条件满足的情况下,反复执行特定代码的功能

循环语句分类:for循环

while循环

do -while循环

for循环:

class ForTest{
	public static void main(String[] args) {
		//循环输出
		for (int i = 1;i<=5 ;i++ ){
			System.out.println("Hello World!"+ i);
		}
		//练习:
		int num =1;
		for (System.out.print("a");num <=3;System.out.println("c"),num++){
			System.out.print("b");
		}
		System.out.println("*********************");
		//例题:遍历100以内的偶数,输出所有偶数的和,输出偶数的个数
		int sum = 0;
		int count = 0;
		for(int i = 1;i<=100;i++){
			if(i %2==0){
				System.out.println(i);
				sum +=i;
				count ++;
			}		
		}
		System.out.println("100以内的偶数的总和是:" + sum);
		System.out.println("100以内的偶数的个数是:" + count);
	}
}

class ForTest1{
	public static void main(String[] args) {
		for(int i = 1;i<=150;i++){
			System.out.print(i+"  ");
			if (i % 3 == 0){
				System.out.print("foo ");
			}
			if (i % 5 == 0){
				System.out.print("biz ");
			}
			if (i % 7 == 0){
				System.out.print("baz ");
			}
			System.out.println();
		}	
	}
}

import java.util.Scanner;
class ForTest2 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入第一个正整数:");
		int m = scan.nextInt();
		System.out.println("请输入第二个正整数:");
		int n = scan.nextInt();
		System.out.println("您输入了两个正整数分别是:" + m + "和" + n);
		
		//获取最大公约数
		//1.取两个数中的较小值
		int min  = (m < n)? m : n;
		//2.遍历
		for (int i = min;i >= 1;i-- ){
			if (m % i ==0 && n % i ==0){
				System.out.println("最大公约数是:" + i);
				break;
			}
		}
		//获取最小公倍数
		//1.取两个数中的较大值
		int max = (m > n)? m : n;
		//2.遍历
		for(int i = max;i <= m * n;i++){
			if (i % m == 0 && i % n == 0){
				System.out.println("最小公倍数是:" + i);
				break;
			}
		}
	}
}

1.
class ForTest3{
	public static void main(String[] args) {
		//例题:遍历100以内的奇数,输出所有奇数的和,输出奇数的个数
		int sum = 0;
		int count = 0;
		for(int i = 1;i<=100;i++){
			if(i % 2 != 0){
				System.out.println(i);
				sum +=i;
				count ++;
			}		
		}
		System.out.println("100以内的奇数的总和是:" + sum);
		System.out.println("100以内的奇数的个数是:" + count);
	}
}

2.
class ForTest4{
	public static void main(String[] args) {
		int sum = 0;
		int count = 0;
		for(int i = 1;i <= 100;i++){
			if (i % 7 ==0){
				System.out.println(i);
				sum += i;
				count++;
			}
		}
		System.out.println("1到100之间是7的倍数的整数有 " + count + "个");
		System.out.println("1到100之间是7的倍数的整数的和是" + sum);
	}
}
3.
class ForTest5 {
	public static void main(String[] args) {
		System.out.println("所谓水仙花数是指一个三位数,它的各个位上的数的立方之和等于它本身");
		System.out.println("所有的水仙花数如下:");
		int count = 0;
		for (int i = 100;i < 1000 ;i++){
			int b = i / 100 % 10;	//百位上的数
			int s = i / 10 % 10;	//十位上的数
			int g = i % 10;			//个位上的数		
			if(i == (b*b*b + s*s*s + g*g*g)){
				System.out.println(i);
				count++;
			}
		}
		System.out.println("共有 " + count + " 个水仙花数");
	}
}
分类: Java