1.if分支 单分支 双分支 多分支
代码如下:
package com.yzh70625;
/**
* @author: XYT
* @create-date: 2022/6/25 15:51
*/
import java.util.Scanner;
public class ceshi {
public static void main(String[] args) {
//if分支
//单分支,双分支,多分支
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的分数");
int score = sc.nextInt();
//if单分支
// if(score>=90) {
// System.out.println("奖励吃大餐");
// }
//if双分支
// if(score>=90) {
// System.out.println("奖励吃大餐");
// }else{
// System.out.println("面壁三天");
// }
//if多分枝
if(score>=90){
System.out.println("优秀");
}else if(score>=80){
System.out.println("良好");
}else if(score>=60){
System.out.println("中等");
}else{
System.out.println("差");
}
}
}
2.switch-case循环
代码如下:
package com.yzh70625;
/**
* @author: XYT
* @create-date: 2022/6/25 15:51
*/
import java.util.Scanner;
public class ceshi {
public static void main(String[] args) {
System.out.println("请输入令狐冲比武名次:");
Scanner scanner = new Scanner(System.in);
int mc = scanner.nextInt();
//String xq = "星期一";
double x = 11.34;
//类型:char,byte,short,int,String,enum
switch (mc){
case 1:
System.out.println("武林盟主");
break;
case 2:
System.out.println("武当掌门");
//break;
case 3:
System.out.println("峨嵋掌门");
break;
default:
System.out.println("逐出师门");
break;
}
}
}
运行结果如下:
3.do…while break continue 死循环 嵌套循环
代码如下:
public static void main(String[] args) {
//while,do..while,for,嵌套,break,continue
//四要素:计数变量初始化,条件判断,循环体,计数更新
/*int i=1;
while (i<=10){
System.out.println("第"+i+"次,我能学会java");
i++;
}*/
//实现无固定次数循环
/*Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("吃一个包子");
//条件:吃饱没有
System.out.println("吃饱了么?");
String answer = scanner.next();
if("吃饱了".equals(answer)){
//退出循环
break;
}
}*/
//do..while:条件不成立时,至少执行一次
/*int i = 101;
do{
System.out.println("第"+i+"次,我能学会java");
i++;
}while (i<=100);*/
/*int sum =0;
for (int i =1; i < 101; i++) {
sum+=i;
//求奇数和,偶数和
if(i%2==0){
}
}
System.out.println(sum);*/
//死循环
//for(;;);
//break,continue
/*for (int i=1;i<=10;i++){
if(i==5){
//遇到break中断循环
//break;
//遇到continue跳过后面代码不执行,进入下次循环
continue;
}
System.out.println(i);
}*/
//嵌套循环
//输出5*5的星星
/*
*****
*****
*****
*****
*****
*/
//外层控制轮数
for (int i=1;i<=5;i++){
//每轮的内容(列数)
for (int j=1;j<=5;j++){
System.out.print("*");
}
//换行
System.out.println();
}
//九九乘法表
}
4.9乘9乘法表
代码如下:
package com.yzh70625;
/**
* @author: XYT
* @create-date: 2022/6/25 17:22
*/
public class jiuchengjiu {
public static void main(String[] args) {
//9×9乘法表
for (int i = 1; i < 10; i++) {
for (int j = 1; j <=i; j++) {
//System.out.print(i+"×"+j+"="+i*j+" ");
//\t 制表符 等间距
System.out.print(i+"×"+j+"="+i*j+"\t");
}
System.out.println();
}
}
}
运行结果如下:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118150.html