常量和变量
常量:是程序中不可改变的量
变量:程序运行中,可以修改的量
如何定义一个变量?
数据类型 变量名 = 初值;
例如: int a = 1;
数据类型的分类
1.基本数据类型:
整形
1).字节类型(byte) :在内存当中占1个字节 使用8个2进制位表示 取值范围 -128–127
2).短整型(short):在内存当中占2个字节
3).整形(int):在内存当中占4个字节
4).长整形(long):在内存当中占8个字节小数类型
如果定义一个小数,系统会默认为double类型,在定义float类型时,需要加一个标识(f)
1). float 单精度浮点型 4个字节
2).double 双精度浮点型 8个字节 如果赋值为int类型,系统会自动进行隐式转换(类型提升)
引申:只能是小类型转换为大类型
强制类型转化 使用前面加个括号 在括号中写上要转换的类型字符类型
char 赋值时需要单引号 字符型和整形之间可以相互转化布尔类型
boolean 只有两个值 true / false
2.引用数据类型:类 接口 数组
以后文章中会详细讲述
算术运算符和逻辑运算符
算术运算符:+ - * / % ++ –
% 取模运算符 对自己小的数取余,等于这个数本身,零对任何数取余都是零
++ 自增 ++在前是先进行自增在参与别的运算.++在后是先参与运算在自增
– 自减 同理 (一般在循环中使用,或者计数器,或阶层递归)逻辑运算符
&& 逻辑与 :(判断条件a) && (判断条件b) 如果 a 和 b 同时为真,整个表达式返回为 真 反之为 假
|| 逻辑或 :(判断条件a) || (判断条件b) 如果 a 和 b 只要有一个为真,整个表达式返回为 真 同时为 假 返回假
! 逻辑非: 非真即假 非假既真
逻辑与的短路现象:当条件1为假时,整个表达式为假,条件2不进行判断运算
逻辑或的短路现象:当条件1为真时,整个表达式为真,条件2不进行判断运算
代码举例:
public class Demo03 {
public static void main(String[] args) {
int num1=10;
int num2=5;
boolean rel = (num1<num2) && (++num1>num2); //短路现象 条件2不运行
System.out.println(num1);
System.out.println(rel);
}
}
输出 10 false
- 关系运算符
< > <= >= == != 成立返回 true 反之 flase
流程控制
- 顺序结构:相当于代码按顺序从上到下依次执行
分支结构
1).if语句 :格式1:if(判断条件){ 执行语句 } 如果判断语句成立,就执行执行语句
格式2:if(判断条件){ 执行语句 } else{ 执行语句1}如果判断语句成立,就执行执行语句,否则,执行执行语句1语句
格式3:if(判断条件){ 执行语句 } else if(判断条件1){ 执行语句1 } else{执行语句2}如果判断条件都不成了,就执行else中的执行语句2
代码举例:
public class Demo05 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int s = scanner.nextInt();
if(s >= 90)
{
System.out.println("优秀");
}else if(s >= 70){
System.out.println("良好");
}else if (s >= 60) {
System.out.println("合格");
}else {
System.out.println("不合格");
}
}
}
输入 90
输出 优秀
2).switch 语句 switch(key) { case value: break; …. default: break;} 当所有的case都没有匹配上,系统就会寻找当前的switch语句中有没有default 有就执行,没有就不执行
如果判断为区间使用 if 如果是简单的几个数字,则使用switch 比较方便
代码举例:
public class Demo06 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int s = scanner.nextInt();
switch (s) {
case 4:
System.out.println("现在是冬天");
break; //跳出switch语句
case 3:
System.out.println("现在是秋天");
break;
case 2:
System.out.println("现在是夏天");
break;
case 1:
System.out.println("现在是春天");
break;
default:
System.out.println("输入错误");
break;
}
scanner.close();
}
}
输入 4
输出 现在是冬天
- 循环结构和条件表达式
1).while 循环 while(判断循环条件){ 执行语句} 循环条件成立,执行语句.反之不执行.
2). 条件表达式:(判断条件) ? 值1 : 值2; 成立返回值1,不成立返回值2
代码举例:
public class Demo07 {
public static void main(String[] args) {
int num1 = 10;
int num2 = 15;
int max = (num1 > num2) ? num1 : num2; //如果判断条件成立就返回 num1 如果判断条件不成立就返回num2
System.out.println(max);
}
}
输出 15
例题训练
- 输入月份 打印 该月份对应有多少天(2月28天来计算)
public class Question{
public static void main(String[] arg){
System.out.println("请输入月份");
Scanner sc = new Scanner(System.in);
int month = sc.nextInt();
switch(month){
case 1:
System.out.println("1月份有31天");
break;
case 2:
System.out.println("2月有28天");
break;
case 3:
System.out.println("3月份有31天");
break;
case 4:
System.out.println("4月份有30天");
break;
case 5:
System.out.println("5月份有31天");
break;
case 6:
System.out.println("6月份有30天");
break;
case 7:
System.out.println("7月份有31天");
break;
case 8:
System.out.println("8月份有31天");
break;
case 9:
System.out.println("9月份有30天");
break;
case 10:
System.out.println("10月份有31天");
break;
case 11:
System.out.println("11月份有30天");
break;
case 12:
System.out.println("12月份有31天");
break;
default:
System.out.println("输入有误");
break;
}
}
}
2.学校举行运动会,百米赛跑跑入10秒内的学生有资格进决赛,根据性别分别进入男子组和女子组
public class Question{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的成绩");
String string = sc.nextLine();
double score = Double.parseDouble(string); //强制类型转换
if(score<10) {
System.out.println("请输入你的性别");
String sex = sc.nextLine();
if(sex.equals("男")) {
System.out.println("恭喜你,你进入男子组");
}else if (sex.equals("女")) {
System.out.println("恭喜你,你进入女子组");
}
}else {
System.out.println("你被淘汰了");
}
}
}
3.计算1 + 2 + 3 + 4 +…. + 10 的值
public class Question{
public static void main(String[] arg){
int sum = 0;
int num = 1;
while( num<11) {
sum = sum+num;
num++;
}
System.out.println(sum);
}
}
4.鸡兔同笼问题 35个头 94只脚,计算兔子和鸡分别有多少个
public class Question{
public static void main(String[] arg){
for(int i=0;i<=35;i++) {
if(2*i+4*(35-i)==94) {
System.out.println("鸡有:"+i+"只"+"兔有:"+(35-i)+"只");
}
}
}
}