Day05

if选择结构

package struct;
import java.util.Scanner;
public class ifDemo01 {
    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);
        }else {
            System.out.println("End");
        }    scanner.close();
    }
}
package struct;

import java.util.Scanner;

public class ifDemo02 {
    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("不及格9");
        }



        scanner.close();
    }
}

package struct;

import java.util.Scanner;

public class ifDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        /*
        if 语句至多有1个else语句,else语句在所有的else if 语句之后,
        if语句可以有若干个else if语句,他们必须在esle语句之前。
        一旦其中一个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();
    }
}

Switch选择结构

package struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透    //匹配一个具体的值
        char grade = 'A';

        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 struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "yg";
        //JDK7的新特性,表达式结果可以是字符串!!!
        //字符的本质还是数字
        //反编译  java---class(字节码文件)---反编译(IDEA)
        switch (name){
            case "ygg":
                System.out.println("ygg");
                break;
            case "yg":
                System.out.println("yg");
                break;
            default:
                System.out.println("???");
        }
    }
}

While循环

package struct;

public class WhileDemo01 {
    public static void main(String[] args) {

        //输出1~100
        int i =0;

        while (i<100){
            i++;
            System.out.println(i);
        }



    }
}

package struct;

public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环
        while (true){
            //等待客户端连接
            //定时检查
        }
    }
}

package struct;

public class WhileDemo03 {
    public static void main(String[] args) {
        int i = 0;
        int sum =0;

        while (i<=100){
            sum = sum +i;
            i++;
        }
        System.out.println(sum);
    }
}

DoWhile循环

package struct;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum =0;
        do{
            sum = sum +i;
            i++;
        }while (i<=100);
        System.out.println(sum);
    }
}
package struct;


public class DoWhileDemo02 {
    public static void main(String[] args) {
        int a = 0;
        while(a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("===============");
        do {
            System.out.println(a);
        }while (a<0);
    }
}

For循环

package struct;

public class ForDome01 {
    public static void main(String[] args) {
        int a = 1;  //初始化条件
        while (a<=100){  //条件判断
            System.out.println(a);  //循环体
            a+=2;  //迭代
        }
        System.out.println("while循环体结束!");
        //初始化条件//条件判断//迭代
        for (int i=1; i<=100;i++){
            System.out.println(i);
        }
        System.out.println("for循环体结束");
    }
}

For循环练习1:计算0到100之间的奇数和偶数的和

package struct;

public class ForDome02 {
    public static void main(String[] args) {
        //练习1!计算0到100之间的奇数和偶数的和

        int oddSum = 0;
        int evenSum = 0;
        for (int i = 0; i < 100; i++) {
            if (i%2!=0){  //奇数
                oddSum+=i;  //oddSum = oddSum + i
            }else {   //偶数
                evenSum+=i;
            }
            
        }
        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);
    }
}



运行结果:奇数的和:2500
	 	偶数的和:2450

For循环练习2:用while或for循环输出1-1000能被5整除的数,并且每行输出3个

package struct;

public class ForDome03 {
    public static void main(String[] args) {
        //练习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*3)==0){    //每行
                System.out.println();
                //System.out.print("\n");
            }
        }
        //println 输出完,会换行
        //pinnt  输出完,不会换行
    }
}


运行结果:0	
        5	10	15	
        20	25	30	
        35	40	45	
        50	55	60	
        65	70	75	
        80	85	90	
        95	100	105	
        110	115	120	
        125	130	135	
        140	145	150	
        155	160	165	
        170	175	180	
        185	190	195	
        200	205	210	
        215	220	225	
        230	235	240	
        245	250	255	
        260	265	270	
        275	280	285	
        290	295	300	
        305	310	315	
        320	325	330	
        335	340	345	
        350	355	360	
        365	370	375	
        380	385	390	
        395	400	405	
        410	415	420	
        425	430	435	
        440	445	450	
        455	460	465	
        470	475	480	
        485	490	495	
        500	505	510	
        515	520	525	
        530	535	540	
        545	550	555	
        560	565	570	
        575	580	585	
        590	595	600	
        605	610	615	
        620	625	630	
        635	640	645	
        650	655	660	
        665	670	675	
        680	685	690	
        695	700	705	
        710	715	720	
        725	730	735	
        740	745	750	
        755	760	765	
        770	775	780	
        785	790	795	
        800	805	810	
        815	820	825	
        830	835	840	
        845	850	855	
        860	865	870	
        875	880	885	
        890	895	900	
        905	910	915	
        920	925	930	
        935	940	945	
        950	955	960	
        965	970	975	
        980	985	990	
        995	1000

For循环练习3:九九乘法表(重要)

package struct;

public class ForDemo04 {
    public static void main(String[] args) {
        //1.打印第一列,
        //2.我们把固定的1再用一个循环包起来
        //3.去掉重复项,i<=j
        //调整样式
        for (int j = 0; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(i+"*"+j+"="+(j*i) + "\t");
            }
            System.out.println();
        }

    }
}


运行结果:
    	1*1=1	
        1*2=2	2*2=4	
        1*3=3	2*3=6	3*3=9	
        1*4=4	2*4=8	3*4=12	4*4=16	
        1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
        1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
        1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
        1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
        1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值