//顺序结构
// Java的基本结构就是顺序结构,除非特别只没,否则就按照顺序一句一句执行。
/*语句与语句之间,框与框之间是按从上到下的顺序进行的,它
是由若干个依次执行的处理步骤组成的,他是任何一个算法都离不开的一种基本算法结构。*/
public static void main(String[] args) {
//顺序结构示例演示
System.out.println ("hello1");
System.out.println ("hello2");
System.out.println ("hello3");
System.out.println ("hello4");
System.out.println ("hello5");
System.out.println ("hello6");
}
}
import java.io.StringReader;
import java.util.Scanner;
//if单选择结构 if(){}
//if双选择结构if(){}else{}
//if多选择结构if(){else if{}}
//嵌套的if选择结构if(){if(){}}
//swith多选择结构
//我们很多时候需要去判断一个东西是否可行,
// 然后我们才去执行,这样一个过程在程序中if语句来表示。
public class Demo2 {
//if单选择结构示例演示
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 ("emd");
scanner.close ();
}
}
package 结构;
import java.util.Scanner;
//if双选择结构
public class Demo3 {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
//考试分数大于60是及格,小于60是不及格
System.out.println ("请输入成绩:");
int score = scanner.nextInt ();
if(score>60){
System.out.println ("及格");
}else{
System.out.println ("不及格");
}
scanner.close ();
}
}
// if多选择结构
public static void main(String[] args) {
//注意点:if语句至多有一个else语句,else语句在所有的else if 语句之后。
// if语句可以有若千个else if 语句,它们必须在else语句之前。
// 一旦其中一个else if 语句检测为 true,其他的else if 以及else语句都将跳过执行。
Scanner scanner = new Scanner (System.in);
//考试分数大于60是及格,小于60是不及格
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 ("成绩不合法");
}
}
}
public class forDemo1 {
public static void main(String[] args) {
int a = 1; //初始化条件
while (a<=100){ //条件判断
System.out.println (a); //循环体
a+=2;//迭代,就是每一次循环都会将a的值刷新
}
System.out.println ("循环结束");
//初始化值 条件判断 迭代
for(int i =1; i<=100;i++){
System.out.println (i);
}
System.out.println ("循环结束");
}
}
//虽然所有循环结构都可以用while或者d...while表示,但Java提供了另-种语句-一for 循环,使- -些循环结构变得更加简单。
//
//for循环语句是支持迭代的一-种通用结构,是最有效、最灵活的循环结构。
//
//for循环执行的次数是在执行前就确定的。语法格式如下:
//
//for(初始化;布尔表达式;更新) {//代码语句
//
//}
//练习1:计算0到100之间的奇数和偶数的和
public static void main(String[] args) {
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i<=100; i++){
if(i%2!=0){
//奇数
sum1+=i; //sum1 = sum1+i;
}else {
//偶数
sum2+=i;
}
}
System.out.println ("奇数和"+sum1);
System.out.println ("偶数和"+sum2);
}
}
//练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
for (int i = 0; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
}
if(i%(5*10)==0){//每行
System.out.println ();
//println会换行
//print不会换行
}
}
}
}
//打印九九乘法表
//1我们先打印第一列
// 2我们把固定的1再用一个循环包起来
// 3去掉重复项i<=j
//4.调整样式
for (int j = 1; j<=9;j++){
for(int i = 1; i<=j;i++){
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println ();
}
}
}
//增强for循环
public class forDemo5 {
public static void main(String[] args) {
int [] sum = {10,20,30,40,50};//定义一个数组
for (int i= 0;i<5;i++){
System.out.println (sum[i]);
}
System.out.println ("==================");
//遍历数组的元素
for (int x:sum){
System.out.println (x);
}
}
}
//只要布尔表达式为true,循环就会一直执行下去。
//
//我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。◆少部分情况需要循环-直执行,比如服务器的请求响应监听等。
//
//循环条件-直为true就会造成无限循环 [死循环],我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死奔溃!
//
//思考:计算1 +2+3+...+100=?
public class whileDemo3 {
public static void main(String[] args) {
//输出1~100
int i= 0;
while(i<100){
i++; //+1
System.out.println (i);
}
}
}
//死循环
//while(true){}
//计算1 +2+3+...+100=?
public class whileDemo4 {
public static void main(String[] args) {
int i = 0;//初始值
int sun = 0;//总和
while (i<=100) {
sun = sun + i;
i++;
}
System.out.println (sun);
}
}
public class 打印三角形 {
public static void main(String[] args) {
//打印(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 ();
}
}
}
//switch
public static void main(String[] args) {
char grade = 'c';
switch (grade) {
//case穿透 //switch匹配一个具体的值
case 'a':
System.out.println ("优秀");
break;//可选,不加break,下面没有加break语数将显示出来。
case 'b':
System.out.println ("良好");
case 'c':
System.out.println ("不及格");
break;
}
}
}
public static void mian(String [] args) {
String name = "booles";
switch (name){
case "booles":
System.out.println ("booles");
break;
case "boolres":
System.out.println ("boolres");
break;
default:
System.out.println ("....");
}
}
}