if语句
if (关系表达式){
语句体;
}
package com.xjk;
import java.util.Scanner;
public class IfDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("你是男的吗?(true/false)");
boolean re = sc.nextBoolean();
if (re) {
System.out.println("男人");
}
System.out.println("over");
}
}
package com.xjk;
import java.util.Scanner;
public class IfDemo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int a = sc.nextInt();
System.out.println("请输入第二个整数:");
int b = sc.nextInt();
// 方式1
if (a == b) {
System.out.println("a等于b");
}
if (a != b) {
System.out.println("a不等于b");
}
// 方式2
String result = "a等于b";
if (a!=b){
result = "a不等于b";
}
System.out.println(result);
}
}
package com.xjk;
public class IfDemo3 {
public static void main(String[] args) {
if (3==4)
System.out.println("333");
System.out.println("444");
}
}
// 打印结果:444
// 原因if大括号是可以省略的,如果省略,那么控制到第一个分号结束
package com.xjk;
public class IfDemo3 {
public static void main(String[] args) {
if (3==4)
System.out.println("333");
System.out.println("444");
}
}
// 333
// 444
if...else语句
if (关系表达式) {
语句体1;
} else {
语句体2;
}
package com.xjk;
public class IfDemo4 {
public static void main(String[] args) {
int a = 4;
int b = 5;
if (a == b) {
System.out.println("a等于b");
} else {
System.out.println("a不等于b");
}
}
}
package com.xjk;
import java.util.Scanner;
public class IfDemo5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数:");
int a = sc.nextInt();
if (a%2==0) {
System.out.println("偶数");
}else {
System.out.println("奇数");
}
}
}
if ... elseif ...else
if (关系表达式1) {
语句体1;
} else if {
语句体2;
} ...
else {
语句体n;
}
package com.xjk;
import java.util.Scanner;
public class IfDemo6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个小数:");
double d = sc.nextDouble();
if (d>0) {
System.out.println("正数");
}else if (d == 0) {
System.out.println("为0");
} else if (d < 0) {
System.out.println("负数");
}
}
}
package com.xjk;
import java.util.Scanner;
public class IfDemo7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入月份:");
int month = sc.nextInt();
if (month >=3 && month<= 5) {
System.out.println("春季");
} else if (month>=6 && month<=8) {
System.out.println("夏季");
}else if (month>=9 && month<=11) {
System.out.println("秋季");
}else if ( month == 12 | month == 1 | month ==2) {
System.out.println("冬季");
}
}
}
语句格式:
switch (表达式) {
case 常量1:
语句体1;
break;
case 常量2:
语句体2;
break;
...
default:
语句体n;
break;
}
package com.xjk;
import java.util.Scanner;
public class SwitchDemo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字(1-7):");
int day = sc.nextInt();
switch(day) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
case 7:
System.out.println("星期日");
break;
default:
System.out.println("没有");
break;
}
}
}
1.case后面只能跟常量,不能跟变量。
2.多个case值不能相同。
3.case的顺序有没有要求,可以放任意位置。
4.default 不一定放在最后位置,可以放任意位置
5.default是可以省略的
6.break是可以省略的,如果省略,代码会继续向下执行,不管下面case是否成功匹配,一直遇到break,或者switch执行结束。
7.switch语句结束:遇到break,或者代码执行到switch语句最后。
package com.xjk;
import java.util.Scanner;
public class SwitchDemo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
int a = sc.nextInt();
System.out.println("请输入符号(* - + /):");
String b = sc.next();
System.out.println("请输入一个整数:");
int c = sc.nextInt();
switch(b) {
case "+":
System.out.println(a+b+c+"="+(a+c));
break;
case "-":
System.out.println(a+b+c+"="+(a-c));
break;
case "*":
System.out.println(a+b+c+"="+(a*c));
break;
case "/":
System.out.println(a+b+c+"="+(a/c));
break;
default:
System.out.println("输入不合法");
break;
}
}
}
循环语句可以在满足循环条件情况下,反复执行某一段代码。
语法格式:
for (初始化语句;判断条件语句;控制条件语句) {
循环体语句体;
}
demo
package branch;
public class BranchDemo {
public static void main(String[] args) {
for (int i=0;i<5;i++) {
System.out.println("hello world");
}
}
}
demo2
// 打印1-10的和
package branch;
public class BranchDemo2 {
public static void main(String[] args) {
int sum = 0;
for(int i=1;i<=10;i++) {
sum += i;
}
System.out.println(sum);// 55
}
}
// 求1-100 奇数和和偶数和
package branch;
public class BranchDemo3 {
public static void main(String[] args) {
int odd = 0;
int even = 0;
for(int i=1;i<=100;i++) {
if (i%2 == 0) {
even += i;
} else {
odd += i;
}
}
System.out.println("奇数和为:"+odd);// 奇数和为:2500
System.out.println("偶数和为:"+even);// 偶数和为:2550
}
}
语句格式:
while (判断条件语句) {
循环体语句;
}
demo
package branch;
public class BranchDemo4 {
public static void main(String[] args) {
int i = 0;
while (i<100) {
System.out.println("这是:"+ i);
i++;
}
}
}
语句格式
do {
循环体语句;
控制条件语句;
}while(判断条件语句);
demo
package branch;
public class BranchDemo5 {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++;
}while(i<100);
}
}
在键盘上输入一个数,如果是1,代表蛇吃到食物,得分为10,并继续,如果输入是非1数字,停止。输出分数:
package branch;
import java.util.Scanner;
public class BranchDemo6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到,贪吃蛇游戏");
System.out.println("请输入您的操作");
int result = sc.nextInt();
int score = 0;
while(result==1) {
score += 10;
System.out.println("请输入您的操作");
result = sc.nextInt();
}
System.out.println(score);
}
}
循环登录:
package branch;
import java.util.Scanner;
public class BranchDemo7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入密码:");
int secret = sc.nextInt();
while (secret!=1234) {
System.out.println("密码错误!");
System.out.println("请重新输入密码:");
secret = sc.nextInt();
}
System.out.println("登录成功");
}
}
package branch;
import java.util.Scanner;
public class BranchDemo7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入密码:");
String secret = sc.nextLine();
while (!secret.equals("abc1234")) {
System.out.println("密码错误!");
System.out.println("请重新输入密码:");
secret = sc.nextLine();
}
System.out.println("登录成功");
}
}
// do while 版本
package branch;
import java.util.Scanner;
public class BranchDemo7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String secret;
do {
System.out.println("请输入密码:");
secret = sc.nextLine();
} while (!"abc1234".equals(secret));
System.out.println("登录成功");
}
}
循环的嵌套
package branch;
public class BranchDemo8 {
public static void main(String[] args) {
for (int i=1;i<=5;i++) {
for (int j=1;j<=i;j++) {
// 内层嵌套不用换行
System.out.print("*");
}
System.out.println();
}
}
}
*
**
***
****
*****
99乘法表
package branch;
public class BranchDemo8 {
public static void main(String[] args) {
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();
}
}
}
打印字母A-Z
package branch;
public class BranchDemo9 {
public static void main(String[] args) {
for (char i=‘A‘; i<=‘Z‘;i++) {
System.out.println(i);
}
}
}
break 结束单层循环
package branch;
public class BranchDemo10 {
public static void main(String[] args) {
for (int i=0;i<10;i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
System.out.println("over");
}
}
// 0
// 1
// 2
// over
continue 只能用在循环语句中,离开使用场景没有意义
package branch;
public class BranchDemo10 {
public static void main(String[] args) {
for (int i=0;i<10;i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
System.out.println("over");
}
}
// 0
// 1
// 2
// 4
// 5
// 6
// 7
// 8
// 9
// over
return 结束整个方法,并返回方法的调用者
package branch;
public class BranchDemo10 {
public static void main(String[] args) {
for (int i=0;i<10;i++) {
if (i == 3) {
return;
}
System.out.println(i);
}
System.out.println("over");
}
}
// 0
// 1
// 2
原文:https://www.cnblogs.com/xujunkai/p/13698035.html