package pro0622;
import java.util.Scanner;
//分支结构中的单层if
/*
if(条件){
操作
}
注意1:条件必须是Boolean类型
注意2:如果操作只有一行,可以省略大括号,但希望写上
注意3:条件满足执行操作,条件不满足结束分支
*/
public class DemoA {
//小明参加考试,如果成绩大于80,奖励
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入小明的成绩:");
int score = in.nextInt();
if (score > 80) {
System.out.println("奖励!");
}
System.out.println("if结构已经结束");
}
}
/*
1.顺序结构:自顶向下运行方式
2.分支结构:多条路中选择其中的一条或者一条狗不选择
2.1:单层if结构(有一条路进行选择,要么走,要么不走)
2.2:if-else结构(有两条路进行选择,选择其中的一条)
2.3:多重if结构(有多条路进行选择,选择其中的一条)
2.4:switch-case结构(等同于多重if)
3.循环结构:重复的执行某些相似的操作
3.1:while循环(先判断后执行)
3.2:do-while循环(限制现再判断)
3.3:for循环(先判断再执行)
*/
package pro0622;
import java.util.Scanner;
//分支结构的if-else结构
/*
if(条件){
操作1
}else
操作2
*/
public class DemoB {
public static void main(String[] args){
//猜数字问题:用户输入一个数,系统随机生成一个数,判断是否猜中
Scanner in = new Scanner(System.in);
System.out.print("请输入一个数:");
int a = in.nextInt();
int b = (int)(Math.random()*6+1);//1-6
if(a==b){
System.out.println("恭喜你,猜中了!");
}else{
System.out.println("很遗憾,没有猜中,系统生成的数为:"+a+",你输入的是:"+b);
}
System.out.println("if-else循环结束了");
}
}
package pro0622;
//分支结构 多重if结构
/*
if(条件1){
操作1
}else if(条件2){
条件2
}else if(条件3){
条件3
}.....
注意1:可以后多个else if
注意2:else可以省略,如果省略,那么可能一条路也不选择
*/
public class DemoC {
public static void main(String[] args){
//随机生成一个成绩,如果大于80,A,70,B,60,C,小于60,D
int score = (int)(Math.random()*101);
if(score>80){
System.out.println("你的成绩是:"+score+"结果为:A");
}else if(score>70){
System.out.println("你的成绩是:"+score+",结果为:B");
}else if(score>60){
System.out.println("你的成绩是:"+score+",结果为:C");
}else if(score<60){
System.out.println("你的成绩是:"+score+",结果为:D");
}
System.out.println("多重if结构完毕");
}
}
package pro0622;
import java.util.Scanner;
public class DemoD {
public static void main(String[] args){
//用户输入一个带包年月的六位整数,判断这个月又多少天
Scanner in = new Scanner(System.in);
System.out.println("请输入一个六为整数:");
int date = in.nextInt();
int year = date/100;
int month = date%100;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
System.out.println(month+"月有31天");
}else if(month==4||month==6||month==9||month==11){
System.out.println(month+"月有30天");
}else if(month==2){
if(year%400==0||(year%100!=0&&year%4==0)){
System.out.println(month+"月有29天");
}else{
System.out.println(month+"月有28天");
}
}else{
System.out.println("日期输入错误");
}
}
}
package pro0622;
//分支结构 switch-case结构
/*
switch(变量)
case 常量1:
操作1
break;
case 常量2:
操作2
break;
...
case 常量n:
操作n
break;
default
注意1:变量和常量只支持:整数、字符型和字符串
注意2:switch-case只是等职操作,不能进行区间操作
注意3:break不能省略,如果省略就不是分支了
注意4:case常量值不能重复
*/
public class DemoE {
public static void main(String[] args){
int a = (int)(Math.random()*6+1);
switch (a) {
case 1:
System.out.println("A");
break;
case 2:
System.out.println("B");
break;
case 3:
System.out.println("C");
break;
case 4:
System.out.println("D");
break;
case 5:
System.out.println("E");
break;
case 6:
System.out.println("F");
break;
default
}
}
}
/*
多重if和switch-case之间的区别
1.多重if可以执行区间和等值操作,switch-case自能执行等值操作
2.多重if条件为Boolean类型,switch-case为整数、字符型和字符串
3.
*/
package XunHuan;
//循环结构-while循环
/*
while(条件){
操作;
}
特点:先判断,后执行
*/
public class Lian01 {
public static void main(String[] args)
{
/* //循环输出1-10十个数
int i=1;//定义一个变量,初始值为1
while(i<11){//循环判断
System.out.println(i);
i++;//循环值的更改
}*/
//求出1-100之和、
int i=1;
int cnt=0;
while(i<=100){
cnt+=i++;
}
System.out.println(cnt);
}
}
package XunHuan;
import java.util.Scanner;
//循环结构——do-while循环
/*
do{
操作;
}while(条件);
特点:先执行,再判断
*/
public class Lian02 {
public static void main(String[] args) {
/*int i=1;
int sum=0;
do{
sum+=i++;
}while(i<=100);
System.out.println(sum);*/
//猜数字,生成一个数字,用户进行猜,猜对结束并提示,猜错继续猜
int a=(int)(Math.random()*101);
int b;
int cnt=0;
Scanner in=new Scanner(System.in);
do{
b=in.nextInt();
if(b<a){
System.out.println("猜小了");
}
else if(b>a){
System.out.println("猜大了");
}
else{
System.out.println("恭喜你猜对了");
}
cnt++;
}while(a!=b);
System.out.println("你总共猜了"+cnt+"次");
}
}
/*
while和do-while之间的区别?
1.while是先判断后执行,而do-while是先执行判断
2.while条件不满足,一次都不执行,而do-while会执行一次
*/
package XunHuan;
//for循环应用
//for(循环开始值;循环条件;循环递增/递减值);
import java.util.Scanner;
//循环结构--break关键字
//break:结束循环
//continue:结束当次循环,开始下次循环
public class Lian03 {
public static void main(String[] args) {
//打印9*9乘法表
/*for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
System.out.print(" i*j="+i*j);
}
System.out.println();
}*/
/*
打印:中间为空的三角形
*
* *
* *
* * * *
*/
int cnt=1;
Scanner in=new Scanner(System.in);
int b=in.nextInt();
for(int i=0;i<b;i++){
for(int j=0;j<cnt;j++){
if(j==0||j==cnt-1||i==b-1) {
System.out.print("* ");
}
else if(j!=0){
System.out.print(" ");
}
else if(j!=cnt-1) {
System.out.print(" ");
}
}
System.out.println();
cnt++;
}
}
}
package XunHuan;
//起名字,直接跳出指定循环
public class Lian04 {
public static void main(String[] args) {
int t=0;
a:for (int i = 0; i <5 ; i++) {
b:for (int j = 0; j <5 ; j++) {
if(j==3){
t=j;
break a;
}
}
}
System.out.println(t);
}
}
循环、分支
最新推荐文章于 2024-01-27 01:01:13 发布