当前位置: 首页 > news >正文

韩顺平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=layerint 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("*");elseSystem.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;}elseSystem.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(); }
}

这个思路妙啊!

相关文章:

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

p110-p154 控制结构&#xff08;第四章&#xff09; 多分支 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 s…...

性能远超GPT-4!谷歌发布Med-Gemini医疗模型;李飞飞首次创业瞄准空间智能;疫苗巨头联合OpenAl助力AI医疗...

AI for Science 企业动态速览—— * 谷歌 Med-Gemini 医疗 AI 模型性能远超 GPT-4 * 斯坦福李飞飞首次创业瞄准「空间智能」 * 疫苗巨头 Moderna 与 OpenAl 达成合作 * 美国能源部推动 AI 在清洁能源领域的应用 * 美年健康荣获「2024福布斯中国人工智能创新场景应用企业TOP10」…...

中国科技大航海时代,“掘金”一带一路

文&#xff5c;白 鸽 编&#xff5c;王一粟 “这不就是90年代的内地吗&#xff1f;” 在深度考察完沙特市场后&#xff0c;华盛集团联合创始人兼CEO张霆对镜相工作室感慨道。 在张霆看来&#xff0c;沙特落后的基建&#xff08;意味着大量创新空间&#xff09;、刚刚开放…...

ffmpeg7.0 flv支持hdr

ffmpeg7.0 flv支持hdr 自从ffmpeg6.0应用enhance rtmp支持h265/av1的flv格式后&#xff0c;7.0迎来了flv的hdr能力。本文介绍ffmpeg7.0如何支持hdr in flv。 如果对enhance rtmp如何支持h265不了解&#xff0c;推荐详解Enhanced-RTMP支持H.265 1. enhance rtmp关于hdr 文档…...

【教程】极简Python接入免费语音识别API

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你&#xff0c;请不吝给个[点赞、收藏、关注]哦~ 安装库&#xff1a; pip install SpeechRecognition 使用方法&#xff1a; import speech_recognition as srr sr.Recognizer() harvard sr…...

详解typora配置亚马逊云科技Amazon S3图床

欢迎免费试用亚马逊云科技产品&#xff1a;https://mic.anruicloud.com/url/1333 当前有很多不同的博客社区&#xff0c;不同的博客社区使用的编辑器也不尽相同&#xff0c;大概可以分为两种&#xff0c;一种是markdown格式&#xff0c;另外一种是富文本格式。例如华为云开发者…...

Python sqlite3库 实现 数据库基础及应用 输入地点,可输出该地点的爱国主义教育基地名称和批次的查询结果。

目录 【第11次课】实验十数据库基础及应用1-查询 要求: 提示: 运行结果&#xff1a; 【第11次课】实验十数据库基础及应用1-查询 声明&#xff1a;著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。 1.简答题 数据库文件Edu_Base.db&#…...

iOS-SSL固定证书

文章目录 1. SSL简介2. 证书锁定原理1.1 证书锁定1.2 公钥锁定1.3 客户端获取公钥1.4 客户端使用SSL锁定选择1.5 项目支持SSL证书锁定1.6 问题记录1. SSL简介 证书锁定(SSL/TLS Pinning)顾名思义,将服务器提供的SSL/TLS证书内置到移动端开发的APP客户端中,当客户端发起请求…...

docker 开启 tcp 端口

前言&#xff1a;查了很多网上资料 都说要修改daemons,json 完全不管用&#xff0c;而且还导致添加 {“host”:["tcp://0.0.0.0:2375","unix:///var/lib/docker.sock"]} 后&#xff0c;docker restart 失败&#xff0c;浪费了不少时间 &#xff01;&am…...

zookeeper之分布式环境搭建

ZooKeeper的分布式环境搭建是一个涉及多个步骤的过程&#xff0c;主要包括准备工作、安装ZooKeeper、配置集群、启动服务以及验证集群状态。以下是搭建ZooKeeper分布式环境的基本步骤&#xff1a; 1. 准备工作 确保所有节点的系统时间同步。确保所有节点之间网络互通&#xf…...

java设计模式三

