韩顺平0基础学Java——第7天

p110-p154

控制结构(第四章)

多分支

if-elseif-else

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		Scanner myscanner = new Scanner(System.in);
		System.out.println("input your score?");
		int score = myscanner.nextInt();
		if(score==100){
			System.out.println("excellent!!");
		}
		else if (score<=99&&score > 80){
			System.out.println("GOOD~~");
		}
		else if (score<=80&&score>=60){
			System.out.println("ok");
		}
		else{
			System.out.println("not ok!!");
		}
		System.out.println("go on...your score is\t"+score);
	}
}

但是你输入110的时候也会报不及格哎!我怎么没想到...所以我们先对输入的信用分进行有效判断

如果用户输入的是hello咋整捏?好吧听说后面会讲...(异常处理)

案例:

输出b啊

嵌套分支

建议不要超过3层。

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		Scanner myscanner = new Scanner(System.in);
		System.out.println("input your score?");
		double score = myscanner.nextDouble();
		System.out.println("input your gender?(man/woman)");
		String gender = myscanner.next();
		if(score>8.0){
			if (gender.equals("man")){
			System.out.println("you will go to man's final competition");
			}
			else if(gender.equals("woman")){
				System.out.println("you will go to woman's final competition");
			}
		}
		else{
			System.out.println("youe are fired!");
		}
		System.out.println("go on...your score is\t"+score);
	}
}

练习:

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		Scanner myscanner = new Scanner(System.in);
		System.out.println("input your age?");
		int age = myscanner.nextInt();
		System.out.println("input the month(like 1,2,3,4...)");
		int month = myscanner.nextInt();
		if(month>=4&&month<=10){
			System.out.println("it's wangji");
			if (age<18&&age>0){
			System.out.println("you are kid,give me 30 yuan");
			}
			else if(age>=18&&age<60){
				System.out.println("you are adult, give me 60 yuan");
			}
			else if(age>=60){
				System.out.println("you are elder, give me 20 yuan");
			}
			else {
				System.out.println("your age is a problem");
			}
		}
		else if ((month<4&&month>=1)||(month>10&&month<=12)){
			System.out.println("it's danji");
			if(age>=18&&age<60){
				System.out.println("you are adult, give me 40 yuan");
			}
			else if(age>=0&&age<18||age>=60){
				System.out.println("your are not adult, give me 20 yuan");
			}
		}
		else{
			System.out.println("your month is a problem");
		}
		System.out.println("go on...");
	}
}

switch分支

学过了,敲下练习吧。

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		Scanner myscanner = new Scanner(System.in);
		System.out.println("input a/b/c/d/e/f/g?");
		char c = myscanner.next().charAt(0);
		switch(c){
		case('a'):{
			System.out.println("feiju1");break;
		}
		case('b'):{
			System.out.println("2");break;
		}
		default:
			System.out.println("=====");
		}
	}
	}

细节讨论:

1.表达式的数据类型,应该和case后的常量类型一致,或者可以自动转换的(比如char和int)

2.switc(表达式)中表达式的返回值必须是byte,short,int,char,enum[],String中的类型。注意enum[]是枚举

3.case子句中必须是常量,或者常量表达式,不能有变量。

4.default是可选的,也可以没有

5.遇到break会退出程序,不遇到会继续执行下面的。

练习:

穿透怎么用啊?好家伙第一次见。

for循环

for(循环变量初始化;循环条件;循环变量迭代){

        循环操作;

}

细节

1.循环条件是返回一个布尔值的表达式

2.for(;条件;)中的初始化和变量迭代是可以写到外面的,但是里面的分号不能省。

3.循环初始值可以有多条初始化语句,但是要求类型一样,并且中间用逗号隔开,迭代也可以有多条

4.输出啥?

i=0,j=0

i=1,j=2

i=2,j=4

练习:

1.

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		int j=0,sum=0;
		for(int i=1;i<=100;i++){
			if(i%9==0){
				System.out.println(i);
				j++;
				sum+=i;
			}
		}
		System.out.println("you zhe me duo ge:"+j+"\nsum is:"+sum);
	}
	}

2.

