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

Java基本语句-分支语句

Day05
如何在API字典中寻找自己想要的Scanner类型
1.点击搜索 输入Scanner
2.字典中回显示各种类型的获取方式:
nextByte()、nextShort()、nextInt()、nextLong()、nextdouble()、nextFloat()、next()多种引用使用。

3.调用Scanner类的相关方法,来获取指定类型的变量
//1.导包:improt java.util.Scanner;
import java.util.Scanner;class ScannerTest{public static void main(String[] args){//2.Scanner的实例化 Scanner scan = new Scanner(System.in);//调用Scanner类的相关方法System.out.println("请输入你的姓名:");String name = scan.next();//字符串类型Syste.out.println(name);Syste.out.println("请输入你的芳龄: ");int age = scan.nextInt();Syste.out.println(age);Syste.out.println("请输入你的体重: ");double weight = scan.nextDouble();Syste.out.println(weight); Syste.out.println("你是否相中我了呢?(true/false)");boolean isLove = scan.nextBoolean();Syste.out.println(isLove);//对于char型的获取, Scanner没有提供相关的方法。只能获取一个字符串Syste.out.println("请输入你的性别: (男/女)");String gender = scan.next()//"男"char genderChar = gender.charAt(0);//在API字典中找:String class,点击找char的方法,然后索引index(0)为零。}
}

以上内容:展示API调用和使用char类型索引使用。
注意:
需要根据相应的方法,来输入指定类型的值。如果输入不同类型会报错。
练习题:
1.

import java.util.Scanner;
class IfTest {public static void main(String[] args) {Scanner scan = new Scanner(System.in);System.out.println("请输入岳小鹏期末成绩: (0-100)");int score = scan.nextInt();if(score == 100){System.out.println("奖励一辆BMW"); }else if(score > 80 && score <= 99){System.out.println("奖励一台iphone xs max");}else if(score >= 60 && score <= 80){System.out.println("奖励一个 iPad");}}
}

分支结构的练习中发现的问题和特点;
1.代码中else 结构是可选的。
2.条件语句是代码的核心,定义条件需求一定要精确。
(1.如果多个条件之间是"互斥"关系(或没有交集的关系),哪个判断和执行语句声明在上面就执行哪个。
2.如果多个条件之间有交集的关系,需要根据实际情况,考虑清楚应该将哪个结构声明在上面
3.如果多个条件式之间有包含的关系,通常情况下,需要将范围小的声明在范围大的上面,否则,范围小的就没有机会执行了。)
3.if-else可以一层多嵌套使用。

import java.util.Scanner;
class IfTest2{public static void main(String[] args){Scanner scan = new Scanner(System.in);System.out.println("请输入第一个整数:");int num1 = scanner.nextInt();System.out.println("请输入第二个整数:");int num2 = scanner.nextInt();System.out.println("请输入第三个整数:");int num3 = scanner.nextInt();if(num1 >= num2){if(num3 >= num1){System.out.println(num2 + "," + num1 + "," + num3);}else if(num3 <= num1){System.out.println(num3 + "," + num2 + "," + num1);}else{System.out.println(num2 + "," + num3 + "," + num1);}else{if(num3 >= num2){System.out.println(num1 + "," + num2 + "," + num3);}else if(num <= num1){System.out.println(num3 + "," + num1 + "," + num2);}else {System.out.println(num1 + "," + num3 + "," + num2);}}   }} 

这道题:排列三个数的大小,利用分支语句来对Scanner提供的数据来进行从大到小的排序。
目的:将Scanner打印和分支语句融合使用。

class IfExer {public static void main(String[] args){int x = 4;int y = 1;if(x > 2)if(y > 2)System.out.println(x + y);//System.out.println("atguigu");} elseSystem.out.println("s is" + x);}
}

是对大括号的使用。

 boolean b = ture;//如果写成if(b=false)能编译通过吗?如果能,结果是?if(b == false)System.out.println("a");
else if(b)System.out.println("b");
else if(!b)System.out.println("c");
elseSystem.out.println("d");

是对于分支语句的条件判断的理解。

3.获取狗的年龄

class IfExer {public static void main(String[] args){int dogAge = 6;if(dogAge >=0 && dogAge <= 2){System.out.println("相当于人的年龄: " + dogAge * 10.5);    } else if(dogAge > 2){System.out.println("相当于人的年龄: " + (2 * 10.5 + (dogAge - 2) * 4));} else {System.out.println("狗还没有出生");}}
}

是对分支语句和打印的使用。

int value = (int)(Math.random() * 90 + 10);// [0.0,1.0) --> [0.0,100.0]]
//公式:[a,b] : (int)(Math.random() * (b - a + 1) + a)

如何使用Math.random();
然后利用强制类型转换来完成随机数使用。

class IfExer{public static void main(String[] args) {Scanner scan = new Scanner(System.in);System.out.println("请输入你的身高:(cm)");int height = scan.nextInt();System.out.println("请输入你的财富: (千万)");double wealth = scan.nextDouble();/*方式一:System.out.println("请输入你是否帅: (true/false)");boolean isHandsome = scan.nextBoolean();if(height >= 180 && wealth >= 1 && isHandsome){System.out.println("我一定要嫁给他!!!");}else if(hegiht >= 180 || wealth >= 1 || isHandsome){System.out.println("嫁吧,比上不足,比下有余。");}else{System.out.println("不嫁!");}*///方式二System.out.println("请输入你是否帅: (是/否)");String isHandsome = scan.next();if(height >= 180 && wealth >= 1 && isHandsome.equals("是")){System.out.println("我一定要嫁给他!!!");}else if(hegiht >= 180 || wealth >= 1 || isHandsom.equals("是")){System.out.println("嫁吧,比上不足,比下有余。");}else{System.out.println("不嫁!");}}
}

逻辑和Scanner的使用
如何用汉字输入是否:先答应字符串、然后将equals==号放入选择中使用。
switch-case

switch(表达式){
case 常量1:执行语句1;//break;
case 常量2:执行语句2;//break;
...
default:执行语句n;//break;
}
int age = 10;
switch(age){case age > 18:System.out.println("成年了");break;default:System.out.println("未成年");
}

switch结构中的表达式,只能是如下的6种数据类型之一:
byte、short、char、int、枚举类型(JDK5.0新增)、String类型(JDK7.0新增)
如果switch case的多条件相同,我们可以合并使用。
break在switch case当中是可选的。
对月的值进行查询。
可以在代码中加入if else语句使用。
如何选择swith case和if-else使用
说明:
1.凡是可以使用Switch-case的结构,都可以转换为if-else.反之,不成立。
2.当我们写分支结构时,当发现既可以使用switch-case,(同时,switch中表达式的取值情况不太多),又可以使用if-else时,我们优先选择使用switch-case。原因:switch-case执行效率稍高。
循环结构:
在某些条件满足的情况下,反复执行特定代码的功能
循环语句分类:
for 循环
初始化部分:
循环条件部分:
循环体部分:
迭代部分:
while 循环
do-while 循环
各位今天内容有些杂乱,有好多地方不太理解,明天我会继续进行修改更新。

http://www.gsyq.cn/news/7472.html

相关文章:

  • HyperWorks许可配置
  • AI --- LLM 之 模型大比拼
  • Java入门知识
  • 12 路低延迟推流!米尔 RK3576 赋能智能安防 360 环视
  • Xilinx DDR3仿真 DBG
  • 对马岛之魂
  • Ubuntu 22 下 DolphinScheduler 3.x 伪集群部署实录
  • 软件工程个人项目
  • P2216 [HAOI2007] 理想的正方形
  • 2-sat板子
  • Node.js 中使用 .env 文件管理环境变量
  • pythonjs逆向 破解滑动验证码 - hello-*
  • Bun:不仅是新的JavaScript运行时,并且重塑了JavaScript工具链
  • AI Agent 与 MCP 核心解析与企业级应用指南
  • P3934 [Ynoi Easy Round 2016] 炸脖龙 I 做题记录
  • 正确输入连字号、连接号、破折号和负号
  • python基础-元组
  • python基础篇-list(列表)
  • vscode使用powershell中文乱码
  • Untitled
  • 完整教程:论园区电气安全管理系统的重要性
  • 没搞懂的package.json
  • 你应该考虑放弃 react-router 的数据路由模式,改而使用更加适合国内版本的封装版本(包含完整可 CV 的模版)
  • 基于CSU8RP1186芯片的握力器解决方案
  • 深入解析:C++ 内存管理:从底层原理到实战应用
  • sass踩坑:@import导致前端项目打包体积膨胀
  • 深入解析:Java 设计模式之桥接模式(Bridge Pattern)
  • 【AP出版】第四届数理统计与经济分析国际学术会议 (MSEA 2025)
  • 数据结构 Trick 之:区间子区间计数
  • mapstruct.Mapper|Mapping详解