工厂模式是一种创建型设计模式&#xff0c;它提供了一个创建对象的接口&#xff0c;但允许子类决定实例化哪一个类。工厂模式有几种变体&#xff0c;包括简单工厂模式、工厂方法模式和抽象工厂模式。下面通过一个简化的案例和对Java标准库中使用工厂模式的源码分析来说明这一模…...

##12 深入了解正则化与超参数调优:提升神经网络性能的关键策略

文章目录 前言1. 正则化技术的重要性1.1 L1和L2正则化1.2 Dropout1.3 批量归一化 2. 超参数调优技术2.1 网格搜索2.2 随机搜索2.3 贝叶斯优化 3. 实践案例3.1 设置实验3.2 训练和测试 4. 结论 前言 在深度学习中&#xff0c;构建一个高性能的模型不仅需要一个好的架构&#xf…...

TODESK怎么查看有人在远程访问

odesk怎么查看有人在远程访问 Todesk作为一款远程桌面控制软件&#xff0c;为用户提供了便捷的远程访问与控制功能。但在享受这种便利的同时&#xff0c;许多用户也关心如何确保自己设备的安全&#xff0c;特别是如何知道是否有人在未经授权的情况下远程访问自己的电脑。本文将…...

【Web漏洞指南】服务器端 XSS(动态 PDF)

【Web漏洞指南】服务器端 XSS&#xff08;动态 PDF&#xff09; 概述流行的 PDF 生成工具常见攻击载荷 概述 如果一个网页使用用户控制的输入创建 PDF&#xff0c;您可以尝试欺骗创建 PDF 的机器人以执行任意的 JS 代码。 因此&#xff0c;如果PDF 创建机器人发现某种HTML标签…...

Qt中的对象树

一. QT对象树的概念 QObject 的构造函数中会传入一个 Parent 父对象指针&#xff0c;children() 函数返回 QObjectList。即每一个 QObject 对象有且仅有一个父对象&#xff0c;但可以有很多个子对象。 那么Qt这样设计的好处是什么呢&#xff1f;很简单&#xff0c;就是为了方…...

QT-day1

1、 自由发挥应用场景&#xff0c;实现登录界面。 要求&#xff1a;尽量每行代码都有注释。 #ifndef MYWIDGET_H #define MYWIDGET_H #include <QWidget> #include <QIcon>//窗口 #include <QLabel>//标签库 #include <QMovie>//动态图片库 #include…...

安装oh-my-zsh(命令行工具)

文章目录 一、安装zsh、git、wget二、安装运行脚本1、curl/wget下载2、手动下载 三、切换主题1、编辑配置文件2、切换主题 四、安装插件1、zsh-syntax-highlighting&#xff08;高亮语法错误&#xff09;2、zsh-autosuggestions&#xff08;自动补全&#xff09; 五、更多优化配…...

解决方案:‘Series‘ object has no attribute ‘xxxx‘

文章目录 一、现象二、解决方案 一、现象 ...... model.fit(X_train, y_train) y_pred model.predict(X_test) recall recall_score(y_test, y_pred) precision precision_score(y_test. y_pred) ......执行语句到**“precision precision_score(y_test. y_pred)”**这里发…...

智慧手术室手麻系统源码,C#手术麻醉临床信息系统源码,符合三级甲等医院评审要求

手麻系统全套源码&#xff0c;C#手术麻醉系统源码&#xff0c;支持二次开发&#xff0c;授权后可商用。 手术麻醉临床信息系统功能符合三级甲等医院评审要求&#xff0c;实现与医院现有信息系统如HIS、LIS、PACS、EMR等系统全面对接&#xff0c;全面覆盖从患者入院&#xff0c;…...

项目公共组件代码

弹出框标题 <Textstyle{{marginTop: 20,marginBottom: 5,fontSize: 20,textAlign: center,fontWeight: bold,color: black,}}>{data.language.CROUPLIST_CLASS_MEMBERS}</Text>可以复用的公共体 import React, {useContext, useEffect, useState} from react; imp…...

Ollama + DeepSeek + 芋道框架 + SearXNG 本地联网搜索完整教程

1. 环境准备与检查 在开始之前,请确保你的环境满足以下条件: 1.1 硬件要求 内存:建议至少8GB可用内存(运行7B模型需要约4-6GB) 硬盘:DeepSeek模型文件约4-5GB空间 CPU/GPU:如有NVIDIA GPU可加速推理(可选) 1.2 软件要求 操作系统:Windows 10/11、macOS、Linux均可 …...