public class day7{
	public static void main(String[] args) {
		for(int i=0,sum=5;i<6;i++,sum--){
			System.out.println(i+"+"+sum+"="+(i+sum));
			}
		}
	}
	

老师这个更妙啊:

while循环

这个之前用的也不太好..基本语法如下.细节:while先判断再执行。

循环变量初始化;

while(循环条件){

        循环体(语句);

        循环变量迭代;

}

练习:

1.

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		int i = 1;
		while(i<=100){
			if(i%3==0)
				System.out.println(i);
			i++;
		}
		}
	}
	

2.

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		int i = 40;
		while(i<=200){
			System.out.println(i);
			i+=2;
		}
		}
	}
	

吃个饭回来在搞》。。

do...while循环控制

语法:

循环变量初始化;

do{

        循环体;

        循环变量迭代;

}while(循环条件);

说明:先执行,再判断,也就是说一定会至少执行一次。

练习:

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		int i = 1;
		System.out.println("1st question");
		do{
			System.out.println(i++);
		}while(i<=100);
		System.out.println("2nd question");
		int k = 1,sum = 0;
		do{
			sum+=k++;
			System.out.println(sum);
		}while(k<=100);
		System.out.println("3th question");
		int j = 1,count = 0;
		do{
			if(j%5==0&&j%3!=0)
				count++;
			j++;
		}while(j<=200);
		System.out.println(count);
		System.out.println("4th question");
		Scanner myscn = new Scanner(System.in);
		char m;
		do{
			System.out.println("huan qian? y/n");
			m = myscn.next().charAt(0);
		}while(m!='y');
		System.out.println("good boy");
	}
}

	
	

老师:好一个先兵后礼,不管还不还钱,先打了再说是吧?

多重循环练习(重点)

1.

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		Scanner mysc = new Scanner(System.in);
		int s1=0,s2=0;
		for(int i=0;i<3;i++){
			for(int j=0;j<5;j++){
				System.out.println("input score of "+(i+1)+" class's the "+(j+1)+" student");
				s1+=mysc.nextInt();
			}
			System.out.println("the "+(i+1)+"class's average is "+((double)s1/5));
			s2+=s1;
			s1=0;
		}
		System.out.println("all classes's average is "+((double)s2/15));
	}
}

2.

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		Scanner mysc = new Scanner(System.in);
		int s1=0,s2=0;
		for(int i=0;i<3;i++){
			for(int j=0;j<5;j++){
				System.out.println("input score of "+(i+1)+" class's the "+(j+1)+" student");
				if(mysc.nextInt()>=60)
					s1++;
			}
			System.out.println("the "+(i+1)+"class's ok student is "+s1);
			s2+=s1;
			s1=0;
		}
		System.out.println("all classes's ok student is "+s2);
	}
}

3.原来System.out.print();就是不换行啊~~

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		Scanner mysc = new Scanner(System.in);
		int s1=1,s2=1;
		for(int i=1;i<=9;i++){
			for(int j=1;j<=i;j++){
				System.out.print(j+"*"+i+"="+i*j+"\t");
			}
		}
		System.out.println("over");
	}
}

练习:空心金字塔

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		Scanner mysc = new Scanner(System.in);
		System.out.println("input the layer?");
		//x=layer
		int x=mysc.nextInt();
		for(int i=1;i<=x;i++){
			for(int j=0;j<x-i;j++)
				System.out.print(" ");
			for(int j=1;j<=2*i-1;j++){
				if(j==1||j==2*i-1||i==x)
					System.out.print("*");
				else
					System.out.print(" ");
			}
			System.out.print("\n");
		}
	}
}

真累人...讲解有点意思0136_韩顺平Java_空心金字塔_哔哩哔哩_bilibili

尝试打印空心菱形:(成功了,虽然折腾了半天因为少写个等号,然后丢给ai立马跑出来,救命,我真的学得会吗?)

跳转控制语句break

当break语句出现在多层嵌套的语句中时,可以使用标签指明要终止的是哪一层语句块。

例题:

抽卡无保底现状:

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
		int x = 0,sun = 0;
        do{
            x=(int)(Math.random()*100)+1;
            if(x==97)
                {System.out.println("x is " + x + "! get it need " + sun + " times~");
                System.out.println("=============");
                break;}
            else
                System.out.println("x is "+x+" already "+ sun +" times");
            sun++;
        }while(true);

	}
}

