一、顺序结构
二、选择结构
import java.util.Scanner;
public class ifdemo {
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();
}
}
import java.util.Scanner;
public class ifdemo2 {
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();
}
}
import java.util.Scanner;
public class ifdemo3 {
public static void main(String[] args) {
//考试分数大于60就是及格,小于60分就不及格
Scanner scanner = new Scanner(System.in);
/**
* if 语句至多有1个else语句,else语句在所有的else if语句之后。
* if 语句可以有若干个else if语句,它们必须在else语句之前。
* 一旦其中有一个else if语句检测为true,其他的else if以及else语句将跳过执行。
*/
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.xqstudy.structure;
public class SwitchDemo1 {
public static void main(String[] args) {
//case穿透 switch匹配一个具体的值
char grade = 'C';
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("未知等级");
}
}
}
package com.xqstudy.structure;
public class SwitchDemo2 {
public static void main(String[] args) {
String name = "雪琴";
//JDK7的新特性,表达式结果可以是字符串!!!
//字符的本质还是数字
// 反编译 java---class(字节码文件)---反编译(IDEA)
switch (name){
case "刘岩":
System.out.println("刘岩");
break;
case "雪琴":
System.out.println("雪琴");
break;
default:
System.out.println("弄啥嘞!");
}
}
}
在idea中字节码文件不能直接复制拖拽打开,但可以将对应的字节码文件(.class)拷贝到对应的文件夹中打开
三、循环结构
//计算1+2+3……+100=?
int i=0;
int sum=0;
while (i<=100){
sum+=i;
i++;
}
System.out.println(sum); //5050
int i=0;
int sum=0;
do {
sum+=i;
i++;
}while (i<=100);
System.out.println(sum);
while和do……while的区别
int a=0;
while (a<0){
System.out.println(a);
a++;
}
System.out.println("==========");
do {
System.out.println(a);
a++;
}while (a<0);
重点:for循坏
快捷键: 数值.for ;eg.or100.for
//初始化;条件判断;迭代
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}
//死循环,三个条件都可为空
/**
* for(;;;){}
*/
//练习1:计算0-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("奇数的和:"+evenSum);
System.out.println("偶数的和:"+oddSum);
//练习2
for (int i = 0; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){ //每行
System.out.println();
// System.out.println("\n");
}
}
/*
println 输出会换行
print 输出不会换行
*/
//练习3
for (int i = 1; i <= 9; i++) {
for (int n = 1; n <= i; n++) {
System.out.print(n+"*"+i+"="+(i*n)+"\t");
}
System.out.println();
}
主要是用来遍历数组和集合的
int[] numbers = {10,20,30,40,52}; //定义了一个数组
//遍历数组的元素
for (int i = 0; i < 5; i++) {
System.out.println(numbers[i]);
}
System.out.println("===========");
//增强for循环
for (int x:numbers) {
System.out.println(x);
}
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i==10){
break;
}
}
System.out.println("123");
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;
}
System.out.println(i);
}
练习
//打印三角形 5行
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
标签:顺序,int,System,else,循环,println,结构,public,out
From: https://www.cnblogs.com/xqstudy/p/18111604