语句
顺序
选择
if
ifelse
ifelseif
switch (☆☆☆):
格式:
switch (表达式或者变量){
case 常量值1:
语句1;
break;
case 常量值2:
语句2;
break;
case 常量值3:
语句3;
break;
case 常量值4:
语句4;
break;
...
default :
语句n;
break;
}
switch 和if的第三种格式比较:
1:switch只能判断单个值,不能判断范围, if 既可以判断单个值 也可以判断范围。
能用switch的地方 我们都可以使用if来代替, 但是能用if的地方 不一定可以使用switch
//用switch来做
int a = 10;
switch (a+11){
case 20:
System.out.println("a");
break;
case 21:
System.out.println("b");
break;
case 21: //编译报错
System.out.println("c");
break;
default :
System.out.println("不匹配");
break;
}
//用if来换 switch
int c = a+11; //21
if (c==20){
System.out.println("a");
}else if (c==21){
System.out.println("b");
}else if (c==21){ //不会报错。 可以重复
System.out.println("c"); // 不执行。
}else{
System.out.println("不匹配");
}
注意事项:
int c = a+11; //21
if (c==20){
System.out.println("a");
}else if (c==21){
System.out.println("b");
}if (c==21){ //不会报错。 上面是一个独立的if第三种格式整体 下面是一个if 第二种格式整体
System.out.println("c"); // 会执行。
}else{
System.out.println("不匹配");
}
案例:判断一个成绩 0-40分 差 40-60分 一般 60-80分 凑合 80-100分 差不多儿
//用if来做
int c = 65;
if (c>0 && c<=100){
if (c<=40){
System.out.println("差");
}else if ( c<=60){ //c>40&&c<=60 35
System.out.println("一般");
}else if (c<=80){
System.out.println("凑合");
}else{
System.out.println("差不多儿");
}
}else{
System.out.println("输入错误");
}
//用switch来做
//int c = 65;
//switch (c){
// case c>0 && c<=40: // 编译报错 case后面必须是常量, 表达式的值 是boolean类型的。
//}
/*
int c = 65;
boolean b = 0<c&& c<=40;
switch (b){ // 编译报错 () 不能对boolean类型进行判断。
case true:
}
*/
int c = 65;
switch (c){
case 0:
case 1:
....
case 100:
} //非常麻烦。
2:如果对单个值进行判断, 而且判断的情况比较多, switch的效率比if要高。
详细解释:https://www.bilibili.com/read/cv5912019
switch 的注意事项:
1:case后面 必须是常量 如果是变量就报错
int a = 10;
int b = 20;
switch (a){
case b: //编译报错 b是变量
System.out.println("abc");
break;
}
2:case 不能重复的判断。
int a = 10;
switch (a){
case 4:
System.out.println("ahaha");
break;
case 4: // 编译报错 重复判断了
System.out.println("hehee");
break;
}
int b = 10;
switch (b){
case 10:
System.out.println("ahaha");
break;
case 10 // 编译报错 重复判断了
System.out.println("hehee");
break;
}
3: switch () 小括号内 ,里面只能存放的数据类型 必须是这几种之一, 否则就编译报错
byte short char int (这些都是小于等于int的---基本类型)
枚举 String (引用类型)
boolean b = true;
switch (b){ //编译报错 数据类型错误。
case true:
System.out.println("aa");
break;
}
4: if的第三种格式里面 最后的else收尾是可以省略的。 同样的道理 switch的最后的default 也是可以省略的。
5: if的第三种格式的 最后的else收尾 必须放在最后面, 但是 switch的default 却是可以放在任意位置的。
int a = 10;
switch (a){
case 5:
System.out.println(5);
break;
default:
System.out.println("默认");
break;
case 10:
System.out.println("10");
break;
} //此处运行的是10
// default 的书写位置随意放,但是执行顺序 依然是所有case都不匹配之后 才去执行default,
int a = 10;
switch (a){
case 5:
System.out.println(5);
break;
default:
System.out.println("默认");
break;
case 6:
System.out.println("10");
break;
} // 默认
6:break 的作用是用来结束switch的, 当程序执行到break switch就结束了。
也就是说 break是可以省略的,但是会发生case穿透(不再看case了 直接执行case后的语句)
1:
int a =10;
int b = 0;
switch (a){
case 10:
b++;
case 20:
b++;
break;
default :
b++;
}
System.out.println(b); //2
2:
int a =10;
int b = 0;
switch (a){
case 10:
b++;
default :
b++;
break;
case 20:
b++;
}
System.out.println(b); //2
3:
int a =10;
int b = 0;
switch (a){
case 30:
b++;
default :
b++;
break;
case 20:
b++;
}
System.out.println(b); //1
4:
int a =10;
int b = 0;
switch (a){
case 30:
b++;
default :
b++;
case 20:
b++;
}
System.out.println(b); //2
循环
for :
循环结构:
class Demo {
public static void main(String[] args){
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
}
}
格式:
for (初始化语句 ; 条件表达式 ; 控制循环语句 ){
循环体;
}
真实格式:
for (语句 ; boolean数据 ; 语句 )语句;
public class Demo1 {
public static void main(String[] args) {
//for (语句 ; boolean数据 ; 语句 )语句;
// 语句1
// 语句2
//for (System.out.println("初始化语句") ; true ; System.out.println("控制循环语句") ) System.out.println("循环体");
// for 循环 的循环体 只能是 for的小括号之后的第一条语句。
// 但是有的时候 我需要让 for循环控制多条语句执行啊, 不想只控制一条啊。
// 那咋办呢??
// 你不是学了{} {} 就可以把多条语句 合成1条
/*
for (语句 ; boolean数据 ; 语句 ){
语句;
语句1;
语句2;
}
*/
/*
for (System.out.println("初始化语句") ; true ; System.out.println("控制循环语句") ) {
System.out.println("循环体");
} */
// 无限次数的执行循环体。
// 这不是我们想要的。 我们想让循环 执行有限次数 在我们的控制之内。
// 所以你要想 我们怎么执行有限的次数
// 执行一定的次数之后 循环就结束。 结束 就是循环遇到了false
// 所以 boolean类型的数据 必须是变化的 不能是常量 只能是变量或者表达式。
// 所以学习循环 其实核心就在于学习 如何控制循环的boolean类型 何时变成false
/*
int i = 0;
for (System.out.println("初始化语句") ; i<5 ; i++ ) {
System.out.println("循环体");
}
*/
/*
for (int i = 0 ; i<5 ; i++ ) {
System.out.println("循环体");
}
*/
boolean b = true;
for (System.out.println("初始化语句"); b ; b=false ){
System.out.println("循环体");
}
}
}
案例1: 打印1-5
for ( int i = 1; i<=5;i++ ){
System.out.println(i);
}
案例2:打印 5-1
for ( int i = 5; i>=1;i--){
System.out.println(i);
}
案例3:打印55-69
for ( int i = 55;i<=69 ;i++ ){
System.out.println(i);
}
案例4:打印 1 4 7 10 13 16 ..... 不能超过100
方法一:效率高
for (int i = 1;i<=100 ; i+=3){
System.out.println(i);
}
方法二:效率低
for (int i = 1;i<=100 ; i++){
if (i%3==1){
System.out.println(i);
}
}
案例5:打印(10以内的奇数) 1 3 5 7 9
方法一:
for (int i =1; i<10 ; i+=2 ){
System.out.println(i);
}
//for (int i = 1;i<10 ; i+2 ){ //编译报错 i+2 不是语句
// System.out.println(i);
//}
//int i = 10;
//i++;
//i+=2;
//i-3;
方法二:
for (int i =1; i<10 ; i++ ){
if (i%2==1){
System.out.println(i); //1 3 5 7 9 // 2 4 6 8
}
}
// 如何判断一个数 是奇数还是偶数
/*
int a = -31; //奇数
//boolean b = (a%2==1 || a%2==-1); // 可以了。
boolean b = a%2!=0; // 可以了。 //-10 10 偶数
if (b){
System.out.println("奇数");
}*/
/*
方法三:
//true && false
for (int i =1; i<10 && i%2==1; i++ ){
System.out.println(i); //1
}
// 循环的条件表达式 是用来终止循环的
// if 的条件表达式 是为了 过滤的 是选择性的执行与不执行的
// 他俩的作用不一样,不能混为一谈哈。
*/
案例6:打印满足 (个位数 和十位数 相同)的 两位数 11 22 33 44 55 ....
方法一:
for (int i = 11; i<=99 ;i+=11 ){
System.out.println(i);
}
方法二:
for (int i = 11; i<=99 ;i++ ){
if (i%11==0){
System.out.println(i);
}
}
第三种方法 ,就是直接把题目的汉语 翻译为java语言
//两位数
for (int i = 10; i<=99 ; i++ ){
//if (个位数 和十位数 相同){
//if (个位数 == 十位数){
int ge = i%10;
int shi = i/10%10;
if (ge == shi){
System.out.println(i);
}
}
案例7:打印所有的水仙花数。
1:水仙花数是三位数
2:每个位上的数的立方和 等于该数本身 比如: 371 = 3*3*3 + 7*7*7 +1*1*1
//三位数
for (int i = 100;i<=999 ;i++ ){
//if (每个位上的数的立方和 等于该数本身){
//if (个位的三次方 + 十位的三次方 + 百位的三次方 == i){
//if (个位*个位*个位 + 十位*十位*十位 + 百位*百位*百位 == i){
int ge = i%10;
int shi = i/10%10;
int bai = i/10/10%10;
if (ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
System.out.println(i);
}
}
案例8:打印所有的水仙花数 的和。 只打印最后的和
int sum = 0;
for (int i = 100;i<=999 ;i++ ){
//if (每个位上的数的立方和 等于该数本身){
//if (个位的三次方 + 十位的三次方 + 百位的三次方 == i){
//if (个位*个位*个位 + 十位*十位*十位 + 百位*百位*百位 == i){
int ge = i%10;
int shi = i/10%10;
int bai = i/10/10%10;
if (ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
sum +=i;
}
}
System.out.println(sum);
案例9:打印所有的水仙花数 的个数。 只打印个数
int count =0;
for (int i = 100;i<=999 ;i++ ){
//if (每个位上的数的立方和 等于该数本身){
//if (个位的三次方 + 十位的三次方 + 百位的三次方 == i){
//if (个位*个位*个位 + 十位*十位*十位 + 百位*百位*百位 == i){
int ge = i%10;
int shi = i/10%10;
int bai = i/10/10%10;
if (ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
count++;
}
}
System.out.println(count);
案例10:打印所有水仙花数 要求 每行打印2个(之前的题都是一个数占一行,现在是2个占一行)
int count =0;
for (int i = 100;i<=999 ;i++ ){
int ge = i%10;
int shi = i/10%10;
int bai = i/10/10%10;
if (ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
count++;
System.out.print(i+" ");
if (count%2==0){
System.out.println();
}
}
}
while :
格式:
初始化语句 ;
while ( 条件表达式 ){
循环体;
控制循环语句;
}
真正格式:
while (boolean类型的值)语句;
public class Demo8 {
public static void main(String[] args) {
//while (boolean类型的值)语句;
/*
while (true)
System.out.println("哈哈");
System.out.println("哈哈1"); //编译报错 执行不到的语句
*/
/*
while (true){
System.out.println("哈哈");
System.out.println("哈哈------------1");
}
*/
//; // 这条语句叫做空语句
/*
int i = 1;
while (i<520); //表达式
{ //编译不报错。
System.out.println("我爱你");
i++;
}
*/
/*
while (true) // true是常量
System.out.println("哈哈");
{ //编译错误: 无法访问的语句
System.out.println("哈哈1");
}
*/
int i = 50;
while (i<55){
System.out.println("哈哈");
System.out.println("哈哈------------1");
i++;
}
}
}
案例1:
for (int i = 1;i<=5 ;i++ ){
System.out.println("helloworld");
}
int j = 1 ;
while ( j<=5 ){
System.out.println("helloworld");
i++;
}
for循环和while循环的区别:
1:for循环和while循环 在任意的情况下 都是可以无条件互换的, 能用while的地方是绝对可以使用for的 能用for的地方 是绝对可以使用while的。
2:用途习惯不一样, 一般有明确范围的地方 我们都用for , 没有明确范围的地方 我们都用while
案例1: 珠穆朗玛峰的高度是 8848米 一张纸的厚度 0.00002米 请问假设纸足够大,请问 这张纸 对折多少次 可以达到珠穆朗峰的高度。
int high = 8848;
double hou = 0.00002;
int count=0;
while (hou < high){
hou *=2;
count++;
}
System.out.println(count);
dowhile
格式:
//do 做 执行
//while 当...的时候
初始化语句 ;
do{
循环体;
控制循环语句;
}while (条件表达式 );
案例1:打印3次HelloWorld
for (int i = 1;i<=3 ;i++ ){
System.out.println("helloworld");
}
int j =1;
while (j<=3 ){
System.out.println("helloworld");
j++;
}
int k = 1;
do{
System.out.println("helloworld");
k++;
}while ( k <= 3);
// 综上所述
// dowhile 第一次执行 不判断,直接执行, 后续在想执行才判断。
案例2: 打印0次HelloWorld
for (int i = 1;i<=0 ;i++ ){
System.out.println("helloworld");
}
int j =1;
while (j<=0 ){
System.out.println("helloworld");
j++;
}
int k = 1;
do{
System.out.println("helloworld");
k++;
}while ( k <= 0); // dowhile 根本就无法打印0次HelloWorld的。
案例3: 键盘录入一个 1-100的数
判断如果这个数 不再这个范围内 就重新录入
不管你录入多少次, 直到你录入正确为止。
import java.util.Scanner;
public class Demo12java {
public static void main(String[] args) {
/*
案例3: 键盘录入一个 1-100的数
判断如果这个数 不再这个范围内 就重新录入
不管你录入多少次, 直到你录入正确为止。
*/
Scanner sc = new Scanner(System.in);
//int a = sc.nextInt();
/*
if (a<0 || a>100){
a = sc.nextInt();
if (a<0 || a>100){
a = sc.nextInt();
if (a<0 || a>100){
a = sc.nextInt();
//..... 无穷无尽了。。。。。
}
}
}
*/
/*
int a = sc.nextInt();
while (a<0 || a>100){
a = sc.nextInt();
}
*/
// 但是--- 我想问问你 你用循环是在解决什么问题 重复问题啊。
// 此时 你的代码还有重复 nextInt 写了两次 重复了。
/*
int a ;
do{
a= sc.nextInt();
}while (a<0 || a>100);
System.out.println(a);
*/
int a ;
while (true){
a = sc.nextInt();
if (a>=1 && a<=100){
break;
}
}
}
}
break continue :
continue : 只能写在循环中。
跳过本次循环,后续的循环继续。
continue 的错误用法:
for (int i = 1; i<=10; i++){
System.out.println(i); // 都打印
if (i ==4){
continue;
}
}
continue 的正确用法:
for (int i = 1; i<=10; i++){
if (i ==4){
continue;
}
System.out.println(i); //4不打印
}
break: 只能写在循环和switch中
终止循环。
for (int i=18; i<=100;i++ ){
System.out.println(i +"工作"); //60岁干完 后面才不干的。
if (i==60){
break;
}
}
// ----------------
for (int i=18; i<=100;i++ ){
if (i==60){
break;
}
System.out.println(i +"工作"); //59岁干完 后面就不干了。。
}
带标号的中断:
a:for (int i = 18; i<=100 ; i++){
switch (i){
case 60:
break a;
}
System.out.println(i+"工作");
}
生成随机数:
import java.util.Random; //第一步
public class Demo {
public static void main(String[] args){
//第二步 创建 Random对象
Random r = new Random();
// 第三步 生成随机数
int a = r.nextInt(100); //[0-99]
int b = r.nextInt(101); //[0-100]
int c = r.nextInt(100) + 1; //[0-99]+ = [1-100]
}
}