练习:

第一题:

结果是6

第二题:

字符串的比较推荐使用:

可以避免空指针。

continue

用于结束本次循环,继续执行下一次循环。和break一样,可以制定label,例:

01010101

如果continue label2的话是013456789循环4次

return

表示跳出所在的方法,在讲解方法的时候,会详细的介绍,如果return写在main里,会退出程序

本例中,return在主方法(main)中,所以退出程序了,只会输出

hello

hello

韩顺平

本章作业

第一题

贴心的询问了你有多少钱,好收保护费。

import java.util.Scanner;
public class day7{
	public static void main(String[] args) {
        Scanner mysc = new Scanner(System.in);
		System.out.println("input your money");
        double money = mysc.nextDouble();
        int count = 0;
        while(true){
            if(money>50000){
                money*=0.95;
                count++;
            }
            else if(money>=1000&&money<=50000){
                money-=1000;
                count++;
            }
            else{
                break;
            }
        }
        System.out.println(count);
	}
}

第二题

import java.util.Scanner;

public class day7 {
    public static void main(String[] args) {
        Scanner mysc = new Scanner(System.in);
        System.out.println("input your integer~");
        int x = mysc.nextInt();
        if (x > 0) {
            System.out.println("zhengshu");
        } else if (x < 0) {
            System.out.println("fushu");
        } else {
            System.out.println("0");
        }
        mysc.close(); 
    }
}

第三题

  1. 如果年份能被4整除且不能被100整除,那么它是闰年。
  2. 如果年份能被400整除,那么它也是闰年。
import java.util.Scanner;

public class day7 {
    public static void main(String[] args) {
        Scanner mysc = new Scanner(System.in);
        System.out.println("input your year~");
        int x = mysc.nextInt();
        if ((x % 4==0&&x % 100 != 0)||(x%400==0)) {
            System.out.println("run nian");
        }  else {
            System.out.println("no run nian");
        }
        mysc.close(); 
    }
}

第四题

import java.util.Scanner;

public class day7 {
    public static void main(String[] args) {
        Scanner mysc = new Scanner(System.in);
        System.out.println("input your integer(abc)~");
        int x = mysc.nextInt();
        int a=0,b=0,c=0;
        a=x/100;
        b=(x-100*a)/10;
        c=(x-100*a-10*b);
        if(x==a*a*a+b*b*b+c*c*c){
            System.out.println("yes!");
        }
        else{
            System.out.println("no");
        }
        mysc.close(); 
    }
}

还有这种写法:

学到惹

第五题

啥都不输出啊

第六题

public class day7 {
    public static void main(String[] args) {
        for(int i=1;i<=100;i++){
            int count = 0;
            while(count<5){
                if(i%5!=0)
                    {System.out.print(i + "\t");
                    count++;}
                i++;
            }
            System.out.println();
        }
    }
}

老师:

不嵌套也可以哈~

第七题

输出的时候可以强制转换一下就不会出数字了!

import java.util.Scanner;

public class day7 {
    public static void main(String[] args) {
        // Scanner mysc = new Scanner(System.in);
        // System.out.println("input your integer(abc)~");
        // int x = mysc.nextInt();
        char c1 = 'a';
        char c2 = 'Z';
        for(int i=0;i<26;i++)
            System.out.print((char)(c1+i));
        System.out.println();
        for(int i=0;i<26;i++)
            System.out.print((char)(c2-i));
        // mysc.close(); 
    }
}

第八题

注意一下sum和flag都是整数,转成double才好算~

import java.util.Scanner;

public class day7 {
    public static void main(String[] args) {
        // Scanner mysc = new Scanner(System.in);
        // System.out.println("input your integer(abc)~");
        // int x = mysc.nextInt();
        double sum = 0;
        int flag=1;
        for(int i=1;i<=100;i++){
            sum+=(double)flag/i;
            flag*=-1;
        }
        System.out.println(sum);
        // mysc.close(); 
    }
}

第九题

import java.util.Scanner;

