1.If语句
if (比较、逻辑运算符) {
代码块
} else if (比较、逻辑运算符) {
代码块
} else if (比较、逻辑运算符) {
代码块
} else { // 不包含以上两种情况所剩余的
代码块
}
//只执行一个符合条件的代码块
代码举例:
@Test
public void test1(){
boolean bool = false;
if(bool){
System.out.println("if...");
} else {
System.out.println("else...");
}
}
举例:
成绩:
>=90 <=100 优秀
>=80 <90 良好
>=70 <80 一般
>=60 <70 及格
< 60 不及格
//假设当前输入成绩为89
@Test
public void test2(){
int score = 89;
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("不及格");
}
}
Scanner用法
第一步:导入sacnner类 import Java.until.*;(写在公共类的来头)
第二步:创建Scanner对象 Sacnner scanner=new Scanner(system.in);//构造Scanner类的对象sc,接收从控制台输入的信息
第三步:打印输出提示符号 System.out.println("提示文字”);
第四步:获取键盘输入的数据 Int score =input.nextlnt();
第五步:打印输出值:System.out.println("打印输出的值为:");
在控制台输入数据:
//在控制台可以输入信息
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score = scanner.nextInt();
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("不及格");
}
}
存在非法输入:(输入的成绩不合法)
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score = scanner.nextInt();
// System.out.println(score);
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 if (score < 0 || score > 100) {
System.out.println("这是非法输入");
} else {
System.out.println("不及格");
}
}
卫语句:
表达异常的分支时,少用 if-else 方式,这种方式可以改写成:
if (condition) {
.....
return obj;
} // 接着写 else 的业务逻辑代码,
说明: 如果非使用 if)...else if...else...方式表达逻辑,避免后续代码维护困难,请勿超过3 层
正例:超过 3 层的if-else 的逻辑判新代码可以使用卫语句、策略模式、状态模式等来实现,其中卫语句示例如下
@Test
public void test3() {
//通过Scanner可以实现从控制台输入信息
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score = scanner.nextInt();
//非法输入-使用卫语句
if (score < 0 || score >100){
System.out.println("这是非法输入");
//return方法后面的代码不再执行
return;
}
//合法输入
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("不及格");
}
}
循环
循环的三要素:
1、循环条件的初始化
2、循环条件的控制
3、循环条件的改变
2.While语句
代码实现:
while (boolean) {
......
//可以反复执行
}
@Test
public void tset1() {
//用while循环输出5遍HelloWorld
int i = 1;
while (i <= 5) {
System.out.println("HelloWorld");
i++;
}
}
3.For语句
for(表达式1; 表达式2; 表达式3){
语句块
}
步骤:
1.先执行“表达式1”;
2.再执行“表达式2”,如果它的值为真(非0),则执行循环体,否则结束循环;
3.执行语句块;
4.执行表达式3;
5.再重复执行2、3、4步直到表达式2值为假。
@Test
public void tset2() {
for (int i = 1; i <= 5; i++) {
System.out.println("HelloWorld");
i++;
}
}
累加:
举例:1+2+3+... + 100
@Test
public void test3() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
System.out.println(i);
sum += i;//sum = sum + 1;
System.out.println("sum: " + sum);
}
System.out.println("sum: " + sum);
}
统计:
举例:计算1-100以内7的倍数的个数
@Test
public void tset4() {
int count = 0;
for (int i = 1; i <= 100; i++){
if (i % 7 == 0) {
System.out.println(i);
count++;
}
}
System.out.println("count: " + count);
}
continue、break
continue:跳出本次循环,继续下一次循环
break:跳出离他最近的那层循环
@Test
//continue:跳出本次循环,继续下一次循环
public void teat1() {
for (int i = 0; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
@Test
//break:跳出离他最近的那层循环
public void test2() {
for (int i = 0; i <= 5; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}
}
例题:
随机生成一个整数1-1000用户输入一个整数,程序给出与存储的数字是“大”或者“小”,知道用户猜到这个数字位置。
如果中途用户希望程序退出,输入0可以退出。int num = 200;
猜吧!300太大了猜吧!180太小了猜吧!200恭喜你,猜对了!
@Test
public void test4() {
Scanner scanner = new Scanner(System.in);
//随机生成一个整数
Random random = new Random();
//输入1000 bound自动生成
//范围[0~1000)
int num = random.nextInt(1000) + 1;
System.out.println("num: " + num);
//while适合判断类型
//while(ture)无限循环、死循环;for(;;)死循环、无限循环
while (true){
System.out.println("猜吧!!");
int guess = scanner.nextInt();
if (guess > num) {
System.out.println("太大了");
}else if (guess < num){
System.out.println("太小了");
}else {
System.out.println("恭喜你,猜对了");
break;
}
}
双层For循环
外层循环控制行数,数一下有几行就能确定外层循环。
内层循环控制列数,这一行打印多少个,到底要打印多少个要找出和当前行之间的一个关系。
例1:
打印如下情况
****
****
****
//****
//****
//****
@Test
public void test6() {
//i=1代表打印第一行
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 4; j++) {
System.out.print("*");
}
//打印完一行之后换行
System.out.println();
}
}
例2:
*
**
***
****
*****
public static void main(String[] args) {
//i=1 j=1;i=2 j=2;i=3 j=3....
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
例3:
打印九九乘法表
@Test
public void test67() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " * " + i + " = " + (i * j) + "\t");
}
System.out.println();
}
}
3.Switch语句
switch: 可以接受的值(int byte 整数类型,char,String)
switch(2) {
case 1:
.....
break;
case 2:
....
break;
.....
default:
.....
break;
}
如果执行case1但是case1中没有break,则输出case1与case2的内容。
代码举例:
@Test
public void test9() {
int num = 2;
switch (num) {
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
case 3:
System.out.println("3");
break;
}
}
例:
判断闰年
//计算年份是不是闰年
@Test
public void test1() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年份:");
int year = scanner.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println("该年份为闰年");
} else {
System.out.println("该年份不是闰年");
}
}
例:
输入月份、年份判断天数:
1、3、5、7、8、10、12 -------- 31天
4、6 、9、11--------------------30天
2--------------------------28/29天(判断闰年)
@Test
public void test10() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入月份");
int month = scanner.nextInt();
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30天");
break;
case 2:
System.out.println("28/29天");
System.out.println("请输入年份:");
int year = scanner.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println("29天");
} else {
System.out.println("28天");
}
break;
}
}
例:
从控制台输入两个数,然后分别打印这两个数,然后交换这两个数的值,输出
@Test
public void test21() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数: ");
int num1 = scanner.nextInt();
System.out.println("请输入第二个数: ");
int num2 = scanner.nextInt();
int temp = num1;
num1 = num2;
num2 = temp;
System.out.println(num1);
System.out.println(num2);
}
例:
按结构输出
1
12
123
1234
12345
@Test
public void test42() {
for (int i = 1; i <= 5; i++) {
int j = 1;
while (j <= i) {
if (j == i) {
System.out.println(j);
} else {
System.out.print(j);
}
j++;
}
}
}
例:
打印正三角形和倒三角形
*
***
*****
*******
*********
//打印正三角形
@Test
public void test53() {
for (int i = 1; i <= 9; i += 2){
for (int k = (9 - i) / 2; k > 0; k--){
System.out.print(" ");
}
for (int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println();
}
}
//打印倒三角形
@Test
public void test54() {
for (int i = 9; i >= 0; i -= 2) {
for (int j = 0; j < (9 - i) / 2; j++) {
System.out.print(" ");
}
for (int k = i; k > 0; k--){
System.out.print("*");
}
System.out.println();
}
}
例:
用for循环输出1—1000之间能被5整除的数,且每行输出3个
@Test
public void test71() {
int count = 0;
for (int i = 1; i <= 1000; i++) {
if (i % 5 == 0) {
count++;
if (count < 3) {
System.out.print(i + " ");
} else {
System.out.println(i);
count = 0;
}
}
}
}
例:
计算9的阶乘
@Test
public void test81() {
int count = 1;
for (int i = 9; i > 0; i--) {
if (i > 1) {
System.out.print(i + " * ");
} else {
System.out.print(i + " = ");
}
count *= i;
}
System.out.print(count);
}