//if 选择语句双向选择语句
package struct;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//scanner键入和关闭先写进去
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
//定义一下等会儿所需要输入的内容所用到的定义,int,double等等
int score=scanner.nextInt();
//语句写进去,然后输出
if (score>60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
scanner.close();
}
}
//二、if多选择结构
package struct;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score=scanner.nextInt();
/*以下相当于其中有一项成立直接进去语句
其他的全部跳过,但是一旦前面有一个if
后面一定要有一个else结尾。
*/
if (score==100){
System.out.println("满分");
}else if (score<100&&score>=90){
System.out.println("A等级");
}else if (score<90&&score>=70){
System.out.println("B等级");
}else if (score<70&&score>=60){
System.out.println("及格");
}else if (score>=0&&score<60){
System.out.println("成绩不合格");
}
else {
System.out.println("成绩不合法");
}
scanner.close();
}
}
//三、switch多选择结构(其实就是一种结果在多种情况下成立,然后进入语句输出结果)
package struct;
public class SwitchDemo02 {
public static void main(String[] args) {
//1。定义所需要的字符
String name="周树人";
//JDK7新特性,表达式结果可以是字符串。
/*switch是一个字符判断属于多种情况中的哪一种,
然后输出结果,如果都不属于输出default结果也可。
*/
//2.使用方法
switch (name){
case "周树人":
System.out.println("周树人");
break;
case "欧吼":
System.out.println("欧吼");
break;
default:
System.out.println("弄啥勒!");
}
}
}
四、while多循环结构(先判断后执行)
package struct;
public class WhileDemo03 {
public static void main(String[] args) {
/*1.while可以进行多次循环不像if,
switch语句只能执行一次,
所以需要有一个终结的命令。
2.对于while而言,不满足条件则不进入循环
*/
int i=0;
int sum=0;
while (i<=100){
sum=sum+i;
i++;
}
System.out.println(sum);
}
}
五、do while循环语句(先执行后判断)
package struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
/*while与do while的区别
就是while是先判断后执行,
另一个是先执行后判断
*/
int i=0;
int sum=0;
do {
sum=sum+i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
六、for循环(前面是while,后面是for循环,两个是比较)
/* for循环里面进行输出的话会输出每次
循环的结果,外部输出的话会输出最终一次的循环 */
/*for循环是支持迭代的一种
通用结构最有效、最灵活的循环结构
与while、do while的区别就是
不是用字符条件来决定执行次数,for
是执行执行就决定了执行次数
*/
package struct;
public class ForDemo01 {
public static void main(String[] args) {
int a=1;//初始化条件
while (a<=100){
System.out.println(a);//循环体
a+=2;//迭代
}
System.out.println("while循环结束");
int sum=0;
for(int i=1;i<=100;i++){
sum=sum+i;
System.out.println(i);
System.out.println(sum);
}
System.out.println("for循环结束");
}
}
练习1.
机算0-100之间的奇数和偶数的和(0到100之间包含100)
package struct;
public class ForDemo02 {
public static void main(String[] args) {
int o=0;
int e=0;
/* for循环里面进行输出的话会输出每次
循环的结果,外部输出的话会输出最终一次的循环 */
for(int i=0;i<=100;i++){
if (i%2!=0){
o+=i;
}else {
e+=i;
}
}
System.out.println(o);
System.out.println(e);
}
}
练习2.
机算0-1000之间能被5整数的数字,并且每行三个输出
package struct;
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if (i % 5 == 0){
System.out.print(i+"\t");// \t相当于Tab键
}
if (i % (5*3) == 0){
System.out.println();
//System.out.print("\n");
}
/* ↑相当于print的时候没有换行
无限输出,当开始为3个5的倍数模为0
的时候开始用println换行输出
*/
/*
换行
println输出完会换行
print输出完不会换行
*/
}
}
}
练习3
打印99乘法表
package struct;
public class ForDemo04 {
public static void main(String[] args) {
for (int j = 0; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i+"x"+j+"="+i*j+"\t");
}
System.out.println();
}
}
}
练习4
打印三角形
package struct;
public class LianXi {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
/*以下两个是对称补充,没有i=j
这步骤是因为图形有一个中间数要形成对称
*/
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
//↓注意点
/*
//死循环
for (;;){
}
*/
七、break与continue
package struct;
public class ContinueDemo {
public static void main(String[] args) {
int i=0;
while (i<100) {
i++;
if (i % 10 == 0) {
System.out.println();
/* continue用于终止本次的循环,
但是还会进入下一次循环,也就是到10的时候
之后的代码无效 */
continue;/*如果这里用了break直接结束
代码答案为0-9*/
}
System.out.println(i);
}
}
}
goto关键字(不需要掌握,了解即可)
package struct;
public class LabelDemo {
public static void main(String[] args) {
int count=0;
outer:for (int i=101;i<150;i++){
for (int j=2;j<i/2;j++){
continue outer;
}
System.out.print(i+" ");
}
}
}
八、
if、while、switch、for等选择语句、循环语句
最新推荐文章于 2022-11-13 19:11:53 发布