网站怎么连接网,新媒体营销,如何推销企业建设网站,长沙品牌网站建设6、If、While、For、Switch
一、If
1、if-else
if (boolean) {代码块
} else if (boolean) {代码块
} else if (boolean) {代码块
} else { // 默认情况代码块
}关于IDEA单元测试控制台不能输入数据的问题#xff1a;
https://blog.csdn.net/m0_72900498/article/details/…6、If、While、For、Switch
一、If
1、if-else
if (boolean) {代码块
} else if (boolean) {代码块
} else if (boolean) {代码块
} else { // 默认情况代码块
}关于IDEA单元测试控制台不能输入数据的问题
https://blog.csdn.net/m0_72900498/article/details/143663689?spm1001.2014.3001.5501
-Deditable.java.test.consoletrue
代码示例
package com.situ.day8;import org.junit.Test;import java.util.Scanner;public class Demo01 {Testpublic void test1() {if (3 5) {System.out.println(3 5);} else {System.out.println(3 5);}}/*90 100 优秀80 90 良好70 80 一般60 70 及格60 不及格*/Testpublic void test2(){//你想表达什么意思就起成什么名字//int score 9;//通过Scanner可以实现从控制台输入信息//对于0和100的数据打印“这是非法输入”Scanner scanner new Scanner(System.in);System.out.println(请输入成绩:);int score scanner.nextInt();//卫语句1if (score 0 || score 100) {System.out.println(这是非法输入);//方法后面的代码不再执行return;}//卫语句2...//合法输入if (score 90 score 100) {System.out.println(优秀);} else if (score 80 score 90) {System.out.println(良好);} else if (score 70 score 80) {System.out.println(一般);} else if (score 60 score 70) {System.out.println(及格);} else {System.out.println(不及格);}}}2、三目运算符
语法boolean ? 数1 数2 执行过程计算boolean的值 若为true则整个结果为数1 若为false则整个结果为数2
Test
public void test55() {int num1 13;int num2 5;int max 0;if (num1 num2) {max num1;} else {max num2;}System.out.println(max);//等价于int max1 num1 num2 ? num1 : num2;System.out.println(max1);
}3、号运算符
1、两边都是数字则做的就是加法运算
2、若一边为字符串则做的是字符串的拼接
Test
public void test7() {int age 28;//豆豆加加System.out.println(我的年龄是23岁);System.out.println(我的年龄是 age 岁);System.out.println(10 20 30);//3030System.out.println( 10 20 30);//102030
}二、While、For
1、while、for
任何复杂的程序逻辑都可以通过“顺序”、“分支”、“循环”这三种基本结构来实现。 循环的三要素
1.循环条件的初始化
2.循环条件的控制
3.循环条件的改变 if (boolean) {
}//if不是循环
while (boolean) {//可以反复执行
}示例
使用While和for循环分别打印五次HelloWorld:
Test
public void test1() {int i 1;while (i 5) {System.out.println(HelloWorld);i;}
}Test
public void test2() {for (int i 1; i 5; i) {System.out.println(HelloWorld);}
}while、for循环主要是两类题目
1、累加思想123… 100
//sum i; sum sum i
//sum - i; sum sum - i
//sum * i; sum sum * i
//sum / i; sum sum / i
Test
public void test3() {int sum 0;for (int i 1; i 100; i) {System.out.println(i);sum i;System.out.println(sum: sum);}System.out.println(sum: sum);
}2、统计思想数一下符合条件的有多少个计算1-100以内7的倍数的个数
Test
public void test4() {int count 0;for (int i 1; i 100; i) {if (i % 7 0) {System.out.println(i);count;}}System.out.println(count: count);
}2、continue、break
continue跳出本次循环继续下一次循环
break跳出离他最近的那层循环
Test
public void test44() {for (int i 1; i 5; i) {if (i 3) {continue;}System.out.println(i);//输出 1 2 4 5}
}Test
public void test45() {for (int i 1; i 5; i) {if (i 3) {break;}System.out.println(i);//输出1 2}
}Test
public void test46() {//i,j,kfor (int i 1; i 5; i) {System.out.println(i: i);for (int j 1; j 5; j) {if (j 3) {break;//break只能退出离他最近的一层循环也就是退出for (int j 1; j 5; j)这层循环}System.out.println(j: j);}}
}例题随机生成一个整数1-1000
用户输入一个整数程序给出与存储的数字是“大”或者“小”知道用户猜到这个数字位置。 如果中途用户希望程序退出输入0可以退出。 int num 200; 猜吧 300 太大了 猜吧 180 太小了 猜吧 200 恭喜你猜对了
//随机生成一个整数1-1000
Test
public void test7() {//params parameter:参数Random random new Random();//the upper bound (exclusive). Must be positive.//exclude排除、不包含//include://value between zero (inclusive) and bound (exclusive)// [0,100) 1// 1-100int num random.nextInt(100) 1;
}关于快捷键获取方法的返回值
random.nextInt(100).var然后回车3、双重for循环 双重for循环
1.外层循环控制行数数一下有几行就能确定外层循环。
2.内层循环控制列数这一行打印多少个到底要打印多少个要找出和当前行之间的一个关系。
Testpublic void test55() {//****for (int i 1; i 4; i) {System.out.print(*);}System.out.println();//*//*//*//*for (int i 1; i 4; i) {System.out.println(*);}}Testpublic void test6() {// i1代表打印第一行for (int i 1; i 3; i) {//i1: 代表打印第一行//i2: 代表打印第二行//i3: 代表打印第三行for (int j 1; j 4; j) {//j代表这一行打印多少个System.out.print(*);}//打印完一行之后需要换行System.out.println();}}// *
// **
// ***
// ****
// *****Testpublic void test545() {// i1 j1// i2 j2// i3 j3for (int i 1; i 5; i) {for (int j 1; j i; j) {System.out.print(*);}System.out.println();}}Testpublic void test75() {for (int i 1; i 9; i) {for (int j 1; j i; j) {//System.out.println(2*714);System.out.print(j * i (i * j) \t);}System.out.println();}}4、无限循环
while(true)
for(;;)三、Switch
switch 可以接受的值(int byte 整数类型charString)
switch(2) {case 1:.....break;case 2:....break;..... default:.....break;
}Test
public void test90() {int num 2;if (num 1) {System.out.println(1);} else if (num 2) {System.out.println(2);} else if (num 3) {System.out.println(3);} else {System.out.println(else);}switch (num) {case 1:System.out.println(1);break;case 2:System.out.println(2);break;case 3:System.out.println(3);break;default:System.out.println(default);break;}
}练习1 输入月份、年份判断天数 1、3、5、7、8、10、12 -------- 31天 4、6 、9、11--------------------30天 2----------------------------------28/29天
练习2 之前用if-else做的程序使用switch完成 90 100 优秀 80 90 良好 70 80 一般 60 70 及格 60 不及格 Test
public void test1() {Scanner scanner new Scanner(System.in);System.out.println(请输入月份);int month scanner.nextInt();switch (month) {case 1:System.out.println(31天);break;case 3:System.out.println(31天);break;case 4:System.out.println(30天);break;case 5:System.out.println(31天);break;case 2:System.out.println(请输入年份);int year scanner.nextInt();break;}
}Test
public void test2() {Scanner scanner new Scanner(System.in);System.out.println(请输入月份);int month scanner.nextInt();switch (month) {case 1:case 3:case 5:case 7:System.out.println(31天);break;case 4:case 6:case 9:case 11:System.out.println(30天);break;case 2:System.out.println(请输入年份);int year scanner.nextInt();break;default:System.out.println(default);break;}
}四、作业
1、计算某年是不是闰年
能被4整除但是不能被100整除 || 能被400整除 if (() || ()) { }
2、从控制台输入两个数,然后分别打印这两个数然后交换这两个数的值 3.1、编写一个收银台收款程序if
定义输入----单价、数量、用户输入金额
定义输出----应收金额、找零
使用double类型变量 scanner.nextDouble();
3.2、当总价500时候打八折
3.3、考虑程序出现异常的情况如收款金额小于应收金额 若收款金额大于等于应收金额则计算找零后输出若收款金额小于应收金额输出错误信息。4、输出一下结构
1
12
123
1234
123455、打印正三角形和倒三角形 *************************
***********6、计算1-100以内所有奇数的和以及所有偶数的和分别打印出来。
7、用for循环输出1—1000之间能被5整除的数且每行输出3个
8、计算9的阶乘