1.1 常量变量
1.1.1 什么是变量
int a = 3;
1.1.2 常量
常量是有类型
1, 2, int(默认)
3.1, 3.44 double(默认)
long : 3333L ,
float: 3.444F
1.1.3 八个基本类型
byte
short
char
2bytes: 无符号的short
int
long
float
double
boolean
1.1.4 运算符
算数
%
+
复合运算符
+= : a = a+1;
1.1.5 float/double
浮点数
精确运算: BigDecimal
1.1.6 问题
short a = 3;
short b = 4;
short c1 = 3 + 4; //没有问题
short c2 = a + 4;
short c3 = 3 + b;
short c4 = a + b;
杨苏: 前三
陈魏:无
原因: 编译器对整数默认int
a, b -short ---->+以后有可能超出界限 ----提升类型 int
2 输入输出
2.1 输出
System.out.println()
System.out.printf()
JOptionPane.showMessageDialog(null,"你好汉中!!");
2.2 输入
Scanner()
String name = JOptionPane.showInputDialog(null, "请输入您的姓名?");
String age = JOptionPane.showInputDialog(null, "请输入您的年龄?");
String gender = JOptionPane.showInputDialog(null, "请输入您的性别?");
JOptionPane.showMessageDialog(
null, "您的姓名:" + name + "\n您的年龄:" + age + "\n您的性别:" + gender);
int a = JOptionPane.showConfirmDialog(null, "Are You Sure?");
System.out.println("a=" + a);
3 运算符
先结构后代码
分类
算数
自增减
赋值
运算符
+ - * / %
++ --
=
复合运算符
+=, -=,*=,/=,%=
逻辑
位
&&(并且) || (或) & |
& | ^ >> <<
条件(三目)
(表达式) ? 结果 1: 结果2
3.1 ++ --
3.1.1 前置运算
++a;
a = a+1;
b = a;
3.1.2 后置运算
a++:
b = a;
a = a+1
3.2 比较运算符
结果: true/false
if(a == b){
}
!= : 非
3.3 逻辑运算
3.3.1 场景
判断一个字符是否是数字
分析:
并且
>=0
<=9
3.3.2 短路运算
重点:温馨提示
||只需要左边成立即可
&& 左边不成立,结果false
&&: 两边都成立
|| :其中一个成立
&:逻辑与,不参与短路运算
|:无短路运算
3.3.3 举例
闰年:
能被400整除 OR
能被4整除,不能被100整除 2000 AND
year % 400 =0 OR
year % 4==0 && year %100 !=0
3.4 三目运算符
注意:判断奇偶数,判断偶数
Scanner scanner = new Scanner(System.in); //输入器
System.out.print("请输入一个正整数: ");
String s = scanner.nextLine(); //字符串
int a = Integer.parseInt(s);
boolean f = (a % 2 == 0); //true/false
String r = f ? a + "是偶数" : a + "是奇数";
System.out.println(r);
3.5 位运算符
3.5.1 &
位与
3.5.2 |
位或
3.5.3 ^
异或
byte a = 3; // 0000 0011
byte b = 1; // 0000 0001
int c = a & b; //0000 0001
int d = a | b; //0000 0011
//^
System.out.println("c=" + c);
System.out.println("d=" + d);
char ch='中';
char key = '龙';
int x = ch ^key ^key;
System.out.println( (char)(ch ^key));
System.out.println("x=" +(char)x);
3.5.4 移位运算符
>> 右移动 : 除2运算
<< 左移动
4 分支结构
4.1 if
单分支、多分支
4.1.1 基本结构
如果true进入分支
if(true){
}
....
4.1.2 分支
结构: 二选一过程
if(){
}else{
}
4.1.3 多分支
if(){
}else if(){
}else if(){
}
.....
else{
}
4.1.4 案例
4.1.4.1 三个数
三个数排序:
核心: 降级为两个数已排好序。
Scanner scanner = new Scanner(System.in);
System.out.print("请输入a:");
int a = Integer.parseInt(scanner.nextLine());
System.out.print("请输入b:");
int b = Integer.parseInt(scanner.nextLine());
System.out.print("请输入c:");
int c = Integer.parseInt(scanner.nextLine());
//核心思想: a,b排序
//a小 b 大 a , b
if (a > b) { //a是小的,a,b交换
int temp = a;
a = b;
b = temp;
}
if (c < a) {
System.out.println(c + "," + a + "," + b);
}else if(c>b){
System.out.println(a + "," + b + "," + c);
}else{
System.out.println(a + "," + c + "," + b);
}
4.1.4.2 猜数
对方猜数[1~99]之间 目标数: 69
第一次猜: 79
[x] 数在 [1~79]
第二次: 59
[x] 数在[ 59~79]
4.1.4.3 随机数
Random ran = new Random();
int r = ran.nextInt(随机数范围);// 99 [0,99)
4.2 switch
多分支
package com.ffyc.ifx;
/**
* Switch结构
* if ...else if ...else if ...else
*/
public class SwitchDemo {
public static void main(String[] args) {
//<60 F <6
// 60-70 C 6
//70~80 B 7
//80~99 A 8 9 89/10 = 8 98/10 =9
//100 Top 10 100/10 ==10
int score = 111;
if (score > 100 || score < 0) {
System.out.println("成绩不合法.....");
} else {
int s = score / 10;
switch (s) {
case 10:
System.out.println("top");
break;
case 9:
case 8:
System.out.println("A");
break;
case 7:
System.out.println("B");
break;
case 6:
System.out.println("C");
break;
default:
System.out.println("F");
break;
}
}
}
}
变量,运算符
最新推荐文章于 2024-07-13 13:55:40 发布