public class day7 {
    public static void main(String[] args) {
        // Scanner mysc = new Scanner(System.in);
        // System.out.println("input your integer(abc)~");
        // int x = mysc.nextInt();
        int s1 = 0;
        for(int i =1;i<=100;i++){
            for(int j=1;j<=i;j++){
                s1+=j;
            }
        }
        System.out.println(s1);
        // mysc.close(); 
    }
}

这个思路妙啊!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/608627.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Word表格标题间距大修改环绕为无仍无法解决

1.选中表格&#xff0c;右键选择【表格属性】 2.选择【环绕】&#xff0c;此时【定位】可以被启用&#xff08;如下&#xff09;&#xff0c;点击进入窗口 3.修改参数和下面一模一样 注意&#xff1a;【垂直】那里的修改方式是先选段落&#xff0c;后在位置输入0

【linux】主分区,扩展分区,逻辑分区,动态分区,引导分区,标准分区

目录 主分区&#xff0c;扩展分区&#xff0c;逻辑分区 主分区和引导分区 主分区&#xff0c;扩展分区&#xff0c;逻辑分区&#xff08;标准分区&#xff09; 硬盘一般划分为一个“主分区”和“扩展分区”&#xff0c;然后在扩展分区上再分成数个逻辑分区。 磁盘主分区扩展…

html+css-Day1(盒子模型)

一、常用属性 1、字体设置font "line-height" 是 CSS 中的一个属性&#xff0c;用于设置文本行之间的距离&#xff0c;也就是行间距。它影响着段落、行内元素或者任何包含文本的元素的可读性。"line-height" 可以设置为数字、长度单位&#xff08;如 px、e…

现货黄金流程到何种程度?现货黄金在金融产品中的占比是多少?

踏入2024年以来&#xff0c;受美联储降息以及地缘局势紧张的影响&#xff0c;美元受压&#xff0c;避险情绪高涨&#xff0c;众多因素影响下黄金价格出现了强势的上涨&#xff0c;屡创历史新高。在上涨如此强劲的背景下&#xff0c;投资者希望通过黄金投资来实现资产增值。市场…

力扣爆刷第135天之数组五连刷(双指针快慢指针滑动窗口)

力扣爆刷第135天之数组五连刷&#xff08;双指针快慢指针滑动窗口&#xff09; 文章目录 力扣爆刷第135天之数组五连刷&#xff08;双指针快慢指针滑动窗口&#xff09;一、704. 二分查找二、27. 移除元素三、977. 有序数组的平方四、209. 长度最小的子数组五、59. 螺旋矩阵 II…

Adversarial Synthesis of Human Pose From Text # 论文阅读

URL https://arxiv.org/pdf/2005.00340 TD;DR 20 年 5 月来自高校的一篇论文&#xff0c;任务是用 GAN 生成 pose&#xff0c;目前 7 引用。 Model & Method 输入的是描述动作的 text&#xff0c;通过 text encoder&#xff08;本文用的是叫做 fastText 的方法&#…

Kafka应用Demo:指派分区订阅消息消费

环境准备 Kafka环境搭建和生产者样例代码与《Kafka应用Demo&#xff1a;按主题订阅消费消息》相同。 消费者代码样例 public class KafkaConsumerService {private static final Logger LOGGER LoggerFactory.getLogger(KafkaConsumerService.class);private static final S…

word图片水印

一、word中旧水印如何删除 打开word模板&#xff0c;想要删除旧水印&#xff0c;如下图所示操作&#xff0c;但是旧水印删除不掉。 以为上传新水印图片会替换掉旧水印&#xff0c;结果显示了2个水印&#xff0c;要怎么删除呢&#xff1f; 如下截图所示&#xff0c;双击打开页…

vue+element的表格(el-table)排班情况表(2024-05-09)

vueelement的表格&#xff08;el-table&#xff09;排班情况&#xff0c;增删查改等简单功能 代码&#xff1a; <template><!-- 表格 --><div class"sedules"><el-header><el-date-pickerv-model"monthValue2"type"month…

YOLOv8网络结构介绍

将按照YOLOv8目标检测任务、实例分割任务、关键点检测任务以及旋转目标检测任务的顺序来介绍&#xff0c;主要内容也是在目标检测任务中介绍&#xff0c;其他任务也只是Head层不相同。 1.YOLOv8_det网络结构 首先&#xff0c;YOLOv8网络分成了三部分&#xff0c;分别是主干网络…

