Java if、switch语句,break,case,类型转换、常量、赋值比较、标识符(2)
if语句:
/*
if else 结构 简写格式: 变量 = (条件表达式)?表达式1:表达式2;
三元运算符:
好处:可以简化if else代码。
弊端:因为是一个运算符,所以运算完必须要有一个结果。
*/
class IfDemo
{
public static void main(String[] args)
{
int x = 1; if(x>1)
{
System.out.println("yes");
}
else
{
System.out.println("a");
}
int a = 9,b;
b = (a>1)?100:200; if(a>1)
b = 100;
else
b = 200; int n = 3; if(n>1)
System.out.println("a");
else if(n>2)
System.out.println("b");
else if(n>3)
System.out.println("c");
else
System.out.println("d"); /*
if(n>1)
System.out.println("a");
if(n>2)
System.out.println("b");
if(n>3)
System.out.println("c");
else
System.out.println("d");
*/
System.out.println("over");
}
}
class IfTest
{
public static void main(String[] args)
{
//需求1:根据用户定义的数值不同。打印对应的星期英文。
/*
int num = 1; if(num==1)
System.out.println("monday");
else if(num==2)
System.out.println("tsd");
else
System.out.println("nono");
*/
//需求2:根据用于指定月份,打印该月份所属的季节。
//3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季 int x = 4; if(x==3 || x==4 || x==5)
System.out.println(x+"春季");
else if(x==6 || x==7 || x==8)
System.out.println(x+"夏季");
else if(x==9 || x==10 || x==11)
System.out.println(x+"秋季");
else if(x==12 || x==1 || x==2)
System.out.println(x+"冬季");
else
System.out.println(x+"月份不存在"); if(x>12 || x<1)
System.out.println(x+"月份不存在");
else if(x>=3 && x<=5)
System.out.println(x+"春季");
else if(x>=6 && x<=8)
System.out.println(x+"夏季");
else if(x>=9 && x<=11)
System.out.println(x+"秋季");
else
System.out.println(x+"冬季"); }
}
switch语句:
class SwitchDemo
{
public static void main(String[] args)
{ int x = 3;
/*
switch(x)//byte short int char
{
default:
System.out.println("d");
//break;
case 4:
System.out.println("a");
//break;
case 6:
System.out.println("b");
break;
case 2:
System.out.println("c");
break; }
*/ /*
int a=4,b =2; char ch = '+'; switch(ch)
{
case '-':
System.out.println(a-b);
break;
case '+':
System.out.println(a+b);
break;
case '*':
System.out.println(a*b);
break;
case '/':
System.out.println(a/b);
break;
default:
System.out.println("feifa"); }
*/
System.out.println("Hello World!");
}
}
class SwitchTest
{
public static void main(String[] args)
{ //需求2:根据用于指定月份,打印该月份所属的季节。
//3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季 int x = 4; switch(x)
{
case 3:
case 4:
case 5:
System.out.println(x+"春季");
break; case 6:
case 7:
case 8:
System.out.println(x+"夏季");
break;
case 9:
case 10:
case 11:
System.out.println(x+"秋季");
break;
case 12:
case 1:
case 2:
System.out.println(x+"冬季");
break;
default:
System.out.println("nono"); } /*
if和switch语句很像。
具体什么场景下,应用哪个语句呢?
如果判断的具体数值不多,而是符合byte short int char这四种类型。
虽然两个语句都可以使用,建议使用swtich语句。因为效率稍高。 其他情况:对区间判断,对结果为boolean类型判断,使用if,if的使用范围更广。 */
System.out.println("Hello World!");
}
}
class VarDemo
{
public static void main(String[] args)
{
/*
//定义变量的格式;
//数据类型 变量名 = 初始化值;
// 定义一个int类型变量.取值为4;
int x = 4;
System.out.println(x);
x = 10;
System.out.println(x); //演示其他数据类型。 byte b = 2;//-128~127; //byte b1 = 128; short s = 30000; long l = 4l; float f = 2.3f; double d = 34.56; char ch = '4';
char ch1 = 'a';
char ch2 = '+';
char ch3 = ' '; boolean bo = true;
boolean bo1 = false; int a = 5;
a = a + 6;
*/
/*
什么时候定义变量?
当数据不确定的时候。需要对数据进行存储时。
就定义一个变量来完成存储动作。 */ //类型的转换。 //byte b = 3; //b = b + 2; //System.out.println(b); //System.out.println((char)5); byte b = 3; b = 3 + 4;
//b = b + 4; System.out.println(b); }
}
操作符:
class OperateDemo
{
public static void main(String[] args)
{
//int x = 4270; //x = x /1000 * 1000;
//System.out.println(-1%5);
int a = 3,b;
//a++; //--> a = a+ 1;
b = ++a;
System.out.println("a="+a);
//字符串数据和任何数据使用+都是相连接,最终都会变成字符串。
//System.out.println("5+5"+(5+5));//"5+5=55" /*
转义字符:通过\ 来转变后面字母或者符号的含义。
\n:换行。
\b:退格。相当于backspace。
\r:按下回车键。window系统,回车符是由两个字符来表示\r\n.
\t:制表符。相当于tab键。
*/
System.out.println("hello \t world");
//System.out.println("hello java");
System.out.println("\\hello\\");
char ch = '\'';
char c = 'a';
}
}
class OperateDemo1
{
public static void main(String[] args)
{
int x = 3; //+= -= *= /= %= x+=4;//x = x + 4; short s = 4; //s = s + 5;
s+=5; int a,b,c;
a = b = c = 5; System.out.println(3=4);
}
}
class OperateDemo2
{
public static void main(String[] args)
{
int x = 7; //逻辑运算符用于连接boolean类型的表达式。 //x>3 & x<6 = true & true = true;
/*
true & true = true;
true & false = false;
false & true = false;
false & false = false; & : 只要两边的boolean表达式结果,有一个为false。那么结果就是false。
只有两边都为true,结果为true。
*/
/*
true | true = true;
true | false = true;
false | true = true;
false | false = false;
| : 两边只要有一个为true,结果为true。
只有两边都有false,结果为false。
*/ /*
^ : 异或;就是和|有点不一样。当true ^ true = false;
true ^ true = false;
true ^ false = true;
false ^ true = true;
false ^ false = false;
^ : 两边相同结果是false。
两边不同结果是true。 */ /*
!!true
*/
int a = 2;
//a>3 && a<6;
/*
&和&&的特点:
&:无论左边是true是false。右边都运算。
&&:当左边为false时,右边不运算。 |:两边都参与运算。
||:当左边为true。右边不运算。
*/ System.out.println(false ^ false);
System.out.println(~6); int n = 3,m = 8;
System.out.println("n="+n+",m="+m); //1,通过第三方变量。
/*int temp;
temp = n;
n = m;
m = temp;
*/ //2不用第三方变量。
//11 = 3 + 8; //3 = 11 - 8;
//8 = 11 - 3;
/*
n = n + m;//如果n和m的值非常大,容易超出int范围。
m = n - m;
n = n - m;
*/
n = n ^ m; m = n ^ m;//(n^m)^m; n = n ^ m;//n ^ (n ^ m) System.out.println("n="+n+",m="+m);
}
}
class OperateDemo3
{
public static void main(String[] args)
{ //System.out.println(Integer.toBinaryString(60));
//System.out.println(Integer.toHexString(60)); int num = 26; //获取60的最低4位,通过&15;
int n1 = num & 15; System.out.println(n1>9?(char)(n1-10+'A'):n1); //要获取下一组四位,将60右移4位。
int temp = num >>> 4; //对temp的值进行最低四位的获取。
int n2 = temp & 15;
System.out.println(n2>9?(char)(n2-10+'A'):n2); /*
0-9 'A' 'B' 'C' 'D' 'E' 'F'
65 66 67
10 11 12 13 14 15 12 - 10 = 2 + 'A' = (char)67;
*/ int x = 1,y; y = (x>1)?'a':200;
System.out.println("y="+y);
}
}
Java if、switch语句,break,case,类型转换、常量、赋值比较、标识符(2)的更多相关文章
- Switch语句的case穿透
Switch语句的case穿透 一 switch语句几点说明: 1. case后面只能是常量,不能是变量,而且,多个case后面的值不能出现相同的. 2.case后面表达式可以接受: 基本数据类型,b ...
- java基础:switch语句应用,循环的详细介绍以及使用,附练习案列
1. switch语句 1.1 分支语句switch语句 格式 switch (表达式) { case 1: 语句体1; break; case 2: 语句体2; break; ... default ...
- 提高java编程质量 - (五)switch语句break不能忘以及default不同位置的用法
先看一段代码: public class Test{ public static void main(String[] args){ System.)); } } public static Stri ...
- Java之戳中痛点 - (5)switch语句break不能忘以及default不同位置的用法
先看一段代码: public class Test{ public static void main(String[] args){ System.)); } } public static Stri ...
- Java中switch语句+例题输出当前月份
学习目标: 掌握switch的使用 学习内容: 1.switch语法 <font color=#000000 size=3> switch(表达式) { case 常量1: 语句体1; b ...
- Java:switch语句例子
1.输入一个名次,第1-4名,分别称为冠军.亚军.季军.殿军,5名及5名以上,称为其他名次. import java.util.Scanner; public class switch1 { publ ...
- JAVA之Switch语句
switch case语句是用来判断case后面的表达式是否与switch的表达式一致,符合的话就执行case语句,不符合则break跳出.而default是当没有符合case的条件下执行的(它不用b ...
- java代码switch语句求分数等级
总结:从键盘输入分数----- 如果在0到100内,则输出等级 小于0或者是大于100都不能输出,这里用if-else条件判断. package com.c2; import java.util.Sc ...
- Java控制语句——switch语句
上述if语句的等值判断,可以用switch来代替. 注意每个case后面一般要添加break,表示当前这个case执行完了:防止出现case穿透,即继续执行case,直到遇到break才跳出. 下面例 ...
- JS基础_条件分支语句:switch语句
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
随机推荐
- Jenkins实现自动打包,MAVEN打包,Shell脚本启动
1.点击New任务 2.创建任务,输入项目名 3.输入描述等 4.选择Git或SVN 5.自动,定时打包 6.在Build下配置
- 第四篇:Vue的项目开发
安装Vue的脚手架cli环境 1)官网下载并安装node,附带npm https://nodejs.org/zh-cn/ node环境: 可以解释执行js语言 提供了npm应用商城,可以为node环境 ...
- 操作系统类型&操作系统结构&现代操作系统基本特征
五大类型操作系统 (1). 批处理操作系统 用户脱机使用计算机 用户提交作业之后直到获得结果之前就不再和计算机打交道. 作业提交的方式可以是直接交给计算中心的管理操作员,也可以是通过远程通讯线路提交. ...
- PHP数组创建和遍历(基础)
数组定义PHP数组可以是混合数组 你的数组里面可以有数字也可以有字符串,二维数组不谈一个数组里还有数组跟C有差别定义方式例如 $dd=array(array(1,2,3),array(1,2,3,4) ...
- vue-cli 官方脚手架 eslink配置 恢复serve状态下的打印
使用官方脚手架 打印会提示保存,主要是eslink的配置(在package.json文件夹里面修改) 配置说明 下面是抄的 "eslintConfig": { "root ...
- mybaits入门学习
学习了简单的mybatis的配置 Bean层: 这个都会很简单 一个完整的Bean 需要getter和setter方法还需要一个空的构造方法和一个满的构造方法. Dao层: 创建一个接口就ok了 pa ...
- POJ2002 &&HDU5365 判断给定的点中有多少个不同的正方形
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 17740 Accepted: 6776 Descript ...
- 【LeetCode】搜索旋转排序数组
[问题]假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个 ...
- 2020牛客寒假算法基础集训营5 部分题解(BDEH)
B: 牛牛战队的比赛地(二分做法)题意:二维平面给定n个点,在x轴找一点使得到n个点距离的最大值最小. 思路:我们可以将问题转化为在x轴找到一个圆心,使得该圆包含这n个点且半径最小,这样就变成了最小圆 ...
- 大二暑假第四周总结--开始学习Hadoop基础(三)
简单学习云数据库系统架构(以UMP系统为例) 一.UMP系统概述 低成本和高性能的MySQL云数据库方案 二.UMP系统架构 架构设计遵循以下原则: 保持单一的系统对外入口,并且为系统内部维护单一的资 ...