Java中各种判断

判断主要通过表达式或者boolean值来判断
比如 大于(>),小于(<),等于(==),大于等于(>=),小于等于(<=),
不等于(!=)
要注意 单个 = 表示赋值 不是数学意义上的等于
但也可以通过输入的内容是否符合要求来判断
写1 其实也表示为真 对应的0 为假

Scanner 的判断

scanner中的判断主要体现在读取即将通过键盘输入的内容进行判断具体如下
这里直接写main方法里面的主要代码,main在上一张 起步中有介绍

Scanner reader = new Scanner(System.in);  //要用先创建System.out.print("input the first number:");
int x = reader.nextInt();      //判断读入的整数
System.out.print("input the second number:");
int y = reader.nextInt();      //同上int max = x>y?x:y;          //三元表达式  一会儿介绍System.out.println("max = "+max);     //输出最大值

对应的三个数的判断

Scanner reader = new Scanner(System.in);System.out.print("input the first number:");  //输入三个整数int x = reader.nextInt();      //这里输入的一定为整数的形式,否则报错System.out.print("input the second number:");int y = reader.nextInt();System.out.print("input the third number:");int z = reader.nextInt();int maxx; // 中间变量int max = (maxx = x > y ? x : y) > z ? x : z;System.out.println("MAX = " + max);

三元表达式判断

Scanner reader = new Scanner(System.in);System.out.print("input the first number:");int x = reader.nextInt();String string = (x%2==0)?"偶数":"奇数";  //三元首先判断?前的表达式是否为真//如果为真,取?后面第一值 否则取第二个值System.out.println(string);

If 判断