制鞋5G智能工厂数字孪生可视化平台,推进行业数字化转型

制鞋5G智能工厂数字孪生可视化平台&#xff0c;推进行业数字化转型。随着科技的飞速发展&#xff0c;5G技术与智能制造的结合正成为推动制鞋行业数字化转型的重要力量。制鞋5G智能工厂数字孪生可视化平台&#xff0c;不仅提高了生产效率&#xff0c;还优化了资源配置&#xff0…

【Linux系统编程】31.pthread_detach、线程属性

目录 pthread_detach 参数pthread 返回值 测试代码1 测试结果 pthread_attr_init 参数attr 返回值 pthread_attr_destroy 参数attr 返回值 pthread_attr_setdetachstate 参数attr 参数detachstate 返回值 测试代码2 测试结果 线程使用注意事项 pthread_deta…

SpringCloud:认识微服务

程序员老茶 &#x1f648;作者简介&#xff1a;练习时长两年半的Java up主 &#x1f649;个人主页&#xff1a;程序员老茶 &#x1f64a; P   S : 点赞是免费的&#xff0c;却可以让写博客的作者开心好久好久&#x1f60e; &#x1f4da;系列专栏&#xff1a;Java全栈&#…

NSSCTF | [SWPUCTF 2021 新生赛]easy_sql

打开题目&#xff0c;提示输入一些东西&#xff0c;很显眼的可以看到网站标题为“参数是wllm” 首先单引号判断闭合方式 ?wllm1 报错了&#xff0c;可以判断为单引号闭合。 然后判断字节数&#xff08;注意‘--’后面的空格&#xff09; ?wllm1 order by 3-- 接着输入4就…

[Linux][网络][网络层][IP协议]详细讲解

目录 0.基本概念1.IP协议头格式2.IP分片与组装1.为什么要分片&#xff1f;2.分片后谁来组装&#xff1f;3.这个分片操作传输层知道吗&#xff1f;4.如何识别报文和报文的不同&#xff1f;5.接收端&#xff0c;如何得知报文是独立的还是一个分片&#xff1f;6.如何区别哪些分片是…

UDP和TCP协议比较,TOE技术

如今在某些方面TCP超越UDP的主要原因如下 在硬件层面的TOE(TCP Offload Engine)功能&#xff0c;将越来越多的TCP功能卸载到网卡上。它极大地提升了TCP的性能&#xff0c;使其在高吞吐量场景下的表现更为出色。近年TCP的拥塞控制算法实现了显著进步。这些新算法显著提高了TCP在…

macos安装mysql一直卡在安装成功那个页面选项的解决办法

问题描述&#xff1a; 我安装的是比较新的版本8.0.37&#xff0c;安装过程中一直卡在安装那个选项上&#xff0c;且页面提示安装成功了&#xff0c;但就是死活不往下面的配置选项那一步走。 解决办法&#xff1a; 1.首先清理掉之前的mysql sudo rm -rf /usr/local/mysql2.然…

软件技术主要学什么课程

软件技术专业主要学习的课程和内容有编程语言、数据结构与算法、数据库技术等&#xff0c;以下是上大学网( www.sdaxue.com)整理的软件技术主要学什么课程&#xff0c;供大家参考&#xff01; 编程语言&#xff1a;掌握一种或多种编程语言&#xff0c;如C#、Java、Python、C等&…

Python 2.x与Python 3.x:初学者该如何选择?

自从Python在1994年首次发布以来,已经经历了多个版本的更新和改进。Python 1.x虽然在发展史上具有重要意义,但早已过时,不再用于实际开发。2000年发布的Python 2.x和2008年发布的Python 3.x则成为了Python家族中最常用的两个版本,形成了一个重要的分界线。特别是Python 3.x…

GPU通用计算介绍

谈到 GPU &#xff08;Graphics Processing Unit&#xff0c;图形显示卡&#xff09;大多数人想到的是游戏、图形渲染等这些词汇&#xff0c;图形处理确实是 GPU 的一大应用场景。然而人们也早已关注到它在通用计算上的巨大潜力&#xff0c;并提出了 GPGPU (General-purpose co…