统信UOS 1060e内网离线安装Docker保姆级教程(附CentOS 8 RPM包下载)

统信UOS 1060e内网离线安装Docker全流程实战指南 在企业级IT基础设施中&#xff0c;内网环境下的软件部署一直是运维工程师面临的特殊挑战。当服务器完全隔离于互联网时&#xff0c;每一个依赖包、每一条配置指令都可能成为阻碍系统正常运行的潜在障碍。本文将深入探讨在统信UO…...

道心网络安全学习笔记系列之好靶场的信息收集2

上节课找了一个图片的网址&#xff0c;继续挑战其它靶场&#xff0c;我们看下一题收集十个百度域名&#xff0c;这还不是顺手就来&#xff0c;但是贴吧不行&#xff0c;那还不简单&#xff0c;去访问百度网站&#xff0c;顺便输入一个搜索词&#xff0c;都不用看&#xff0c;前…...

WPF图片处理避坑指南:Image控件Stretch属性的4种模式详解(含效果对比图)

WPF图片处理避坑指南&#xff1a;Image控件Stretch属性的4种模式详解 刚接触WPF开发的工程师们&#xff0c;是否经常遇到图片显示变形、比例失调的困扰&#xff1f;Image控件的Stretch属性看似简单&#xff0c;却藏着不少设计哲学。今天我们就来彻底拆解这个影响图片显示效果的…...

3分钟搞定网易云音乐加密文件:NCMD解密工具终极指南

3分钟搞定网易云音乐加密文件&#xff1a;NCMD解密工具终极指南 【免费下载链接】ncmdump 项目地址: https://gitcode.com/gh_mirrors/ncmd/ncmdump 还在为网易云音乐的NCM加密文件无法在其他播放器播放而烦恼吗&#xff1f;今天我要为你介绍一款简单高效的音频解密神器…...

从零开始理解Transformer的计算复杂度:自注意力与前馈网络的详细对比

从零开始理解Transformer的计算复杂度&#xff1a;自注意力与前馈网络的详细对比 在人工智能领域&#xff0c;Transformer架构已经成为自然语言处理任务的事实标准。但对于初学者来说&#xff0c;理解其内部工作机制&#xff0c;特别是计算复杂度这一关键概念&#xff0c;往往充…...

HunyuanVideo-Foley实战案例:为AI生成视频自动匹配Foley音效工作流

HunyuanVideo-Foley实战案例&#xff1a;为AI生成视频自动匹配Foley音效工作流 1. 项目背景与价值 在视频制作领域&#xff0c;Foley音效&#xff08;环境音、动作音效等&#xff09;的创作往往需要专业录音设备和大量人工处理。HunyuanVideo-Foley创新性地将视频生成与音效生…...

实战指南:基于Cursor与快马平台,从零搭建一个可用的商品管理后台

今天想和大家分享一个实战项目——用Cursor和InsCode(快马)平台从零搭建商品管理后台的全过程。这个项目麻雀虽小五脏俱全&#xff0c;包含了前后端完整链路&#xff0c;特别适合想练手全栈开发的朋友。 项目架构设计 整个系统采用前后端分离模式。后端用Spring Boot搭建RESTfu…...

MMC级联H桥仿真图解析:电压电流双闭环控制策略研究

MMC&#xff0c;级联H桥仿真图&#xff0c;电压电流双闭环。最近在搞MMC&#xff08;模块化多电平换流器&#xff09;的仿真&#xff0c;发现这玩意儿真是电力电子界的乐高——全靠子模块堆叠。特别是级联H桥的结构&#xff0c;玩电压合成比搭积木刺激多了。今天咱们就着电压电…...

盘点那些提高作物耐盐性的方法(一)

本文内容速览&#xff1a;随着全球气候变化加剧和不合理灌溉的持续影响&#xff0c;土壤次生盐渍化问题日益突出&#xff0c;许多地区的耕地盐碱化程度不断加重。传统手段在应对作物的高盐胁迫时逐渐显现出效果上限——部分作物的耐盐性改良已进入平台期&#xff0c;单纯依靠农…...