一.随堂复习
(一)(了解)流程控制结构
1.顺序结构
2.分支结构
- if-else
- switch-case
3.循环结构
- for
- while
- do-while
(二)分支结构一:if-else
在程序中,凡是遇到了需要使用分支结构的地方,都可以考虑使用if-else。
基本语法
/**
* ClassName:IfElseTest
* Package:PACKAGE_NAME
* Description:if-else条件判断结构
* 格式1:if(条件表达式){
* 语句块;
* }
* 格式2:if(条件表达式){
* 语句块1;
* }else{
* 语句块2;
* }
* 格式3:if(条件表达式1){
* * 语句块1;
* * }else if(条件表达式2){
* * 语句块2;
* * }
* ...
* }else if(条件表达式n){
* 语句块n;
* }else{
* 语句块n+1;
* }
*
*/
public class IfElseTest {
public static void main(String[] args){
/*案例1: 成年人心率的正常范围是每分钟60-100次。体检时,如果心率不在此范围内,则提示需要做进一步的检查。*/
int heartBeats = 89;
if(heartBeats < 60 || heartBeats > 100){
System.out.println("需要进一步检查");
}
System.out.println("体检结束");
//***********************************************
int num = 13;
if(num % 2 ==0){
System.out.println(num + "是偶数");
}else{
System.out.println(num + "是奇数");
}
}
}
案例
import java.util.Scanner;
/**
* ClassName:IfElseTest2
* Package:PACKAGE_NAME
* Description:
* 案例:
* 由键盘输入三个整数分别存入变量num1、num2、num3,对它们进行排序(使用 if-else if-else),并且从小到大输出。
*/
public class IfElseTest2 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入三个数");
int num1 = scan.nextInt();
int num2 = scan.nextInt();
int num3 = scan.nextInt();
if(num1 >= num2) {
if (num3 >= num1) {
System.out.println(num2 + "," + num1 + "," + num3);
} else if (num3 <= num2){
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);
}
}
scan.close();
}
}
(三)分支结构二:switch-case
1.在特殊的场景下,分支结构可以考虑使用switch-case
- 指定的数据类型: byte short char int;枚举类 (dk5.0)( String (jdk7.0)
- 可以考虑的常量值有限且取值情况不多
2.特别之处: case穿透
3.在能使用switch-case的情况下,推荐使用switch-case,因为比if-else效率稍高
基本语法
/**
* ClassName:SwitchCaseTest
* Package:PACKAGE_NAME
* Description:
* switch(表达式){
* case 常量1:
* //执行语句1
* break;
* case 常量2:
* //执行语句2
* break;
* ...
* default:
* //执行语句n
* //break;
* }
*
* 2.执行过程:根据表达式中的值,依次匹配case语句。一旦与某一个case中的常量相等,那么就执行此case中的执行语句。执行完此执行语句之后,
* 情况1: 遇到break,则执行break后,跳出当前的switch-case结构
* 情况2:没有遇到break,则继续执行其后的case中的执行语句。
* ...
* 直到遇到break或者执行完所有的case及default中的语句,退出当前的switch-case结构
* 3.说明:
* ① switch中的表达式只能是特定的数据类型。如下: byte / short / char / int 枚举(JDK5.0新增) / String(JDK7,0新增)
* ② case 后都是跟的常量,使用表达式与这些常量做相等的判断,不能进行范围的判断。
* ③ 开发中,使用switch-case时,通常case匹配的情况都有限。
* ④ break:可以使用在switch-case中。一且执行此break关键字,就跳出当前的switch-case结构
* ⑤ default:类似于if-else中的else结构。
* default是可选的,而且位置是灵活的。
*
*/
public class SwitchCaseTest {
public static void main(String[] args){
int num =1;
switch (num){
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");
break;
}
//*******************************
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("季节输入有误");
break;
}
}
}
案例
/**
* ClassName:SwitchCaseTest1
* Package:PACKAGE_NAME
* Description:案例3: 使用switch-case实现: 对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。
*
*/
public class SwitchCaseTest1 {
public static void main(String[] args){
int score = 88;
//方法1
//case穿透
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;
default: //110 时执行
System.out.println("成绩输入有误");
break;
}
//方法2
switch (score / 60){
case 0:
System.out.println("不及格");
break;
case 1: //110 时执行
System.out.println("及格");
break;
default:
System.out.println("成绩输入有误");
break;
}
}
}
import java.util.Scanner;
/**
* ClassName:SwitchCaseTest2
* Package:PACKAGE_NAME
* Description:案例:编写程序:从键盘上输入2023年的“month”和“day”,要求通过程序输出输入的日期为2023年的第几天。
*
*/
public class SwitchCaseTest2 {
public static void main (String[] args){
//1.使用Scanner,从键盘获取2023年的month、day
Scanner input = new Scanner(System.in);
System.out.println("请输入2023年的月份:");
int month = input.nextInt();
System.out.println("请输入2023年的天:");
int day = input.nextInt();
//假设用户输入数据合法。后期开发中使用正则表达式校验。
//2.使用switch-case实现分支结构
int sumDays = 0;
/*方法1,不推荐.存在数据冗余
switch (month){
case 1:
sumDays = day;
break;
case 2:
sumDays = 31 + day;
break;
case 3:
sumDays = 31 + 28 + day;
break;
case 4:
sumDays = 31 + 28 + 31 + day;
break;
//...
case 12:
sumDays = 31 + 28 + ... + 30 + day;
break;
}
*/
//方法2,穿透性
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:
sumDays += 28;//28是2月份的总天数
case 2:
sumDays += 31;//31是1月份的总天数
case 1:
sumDays += day;
break;
}
System.out.println("2023年" + month + "月" + "day" + "日是当年的第" + sumDays + "天");
input.close();//为了防止内存泄漏
}
}
import java.util.Scanner;
/**
* ClassName:SwitchCaseTest3
* Package:PACKAGE_NAME
* Description:编写程序:从键盘上读入一个学生成绩,存放在变量score中,根据score的值输出其对应的成绩等级:
* score>=90 等级:A
* 70<=score<99 等级:B
* 60<=score<70 等级:C
* score<60 等级:D
*
*/
public class SwitchCaseTest3 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入学生成绩:");
int score = input.nextInt();
char grade;
switch (score / 10){
case 10:
case 9:
grade = 'A';
break;
case 8:
case 7:
grade = 'B';
break;
case 6:
grade = 'C';
break;
default:
grade = 'D';
}
System.out.println("学生成绩为" + score + ",对应的等级为" + grade);
input.close();
}
}
(四)循环结构一:for
1.凡是循环结构,都有4个要素: ①初始化条件② 循环条件(Boolean类型)③ 循环体④迭代条件
2.应用场景:有明确遍历次数。for(int i = 1; i <= 100;i++)
基本语法
/**
* ClassName:ForTest
* Package:PACKAGE_NAME
* Description:for循环
* 1.Java中规范了3种循环结构:for、while、do-while
* 2.四要素:①初始化条件、②循环条件、③循环体、④迭代部分
* 3.for循环格式:
* for(①;②;④){
* ③
* }
* 执行过程:①②③④②③④---②
*
*/
public class ForTest {
public static void main(String[] args){
//需求1:输出5行helloworld
for(int i = 1; i <= 5 ;i++){
System.out.println("helloword");
}
//需求2:遍历1-100以内的偶数,并获取偶数的个数,获取所有的偶数的和
int count = 0;
int sum = 0;
for(int i = 1;i <= 100;i++){
if(i % 2 == 0){
System.out.println(i);
count++;
sum += i;
}
}
System.out.println("偶数的个数为:" + count);
System.out.println("偶数的总和为:" + sum);
}
}
案例
/**
* ClassName:ForTest1
* Package:PACKAGE_NAME
* Description:题目:输出所有的水仙花数,所谓水仙花数是指一个3位数,其各个位上数字立方和等于其本身。
* 153 = 1*1*1 + 3*3*3 + 5*5*5
*
*
*/
public class ForTest1 {
public static void main(String[] args){
for(int i = 100;i <= 999;i++){
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if(i == ge * ge * ge + shi * shi * shi + bai * bai * bai){
System.out.println(i);
}
}
}
}
/**
* ClassName:ForTest2
* Package:PACKAGE_NAME
* Description:说明:输入两个正整数m和n,求其最大公约数和最小公倍数。
* 比如: 12和20的最大公约数是4,最小公倍数是60.
* 约数:12为例,约数有1,2,3,4,6,
* 20为例,约数有1,2,4,5,10,20
* 倍数:12为例,倍数有12,24,36,48,6[,72,....
* 20为例,倍数有20,40,60,80,....
*
*/
public class ForTest2 {
public static void main(String[] args){
int m = 12;
int n = 20;
int min = (m < n) ? m : n;
//需求1:最大公约数
//方法1
int result = 1;
for(int i = 1;i <= min;i++){
if(m % i == 0 && n % i == 0){
result = i;
}
}
System.out.println(result);
//方法2:推荐
for(int i = min;i >= 1;i--){
if(m % i ==0 && n % i ==0){
System.out.println("最大公约数为:" + i);
break;
}
}
//需求2:最小公倍数
int max = (m > n) ? m : n;
for(int i = max;i <= m*n;i++){
if(i % m ==0 && i % n ==0){
System.out.println("最小公倍数为:" + i);
break;
}
}
}
}
(五)循环结构二:while
1.应用场景:没有明确遍历次数。
基本语法
/**
* ClassName:WhileTest
* Package:PACKAGE_NAME
* Description:while循环
* 1.四要素:①初始化条件、②循环条件、③循环体、④迭代部分
* 2.while的格式
* ①
* while(②){
* ③
* ④
* }
* 3.执行过程:①②③④②③④---②
* 4.for和while可以相互转换
* 5.for循环和while循环的小区别: 初始化条件的作用域范围不同。while循环中的初始化条件在while循环结束后,依然有效。
*
*/
public class WhileTest {
public static void main(String[] args){
//遍历10次helloworld
int i = 1;
while(i <= 10){
System.out.println("helloword");
i++;
}
//遍历1-100以内的偶数,并获取偶数的个数,获取所有的偶数的和
int j = 1;
int count = 0;
int sum = 0;
while(j <= 100){
if(j % 2 == 0){
System.out.println(j);
count++;
sum += j;
}
j++;
}
System.out.println("偶数的个数为:" + count);
System.out.println("偶数的总和为:" + sum);
}
}
案例
import java.util.Scanner;
/**
* ClassName:WhileTest1
* Package:PACKAGE_NAME
* Description:随机生成一个100以内的数,猜这个随机数是多少?
* 从键盘输入数,如果大了,提示大了;如果小了,提示小了:如果对了,就不再猜了,并统计一共猜了多少次。
* 提示: 生成一个[a,b] 范围的随机数的方式: (int)(Math.random() * (b - a + 1) + a)
*
*/
public class WhileTest1 {
public static void main(String[] args){
//1.生成一个[1,100]范围的随机整数
int random = (int)(Math.random() * 100) + 1;
//2.使用scanner,从键盘获取数据
Scanner scan = new Scanner(System.in);
System.out.println("请输入1-100范围的一个整数:");
int guess = scan.nextInt();
//3.声明一个变量,记录猜的次数
int guessCount = 1;
//4.使用循环结构,进行多次循环的对比和获取数据
while(random != guess){
if(guess > random){
System.out.println("你输入的数据大了!");
} else if (guess < random) {
System.out.println("你输入的数据小了!");
}
System.out.println("请输入1-100范围的一个数:");
guess = scan.nextInt();
guessCount++;
}
//
System.out.println("恭喜你,猜对了!");
System.out.println("共猜了" + guessCount + "次");
}
}
/**
* ClassName:WhileTest2
* Package:PACKAGE_NAME
* Description:世界最高山峰是珠穆朗玛峰,它的高度是8848.86米,假如我有一张足够大的纸,它的厚度是0.1毫米。
* 请问,我折叠多少次,可以折成珠穆朗玛峰的高度?
*
*/
public class WhileTest2 {
public static void main(String[] args){
//1.声明珠峰的高度、纸的默认厚度
double paper = 0.1;//单位:毫米
double zf = 8848860;//单位:毫米
//2.定义一个变量,记录折纸次数
int count = 0;
//3.通过循环结构,不断调整纸的厚度 (当纸的厚度超过珠峰高度时,停止循环)
while (paper <= zf){
paper *= 2;
count++;
}
System.out.println("paper的高度为:" + paper / 1000 + ",超过了珠峰高度" + zf / 1000);
System.out.println("共折纸" + count + "次");
}
}
(六)循环结构三:do-while
1.至少执行一次循环体
2.开发中,使用较少
基本语法
/**
* ClassName:DoWhileTest
* Package:PACKAGE_NAME
* Description:do-while循环
* 1.四要素:①初始化条件、②循环条件、③循环体、④迭代部分
* 2.do-while的格式
* ①
* do{
* ③
* ④
* }while(②);
* 3.执行过程:①③④②③④②---②
* 4.do-while循环至少执行一次循环体。
* for、while、do-while循环三者之间是可以相互转换的。
* do-while循环在开发中使用较少
*
*/
public class DoWhileTest {
public static void main(String[] args){
//需求:遍历100以内的偶数,并输出偶数的总和
int i = 1;
int count = 0;
int sum = 0;
do{
if(i % 2 == 0){
System.out.println(i);
count++;
sum += i;
}
i++;
}while(i <= 100);
System.out.println("偶数的个数:" + count);
System.out.println("偶数的总和:" + sum);
//*****************************
int num1 = 10;
while(num1 > 10){
System.out.println("while:hello");
num1--;
}
int num2 = 10;
do{
System.out.println("do-while:hello");
num2--;
}while(num2 > 10);
}
}
案例
import java.util.Scanner;
/**
* ClassName:DowhileTest1
* Package:PACKAGE_NAME
* Description: 模拟ATM取款
* 声明变量balance并初始化为0,用以表示银行账户的余额,下面通过ATM机程序实现存款,取款等功能。
* ======ATM======
* 1.存款
* 2.取款
* 3.显示余额
* 4.退出
* 请选择(1-4):
*
*
*/
public class DowhileTest1 {
public static void main(String[] args){
//1.定义balance的变量,记录账户余额
double balance = 0.0;
boolean flag = true;//控制循环的结束
Scanner scan = new Scanner(System.in);//实例化Scanner
do{
//2.声明ATM取款的界面
System.out.println("======ATM======");
System.out.println("1.存款");
System.out.println("2.取款");
System.out.println("3.显示余额");
System.out.println("4.退出");
System.out.println("请选择(1-4):");
//3.使用Scanner获取用户选择
int selection = scan.nextInt();
switch (selection){
//4.根据用户的选择,坚决执行存款、取款、显示余额、退出的操作
case 1:
System.out.println("请输入存款金额:");
double money1 = scan.nextDouble();
if(money1 > 0){
balance += money1;
}
break;
case 2:
System.out.println("请输入取款金额:");
double money2 = scan.nextDouble();
if (money2 > 0 && money2 <= balance){
balance -= money2;
}else{
System.out.println("输入数据有误或余额不足");
}
break;
case 3:
System.out.println("账户余额为:" + balance);
break;
case 4:
flag = false;
System.out.println("感谢使用,欢迎下次光临!");
break;
default:
System.out.println("输入有误,请重新输入");
}
}while(flag);
scan.close();
}
}
(七)“无限”循环
基本语法
/**
* ClassName:ForWhileTest
* Package:PACKAGE_NAME
* Description:"无限"循环结构的使用
* 1.格式: while(true) 或 for(;;)
* 2.开发中,有时并不确定需要循环多少次,需要根据循环体内部某些条件,来控制循环的结束(使用break)。
* 3.如果此循环结构不能终止,则构成了死循环!开发中要避免出现死循环。
*
*
*/
public class ForWhileTest {
public static void main(String[] args){
for (;;){//while(true){
System.out.println("i love you!");
}
//死循环的后面不能有执行语句。
//System.out.println( "end" ) ;
}
}
案例
import java.util.Scanner;
/**
* ClassName:ForWhileTest1
* Package:PACKAGE_NAME
* Description:
*
*/
public class ForWhileTest1 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int positiveCount = 0;//记录正数个数
int negativeCount = 0;//记录负数个数
for (;;){//while(true){
System.out.println("请输入一个整数(输入为0时程序结束):");
int num = scan.nextInt();
if (num > 0){
positiveCount++;
}else if(num < 0){
negativeCount++;
}else{
System.out.println("程序结束");
break;
}
}
System.out.println("正数个数为:" + positiveCount);
System.out.println("负数个数为:" + negativeCount);
scan.close();
}
}
(八)嵌套循环
基本语法
/**
* ClassName:ForForTest
* Package:PACKAGE_NAME
* Description:嵌套循环
*
*/
public class ForForTest {
public static void main(String[] args){
/*
******
******
******
******
******
*/
for(int i = 1;i <= 5;i++){
for(int j = 1; j<= 6;j++){
System.out.print("*");
}
System.out.println();
}
/***************
*
**
***
****
*****
*/
for (int i = 1;i <= 5; i++){
for(int j = 1;j <= i; j++){
System.out.print("*");
}
System.out.println();
}
/*
******
*****
****
***
**
*
*/
for (int i = 1;i <= 6; i++){
for(int j = 1;j <= 7-i ; j++){
System.out.print("*");
}
System.out.println();
}
/*******************
i(第几行) j(每一行中-的个数) k(每一行中*的个数) i+j=5--->j=5-i
----* 1 4 1 k=2i-1
---*** 2 3 3
--***** 3 2 5
-******* 4 1 7
********* 5 0 9
******* 1 1 7 i=j
***** 2 2 5 2i+k=9--->k=9-2i
*** 3 3 3
* 4 4 1
*/
//上半部分
for (int i = 1; i <= 5; i++){
//-
for (int j = 1;j <= 5 - i;j++){
System.out.print("-");
}
//*
for (int k = 1;k <= 2 * i - 1;k++){
System.out.print("*");
}
System.out.println();
}
//下半部分
for (int i = 1;i <= 4;i++){
//-
for (int j = 1;j <= i;j++){
System.out.print("-");
}
//*
for (int k = 1;k <= 9 - 2 * i;k++){
System.out.print("*");
}
System.out.println();
}
}
}
案例
/**
* ClassName:NineNineTable
* Package:PACKAGE_NAME
* Description:九九乘法表
*
*/
public class NineNineTable {
public static void main(String[] args){
for (int i = 1;i <= 9;i++){
for (int j = 1;j <= i;j++){
System.out.print(i + "*" + j + "=" + i * j + "\t");
}
System.out.println();
}
}
}
(九)关键字break、continue
1.break在开发中常用;而continue较少使用
2.笔试题: break和continue的区别。
基本语法
/**
* ClassName:BreakContinueTest
* Package:PACKAGE_NAME
* Description:1.break和continue关键字的使用
* 使用范围 在循环结构中的作用 相同点
* break switch-case和循环结构中 在结束(或跳出)当前循环结构 在此关键字后面不能声明语句
* continue 循环结构中 在结束(或跳出)当次循环 在此关键字后面不能声明语句
*
* 2.了解带标签的break和continue的使用
* 3.开发中,break的使用频率要远高于continue
*/
public class BreakContinueTest {
public static void main(String[] args){
for(int i = 1;i <= 10;i++){
if (i % 4 == 0){
//break;
continue;
//编译不通过
//System.out.println("***********");
}
System.out.print(i);
}
System.out.println();
//*********************************
for (int j = 1;j <= 4; j++){
for (int i = 1;i <= 10; i++){
if(i % 4 == 0){
//break;
continue;
}
System.out.print(i);
}
System.out.println();
}
}
}
(十)scanner类的使用
基本语法
import java.util.Scanner;
/**
* ClassName:ScannnerTest
* Package:PACKAGE_NAME
* Description:如何从键盘获取不同类型(基本数据类型、String类型) 的变量: 使用Scanner类。
* 使用Scanner获取不同类型数据的步骤
* 步骤1: 导包 import java.util.Scanner
* 步骤2: 提供 (或创建) 一个Scanner类的实例
* 步骤3: 调用Scanner类中的方法,获取指定类型的变量
* 步骤4: 关闭资源,调用Scanner类的close()
*
* Scanner类中提供了获取byte \ short int long float double boolean String类型变量的方法
* 注意,没有提供获取char类型变量的方法。
* 需要使用next().charAt(0)
*/
public class ScannnerTest {
public static void main(String[] args){
//案例:小明注册某交友网站,要求录入个人相关信息。如下:
//请输入你的网名、你的年断你的体重、你是否单身、你的性别等情况。
//步骤2: 提供 (或创建) 一个Scanner类的实例
Scanner scanner = new Scanner(System.in);
System.out.println("请输入网名:");
//步骤3: 调用Scanner类中的方法,获取指定类型的变量
String name = scanner.next();
System.out.println("请输入你的年龄:");
int age = scanner.nextInt();
System.out.println("请输入你的体重:");
double weight = scanner.nextDouble();
System.out.println("你是否单身(单身:true;不单身:false):");
boolean isSingle = scanner.nextBoolean();
System.out.println("请输入你的性别:(男/女)");
char gender = scanner.next().charAt(0);
System.out.println("网名:" + name + ",年龄:" + age + ",体重:" + weight + ",是否单身:" + isSingle + ",性别:" + gender);
System.out.println("注册完成");
//步骤4: 关闭资源,调用Scanner类的close()
scanner.close();//为了防止内存泄漏
}
}
案例
import java.util.Scanner;
/**
* ClassName:ScannerExer
* Package:PACKAGE_NAME
* Description:大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出一定的条件:
* 高:180cm以上; 富:财富1千万以上; 帅:是。 如果这三个条件同时满足,则:“我一定要嫁给他!!!”如果三个条件有为真的情况,则:“嫁吧,比上不足,比下有余。如果三个条件都不满足,则:“不嫁!”
*
*/
public class ScannerExer {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入身高:(cm)");
int height = scanner.nextInt();
System.out.println("请输入财富:(千万)");
double wealth = scanner.nextDouble();
System.out.println("帅否:(帅:true)");
boolean isHandsome = scanner.nextBoolean();
if(height >= 180 && wealth >= 1.0 && isHandsome == true){
System.out.println("我一定要嫁给他!!!");
}else if(height >= 180 || wealth >= 1.0 || isHandsome == true){
System.out.println("嫁吧,比上不足,比下有余");
}else{
System.out.println("不嫁");
}
scanner.close();//为了防止内存泄漏
}
}
(十一)获取随机数
基本语法
/**
* ClassName:RandomTest
* Package:PACKAGE_NAME
* Description:如何获取一个随机数?
* 1.可以使用Java提供的API:Math类的random()
* 2.random()调用以后,会返回一个[0.0,1.0)范围的double型的随机数
*
* 3.需求1: 获取一个[0,100]范围的随机整数?
* 需求2: 获取一个[1,100]范围的随机整数?
* 4.需求:获取一个[a,b]范围的随机整数?
* (int)(Math.random()*(b-a+1))+a
*/
public class RandomTest {
public static void main(String[] args){
double d1 = Math.random();
System.out.println("d1 = " + d1);
int num1 = (int) (Math.random() * 101);
System.out.println("num1 = " + num1);
int num2 = (int) (Math.random() * 100) + 1;
}
}
(十二)谷粒记账软件
/**
* ClassName:GuLiAccount
* Package:PACKAGE_NAME
* Description:谷粒记账软件
*
*/
public class GuLiAccount {
public static void main(String[] args){
boolean isFlag = true;//控制循环的结束
//初始金额
int balance = 10000;
String info = "";//记录收支信息
while (true){
System.out.println("---------------谷粒记账软件---------------");
System.out.println(" 1.收支明细");
System.out.println(" 2.登记收入");
System.out.println(" 3.登记支出");
System.out.println(" 4.退 出\n");
System.out.print(" 请选择(1-4):");
char selection = Utility.readMenuSelection();//获取用户的选项:'1' '2' '3' '4'
switch (selection){
case '1':
System.out.println("---------------当前收支明细记录---------------");
System.out.println("收支\t\t账户金额\t\t收支金额\t\t说 明");
System.out.println(info);
System.out.println("-------------------------------------------");
break;
case '2':
System.out.print("本次收入金额:");
int money1 = Utility.readNumber();
if (money1 > 0){
balance += money1;
}
System.out.print("本次收入说明:");
String addDesc = Utility.readString();
info += "收入\t\t" + balance + "\t\t" + money1 + "\t\t\t" + addDesc + "\n";
System.out.println("---------------登记完成---------------");
break;
case '3':
System.out.print("本次支出金额:");
int money2 = Utility.readNumber();
if (money2 > 0 && balance >= money2){
balance -= money2;
}
System.out.print("本次收入说明:");
String minusDesc = Utility.readString();
info += "支出\t\t" + balance + "\t\t" + money2 + "\t\t\t" + minusDesc + "\n";
System.out.println("---------------登记完成---------------");
break;
case '4':
System.out.print("\n确认是否退出(Y/N):");
char isExit = Utility.readConfirmSelection();
if (isExit == 'Y'){
isFlag = false;
}
break;
}
}
}
}
(十三)算法魅力
/**
* ClassName:PrimeNumberTest
* Package:PACKAGE_NAME
* Description:题目: 找出100以内所有的素数(质数)? 100000以内的呢?
* 质数:只能被1和它本身整除的自然数。比如: 2,3,5,7,11,13,17,19,23,....
* 换句话说,从2开始到这个自然数-1为止,不存在此自然数的约数.
*
*/
public class PrimeNumberTest {
public static void main(String[] args){
/*方法一
for(int i = 2;i <= 100;i++){//遍历100以内的自然数
int number = 0;//记录i的约数的个数(从2开始,至i-1)
//判定i是否是质数
for (int j = 2;j < i;j++){
if (i % j == 0){
number++;
}
}
if (number == 0){
System.out.println(i);
}
}
*/
//方法二
for(int i = 2;i <= 100;i++){//遍历100以内的自然数
boolean isFlag = true;
//判定i是否是质数
for (int j = 2;j < i;j++){
if (i % j == 0){
isFlag = false;
}
}
if (isFlag){ //if(isFlag == true){
System.out.println(i);
}
}
}
}
/**
* ClassName:PrimeNumberTest1
* Package:PACKAGE_NAME
* Description:遍历100000以内的所有质数。体会性能差别
* 此为方法1
*
*/
public class PrimeNumberTest1 {
public static void main(String[] args){
//获取当前系统时间
long start = System.currentTimeMillis();
boolean isFlag = true;
int count = 0;
for(int i = 2;i <= 100000;i++){
//判定i是否是质数
for (int j = 2;j < i;j++){
if (i % j == 0){
isFlag = false;
}
}
if (isFlag){
count++;
}
//重置isFlag
isFlag = true;
}
//获取系统当前时间:
long end = System.currentTimeMillis();
System.out.println("质数的总个数:" + count);//9592
System.out.println("花费的时间为:" + (end - start));//8456
}
}
/**
* ClassName:PrimeNumberTest2
* Package:PACKAGE_NAME
* Description:遍历1000000以内的所有质数。体会性能差别
* 此为方法2 优化
*
*/
public class PrimeNumberTest2 {
public static void main(String[] args){
//获取当前系统时间
long start = System.currentTimeMillis();
boolean isFlag = true;
int count = 0;
for(int i = 2;i <= 100000;i++){
//判定i是否是质数
for (int j = 2;j < Math.sqrt(i);j++){
if (i % j == 0){
isFlag = false;
break;//针对于非质数有效果
}
}
if (isFlag){
count++;
}
//重置isFlag
isFlag = true;
}
//获取系统当前时间:
long end = System.currentTimeMillis();
System.out.println("质数的总个数:" + count);//9592
System.out.println("花费的时间为:" + (end - start));//加上break:671----->加上Math.sqrt():10
}
}
二.企业真题
(一)break和continue的作用(智*图)
1.break和continue关键字的使用
使用范围 在循环结构中的作用 相同点
break switch-case和循环结构中 在结束(或跳出)当前循环结构 在此关键字后面不能声明语句
continue 循环结构中 在结束(或跳出)当次循环 在此关键字后面不能声明语句
2.了解带标签的break和continue的使用
3.开发中,break的使用频率要远高于continue
(二)if分支语句和switch分支语句的异同之处(智*图)
1.if-else语句优势
- if语句的条件是一个布尔类型值,if条件表达式为true则进入分支,可以用于范围的判断,也可以用于等值的判断,使用范围更广。
- switch语句的条件是一个常量值 (byte,short,int,char,枚举,String),只能判断某个变量或表达式的结果是否等于某个常量值,使用场景较狭窄。
2.switch语句优势*
- 当条件是判断某个变量或表达式是否等于某个固定的常量值时,使用i和switch都可以,习惯上使用switch更多。因为 效率稍高 。当条件是区间范围的判断时,只能使用i语句。
- 使用switch可以利用穿透性,同时执行多个分支,而if…else没有穿透性
(三)什么时候用语句if,什么时候选用语句switch(灵伴*来科技)
同上
(四)switch语句中忘写break会发生什么(北京*蓝)
case穿透
(五)Java支持哪些类型循环(上海*睿)
for;while;do-while
增强for(或foreach),
(六)while和do while循环的区别(国科技研究院)
do-while至少执行一次