顺序结构
-
选择结构
-
-
package com.zhu.struct;
import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//equals:判断字符串是否想等
if (s.equals("Hello")){
System.out.println(s);
}
System.out.println("end");
scanner.close();
}
}
package com.zhu.struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
//考试分数大于60就是及格,小于60分就不及格。
Scanner scanner = new Scanner(System.in);
System.out.println("请输入考试分数");
int score = scanner.nextInt();
if (score<60){
System.out.println("不及格");
}
else{
System.out.println("及格");
}
scanner.close();
}
}
-
package com.zhu.struct;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score==100){
System.out.println("恭喜你获得满分");
}
else if(score<100 && score>=90){
System.out.println("您的等级为A等级");
}
else if(score<90 && score>=80){
System.out.println("您的等级为B等级");
}
else if(score<80 && score>=70){
System.out.println("您的等级为C等级");
}
else if(score<70 && score>=60){
System.out.println("您的等级为D等级");
}
else if(score<60 && score>=0){
System.out.println("您的等级不合格");
}
else {
System.out.println("您输入的分数不合法");
}
scanner.close();
}
}
-
package com.zhu.struct;
import java.util.Scanner;
public class SwitchDemo01 {
public static void main(String[] args) {
//case 穿透 //switch 匹配一个具体的值
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的等级");
char grade = scanner.next().charAt(0);
switch (grade) {
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("再接再厉");
break;
case 'E':
System.out.println("挂科");
break;
default:
System.out.println("未知等级");
}
scanner.close();
}
}
-
-
package com.zhu.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1~100
int i = 0 ;
while (i<100){
i++;
System.out.println(i);
}
}
}
package com.zhu.struct;
public class WhileDemo02 {
public static void main(String[] args) {
//死循环
while(true){
//等待客户端连接
//定时检查
//...........
}
}
}
package com.zhu.struct;
public class WhileDemo03 {
public static void main(String[] args) {
//计算1加到100;
int i = 0;
int sum = 0;
while (i<=100){
sum = sum+i;
i++;
}
System.out.println(sum);
}
}
-
package com.zhu.struct;
public class DowhileDemo01 {
public static void main(String[] args) {
//计算1加到100;
int i = 0;
do{
i++;
}
while (i<0);
System.out.println(i);
}
}
-
package com.zhu.struct;
public class ForDemo01 {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(i);
}
}
}
package com.zhu.struct;
public class ForDemo02 {
public static void main(String[] args) {
//计算1-100奇数与偶数的和
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i <= 100; i++) {
if(i%2!=0){
oddSum+=i;
}
else {
evenSum+=i;
}
}
System.out.println("奇数和为:"+oddSum);
System.out.println("偶数和为:"+evenSum);
}
}
package com.zhu.struct;
public class ForDemo03 {
public static void main(String[] args) {
//用for循环输出1-1000之间能被5整除的数,并且每行输出3个
for (int i = 1; i <=1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
}
if(i%15==0){
System.out.println("\n");
}
}
//println 输出完会换行
//print 输出完不会换行 \t:等于tab键 \n:换行
}
}
package com.zhu.struct;
import java.util.Scanner;
public class ForDemo04 {
public static void main(String[] args) {
int i = 1;
//用While循环输出1-1000之间能被5整除的数,并且每行输出3个
while (i<=1000){
if(i%5==0){ //判断i能不能被5整除
System.out.print(i+"\t");//如果能被5整除。则输出i并且空几格
}
i++;//i自增,以便进行下一个数的判断
if(i%15==0){ //判断如果输出的3个数,就换行
System.out.println("");
}
}
}
}