Scanner reader = new Scanner(System.in);System.out.println("Input your age:");int age = reader.nextInt();if(age>18)   {   //if里面的值必须为真或假 若为真。执行后面括号里的内容//若为假  则不执行里面的内容System.out.println("OK");}System.out.println("WELcome");
if(true)
System.out.println("2236554");
System.out.println("123");//如果没有带括号那么 即使执行也执行后面的第一句 其他不执行

if-else

     Scanner reader = new Scanner(System.in);System.out.print("input: ")  ;int i = reader.nextInt();//若为真 执行if后面括号里的,否则执行else里面的语句if(i == 121 ) {System.out.println("123");}else {System.out.println("qwe");}

if --else if --else

Scanner reader = new Scanner(System.in);System.out.println("2361+46553: s");int x = reader.nextInt();if (x == 000) {              //相当于多层依次判断 满足了就执行System.out.println("get  000");}else if (x == 001) {      //但可以确定的是,只会执行其中的一种情况System.out.println("get  001");}else if(x ==002){System.out.println("get  002");}else if (x == 003) {System.out.println("get  003");}else {System.out.println("none  in  there");}

switch

寻找符合的那个,然后执行

     Scanner reader = new Scanner(System.in);System.out.println("Input the number:");int day = reader.nextInt();     switch (day) {    //将输入的内容和case中对比 执行对应语句case 1:System.out.println("monday\n");break;     //这里的break表示执行完了就跳出switch 不对比后面case 2:System.out.println("tuesday\n");break;      //若不写break 当判断通过后,会继续对比走后面的case 3:System.err.println("wedsday\n");break;case 4:System.out.println("thesday\n");break;case 5:System.out.println("friday\n");break;case 6:System.out.println("startday\n");break;case 7:System.out.println("sunday\n");break;default:    //当没有匹配时默认执行default里的内容System.out.println("input error\n");break;}

来看看对不写break的贯穿利用

Scanner reader = new Scanner(System.in);
int year;                               //初始化
int mon;System.out.print("Input the year number:");           //年月输入
year = reader.nextInt();
System.out.print("Input the month number:");
mon = reader.nextInt();switch (mon) {                              //Switch挑选case 2:                                       //二月分辨if (year%4 == 0) {System.out.println("In this year,This month has 29 days");}else {System.out.println("In this year,This month has 28 days");}break;case 4:                                 //三十天的月份case 6:case 9:case 11:System.out.println("In this year,this month has 30 days");break;case 1:                                 //三十一天的月份case 3:case 5:case 7:case 8:case 10:case 12:System.out.println("In this year, this month has 31 days");break;default:                    //错误输入System.out.println("Error");break;}

最后看一下 if 和 switch 对判断范围的实例
判断成绩等级的例子

 int score;                             //初始化Scanner reader = new Scanner(System.in);  //定义初始化System.out.println("Input your score:");score = reader.nextInt();                      //输入确定变量if (score>=90) {                                //jiang'ji'pan'dSystem.out.println("优秀");}else if (score >=80) {System.out.println("良好");}else if(score >= 60){System.out.println("及格");}else{System.out.println("不及格");}//对比Scanner reader = new Scanner(System.in);System.out.println("Input your score:");int score = reader.nextInt();switch(score/10) {case 10:case 9:System.out.println("YX");break;case 8:System.out.println("LH");break;case 7:case 6:System.out.println("JG");break;case 0:case 1:case 2:case 3:case 4:case 5:System.out.println("BHG");break;default:System.out.println("Illegal input");break;}

个人所得税 和 工资的例子

//   涨工资Scanner reader = new Scanner(System.in);    //变量初始化与定义int year;double basic;System.out.print("输入基础工资:");       //键盘录入basic = reader.nextDouble();System.out.print("输入工作年份:");year = reader.nextInt();//if else  判断过程if (year >= 10 && year < 15) {System.out.println("您目前工作了" + year + "年,基本工资为 " + basic + "元, 应涨工资 5000元,涨后工资" + (basic + 5000) + "元");} else if (year >= 5 && year < 10) {System.out.println("您目前工作了" + year + "年,基本工资为 " + basic + "元, 应涨工资 2500元,涨后工资" + (basic + 2500) + "元");} else if (year >= 3 && year < 5) {System.out.println("您目前工作了" + year + "年,基本工资为 " + basic + "元, 应涨工资 2500元,涨后工资" + (basic + 1000) + "元");} else if (year >= 1 && year < 3) {System.out.println("您目前工作了" + year + "年,基本工资为 " + basic + "元, 应涨工资 2500元,涨后工资" + (basic + 500) + "元");} else if (year >= 0 && year < 1) {System.out.println("您目前工作了" + year + "年,基本工资为 " + basic + "元, 应涨工资 2500元,涨后工资" + (basic + 200) + "元");}else {System.out.println("输入有误,请重新来过。");}//switch (year) {      //switch环境case 10:case 11:case 12:case 13:case 14:case 15:System.out.println("您目前工作了" + year + "年,基本工资为 " + basic + "元, 应涨工资 5000元,涨后工资" + (basic + 5000) + "元");break;case 9:case 8:case 7:case 6:case 5:System.out.println("您目前工作了" + year + "年,基本工资为 " + basic + "元, 应涨工资 2500元,涨后工资" + (basic + 2500) + "元");break;case 4:case 3:System.out.println("您目前工作了" + year + "年,基本工资为 " + basic + "元, 应涨工资 1000元,涨后工资" + (basic + 1000) + "元");break;case 2:case 1:System.out.println("您目前工作了" + year + "年,基本工资为 " + basic + "元, 应涨工资 500元,涨后工资" + (basic + 500) + "元");break;case 0:System.out.println("您目前工作了" + year + "年,基本工资为 " + basic + "元, 应涨工资 200元,涨后工资" + (basic + 200) + "元");break;default:System.out.println("输入有误,请重新来过。");break;}
//
//个人所得税  数据可以自己修改
double basic;double YN;Scanner reader = new Scanner(System.in);System.out.println("你的基本工资为:");basic = reader.nextDouble();if (basic <= 7662) {YN = basic - basic * 0.2 - 5000;} else {YN = basic - 7762 * 0.2 - 5000;}if (YN <= 0) {System.out.println("应交个人所得税:" + 0);} else if (YN <= 1500) {System.out.println("应交个人所得税:" + (YN * 0.03 - 0));} else if (YN <= 4500 && YN > 1500) {System.out.println("应交个人所得税:" + (YN * 0.1 - 105));} else if (YN <= 9000 && YN > 4500) {System.out.println("应交个人所得税:" + (YN * 0.2 - 555));} else if (YN <= 35000 && YN > 9000) {System.out.println("应交个人所得税:" + (YN * 0.25 - 1005));} else if (YN <= 55000 && YN > 35000) {System.out.println("应交个人所得税:" + (YN * 0.3 - 2700));} else if (YN <= 80000 && YN > 55000) {System.out.println("应交个人所得税:" + (YN * 0.35 - 5505));} else if (YN > 80000) {System.out.println("应交个人所得税:" + (YN * 0.45 - 13505));}

Java专题 基础篇--判断(三元表达式,switch等) +个税计算案例相关推荐

  1. JAVA多线程基础篇-关键字synchronized

    1.概述 syncronized是JAVA多线程开发中一个重要的知识点,涉及到多线程开发,多多少少都使用过.那么syncronized底层是如何实现的?为什么加了它就能实现资源串行访问?本文将基于上述 ...

  2. Java面试基础篇之集合

    文章目录 你知道的集合都有哪些? 哪些集合是线程安全的? Collection 集合类和数组有什么不同? Collection和Collections有什么区别? 如何确保一个集合不能被修改? Lis ...

  3. java实现linkstring,【JAVA SE基础篇】32.String类入门

    [JAVA SE基础篇]32.String类入门 1.字符串 1.String类又称作不可变字符序列 2.String位于java.lang包中,java程序默认导入java.lang包下所有的类 3 ...

  4. java 中间容器 表格_【JAVA SE基础篇】45.迭代器、Collections工具类以及使用容器存储表格...

    本文将要为您介绍的是[JAVA SE基础篇]45.迭代器.Collections工具类以及使用容器存储表格,具体完成步骤: 1.迭代器 迭代器为我们提供了统一遍历容器(List/Map/Set)的方式 ...

  5. java预科基础篇2021.2.3学习记录

    java预科基础篇2021.2.3学习记录 初识博客 本以为老师会讲是在微博上写博客做记录,没想到会是很多程序员专用的博客 博客为音译,正确翻译结果为网络日记,英文为bog 较为专业的程序员用博客为: ...

  6. java se运算符优先级,【JAVA SE基础篇】10.运算符优先级与类型转换

    [JAVA SE基础篇]10.运算符优先级与类型转换 1.运算符的优先级 运算符的优先级在考试中会考,了解即可,多用就会熟能生巧 实际使用过程中建议用小括号来分优先级 关键就是:逻辑非>逻辑与& ...

  7. Java - ip2region - 基础篇(你知道ip2region吗?)

    Java - ip2region - 基础篇(你知道ip2region吗?) 本篇主要介绍 ip2region, ip2region 支持很多客户端,本次主要以Java来介绍 在进行系统开发时,我们一 ...

  8. JAVA红石_【Mc我的世界红石研究日记】第四期:红石基础元件(四)——JAVA版基础篇...

    Hello,大家好,欢迎来到Mc元气工作室!本期给大家带来Mc我的世界红石研究日记·第四期!版本:JAVA1.14.3. 第三期答题互动答案 以下哪一个选项被红石比较器检测出的红石信号与其他三项不同? ...

  9. Java+Selenium3基础篇5-第一个自动化测试脚本

    前面几篇我们介绍了环境搭建和三大浏览器的启动方法,这篇文章我们介绍第一个自动化测试脚本.我的个人经验是,自动化脚本编写比较容易,最大的困难去如何去写测试断言.自动化测试,最重要的还是落在测试上面,而不 ...

最新文章

  1. Compass(更新中。。。)
  2. ie8开发人员工具无法使用,按f12任务栏里出现任务,但是窗体不弹出
  3. Spring Security 实战:登录成功后返回 JWT Token
  4. IDEA中记一次BuildProject不好使的解决过程
  5. jQuery Ajax 实例
  6. 【STC15库函数上手笔记】7、PCA与PWM
  7. [Leedcode][JAVA][第990题][等式方程的可满足性][并查集]
  8. node.js路由控制
  9. java日历事件处理_日历表的事件处理和管理(刘静)
  10. linux memcpy 效率,memcpy每秒字节速率
  11. html自动生成工具_「写论文神器」一个好用的论文自动生成工具,内含30w 文献数据...
  12. 漫画:什么是 “智猪博弈” ?
  13. httplistener java_Java监听器listener的介绍
  14. C++求复数的角度_高考数学一轮复习33,复数,常见类型及解题策略
  15. php安装调式redis扩展,下载安装thinkphp5.0,调试Redis是否可以正常使用
  16. putty传文件至服务器,putty传输文件
  17. 如何在Ubuntu上使用Canonical的Livepatch服务
  18. 2017中国云计算技术大会将于5月18-19日在京召开
  19. vue导出excel加一个进度条_vue纯前端导出excel表格
  20. 银河麒麟V10业务系统适配记录 处理器:FT2000+ 中国信创服务社区

热门文章

  1. 易语言lsp劫持_[原创]不用导出任何函数的DLL劫持注入,完美!
  2. Bugku:杂项 小美的秘密part2
  3. 2021年【教师资格】流程全过程
  4. LNA设计学习心得记录----MOS管的选取
  5. 35、sparkSQL及DataFrame
  6. Sack Panic漏洞TCP MSS机制(二)(together with myself)
  7. 一年多前的Linux笔记,仅以此文纪念当时的年少无知
  8. 如何安装xampp(linux版)
  9. 2020美赛D题原文及翻译
  10. 【翻译】开发者体验门户 后台 如何解决Spotify的复杂性问题