2P35 顺序结构
2P36 选择结构
package com.hu.struct;
import java.util.Scanner;
// 顺序结构,依次执行
// 选择结构
/*
if 单选
if 双选
if 多选
嵌套if
switch多选
*/
public class P36 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
// 比较字符串
if (s.equals("Hello")){
System.out.println(s);
}
System.out.println("end");
scanner.close();
}
}
package com.hu.struct;
import java.util.Scanner;
public class P36_1 {
public static void main(String[] args) {
// 考试分数大于60就是及格,小于60就不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int mark = scanner.nextInt();
if (mark >= 60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close();
}
}
package com.hu.struct;
import java.util.Scanner;
public class P36_2 {
// 一旦其中一个else if 语句检测为true,其他的else if以及else都将跳过执行
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score == 100){
System.out.println("A+");
}else if (score >= 90 && score < 100){
System.out.println("A");
}else if (60 <= score && score <= 90){
System.out.println("B");
}else {
System.out.println("不及格");
}
scanner.close();
}
}
2P37 Switch选择结构
package com.hu.struct;
public class P37 {
public static void main(String[] args) {
char grade = 'F';
// 不写break会出现case穿透
// switch匹配一个具体的值
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
case 'C':
System.out.println("及格");
case 'D':
System.out.println("再接再厉");
case 'E':
System.out.println("挂科");
default:
System.out.println("未知等级");
}
}
}
package com.hu.struct;
public class P37_1 {
// JDK7新特性,表达式结果可以是字符串
// 字符的本质还是数字
// 反编译 java -- class字节码文件 -- 反编译(Idea)
public static void main(String[] args) {
String name = "jay";
switch (name){
case "gg":
System.out.println("gg");
break;
case "jay":
System.out.println("jay");
break;
default:
System.out.println("nothing");
}
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.hu.struct;
public class P37_1 {
public P37_1() {
}
public static void main(String[] args) {
String name = "jay";
byte var3 = -1;
switch(name.hashCode()) {
case 3296:
if (name.equals("gg")) {
var3 = 0;
}
break;
case 104994:
if (name.equals("jay")) {
var3 = 1;
}
}
switch(var3) {
case 0:
System.out.println("gg");
break;
case 1:
System.out.println("jay");
break;
default:
System.out.println("nothing");
}
}
}
2P38 while循环结构
package com.hu.struct;
public class P38 {
// 我们通常需要一个让表达式失效的方式来结束循环
public static void main(String[] args) {
// 输出1~100
int i = 0;
while (i <100){
i++;
System.out.println(i);
}
}
}
package com.hu.struct;
public class P38_1 {
public static void main(String[] args) {
// 少部分情况需要死循环,比如监听服务器请求
int i = 0;
int sum = 0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
2P39 do while 循环结构
package com.hu.struct;
public class P39 {
public static void main(String[] args) {
// while语句不满足条件就不能进入循环,但有时我们需要不满足条件也至少执行一次
// 这就是do while
// while先判断后执行,do while先执行后判断
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
package com.hu.struct;
public class P39_1 {
public static void main(String[] args) {
int a = 0;
while (a<0){
System.out.println(a);
a++;
}
System.out.println("================");
do {
System.out.println(a);
a++;
}while (a<0);
}
}
2P40 For循环结构
package com.hu.struct;
public class P40 {
public static void main(String[] args) {
// for(初始化;布尔表达式;更新)
int a = 1; //初始化
while (a<=100){
System.out.println(a); // 循环体
a+=2; // 迭代,更新
}
System.out.println("while循环结束");
// 初始化 条件判断 迭代
// For循环是最有效 最灵活的循环结构
for (int i=1;i<=100;i++){
System.out.println(i);
}
System.out.println("For循环结束");
// for (; ;)死循环
}
}
package com.hu.struct;
public class P40_1 {
public static void main(String[] args) {
// 0 到 100 之间的奇数和偶数的和
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i <= 100; i++) {
if (i % 2 != 0){
oddSum += i;
}else {
evenSum += i;
}
}
System.out.println("奇数和为:" + oddSum);
System.out.println("偶数和为:" + evenSum);
}
}
package com.hu.struct;
public class P40_2 {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
// print输出完不会华航,println输出完会换行
if (i % 5 == 0){
System.out.print(i+"\t");
}
if (i % 15 == 0){
System.out.println();
}
}
}
}
2P41 九九乘法表
package com.hu.struct;
public class P41 {
public static void main(String [] args){
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j + "*" + i + "=" + j*i + "\t");
}
System.out.println();
}
}
}
2P42 增强for循环
package com.hu.struct;
public class P42 {
// 用于遍历集合和数组
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
System.out.println(numbers[i]);
}
// 遍历数组元素
for (int x : numbers){
System.out.println(x);
}
}
}
2P43 break、continue、goto
package com.hu.struct;
public class P43 {
public static void main(String[] args) {
int i = 0;
while (i < 100){
i ++;
if (i == 30){
break;
}
}
System.out.println(i);
}
}
package com.hu.struct;
public class P43_1 {
// break用于强制退出循环,可用于switch
// continue只用于循环语句,可终止某次循环过程,跳过循环体尚未执行的程序,然后进行下一次执行循环的判定
public static void main(String[] args) {
int i = 0;
while (i < 100){
i++;
if (i % 10 ==0){
System.out.println();
continue;
}
System.out.println(i);
}
}
}
package com.hu.struct;
public class P43_2 {
public static void main(String[] args) {
// 打印101 - 150之间所有的质数
outer:for (int i = 101; i <= 150; i++){
for (int j = 2; j < i/2; j++){
if (i % j == 0){
// 标签continue
continue outer;
}
}
System.out.print(i + " ");
}
}
}
2P44 练习题
package com.hu.struct;
public class P44 {
public static void main(String[] args) {
// 打印三角形 5行
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i ; j++) {
System.out.print("*");
}
for (int j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
3P45 什么是方法
类+对象+方法
方法是语句的集合
- 方法是解决一类问题步骤的有序组合
- 方法包含于类或对象中
- 方法在程序中被创建,在其他地方被引用
保持方法原子性,一个方法完成一个功能
修饰符+返回值类型+方法名+参数类型+方法体
package com.hu.method;
public class P45 {
public static void main(String[] args) {
int sum = add(1, 2);
System.out.println(sum);
}
// 加法
public static int add(int a,int b){
return a + b;
}
}
3P46 方法的定义和调用
package com.hu.method;
/*
方法返回值时,方法调用通常被当作一个值
方法返回空时,方法调用为一条语句
*/
public class P45_1 {
public static void main(String[] args) {
int max = max(10, 20);
System.out.println(max);
}
public static int max(int a, int b){
int result = 0;
if (a == b){
System.out.println("a == b");
// 终止方法
return 0;
}
if (a > b){
result = a;
}else {
result = b;
}
return result;
}
}
Java都是值传递
3P47 方法的重载
需要看一下菜鸟教程
方法名称必须相同,参数列表必须不同,返回值类型可相同可不同
jvm会根据调用参数的不同匹配调用不同的方法
package com.hu.method;
public class P47 {
public static void main(String[] args) {
double max = max(10.2, 20.2);
System.out.println(max);
}
public static int max(int a, int b){
int result = 0;
if (a == b){
System.out.println("a == b");
// 终止方法
return 0;
}
if (a > b){
result = a;
}else {
result = b;
}
return result;
}
public static double max(double a, double b){
double result = 0;
if (a == b){
System.out.println("a == b");
// 终止方法
return 0;
}
if (a > b){
result = a;
}else {
result = b;
}
return result;
}
}