3.1 运算符
3.1.1 概述
算术运算符 | + - * / | 基本运算 |
% | 取余数,求模,算整除 | |
++ -- | 自增 自减 | |
比较运算符 | == | 相等比较 |
!= | 不等比较 | |
逻辑运算符 | && & | 逻辑与(短路与),两边同为真结果才为真 |
|| | | 逻辑或(短路或),两边只要有一个真结果就是真 | |
+ | 字符串连接 | |
! | 非,非真是假,非假是真 | |
三元运算符 | ?: | 三项运算 1?2:3 1是真取2,1是假取3 |
赋值运算符 | = | 赋值运算 |
+= -= *= /= | 复合的赋值运算 a+=2;//a=a+2 |
3.1.2 练习1:平年闰年
输入年号,判断是否是闰年。两个条件:
1、能被4整除,并且不能被100整除
2、或者能被400整除
package day0203_平年闰年;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
System.out.println("年号:");
int y = new Scanner(System.in).nextInt();
String r="平年";
if(y%4==0){
if(y%100!=0){
r="闰年";
}
}
if(y%400==0){
r="闰年";
}
或者
if((y%4==0&&y%100!=0)||y%400==0){
r="闰年";
}
System.out.println(y+"年是"+r);
}
}
3.1.3 练习2:自增自减
int a = 1;
System.out.println(a++);//先取值再自增
int b=1;
System.out.println(++b);//先自增再取值
int c=1;
int d = c++;
int e = ++c;
System.out.println(d);
System.out.println(e);
3.1.5 练习4:求三个数的最大值
package day0203_平年闰年;
import java.util.Scanner;
public class Test1_三个数的最大值 {
public static void main(String[] args) {
System.out.println("整数a:");
int a = new Scanner(System.in).nextInt();
System.out.println("整数b:");
int b = new Scanner(System.in).nextInt();
System.out.println("整数c:");
int c = new Scanner(System.in).nextInt();
int max = a>b?a:b;
max=max>c?max:c;
或者
int max = a>b?(a>c?a:c):(b>c?b:c);
System.out.println(max);
}
}
3.2 分支结构1:if
3.2.1 概述
顺序结构的程序虽然能解决计算、输出等问题,但不能做判断再选择。对于要先做判断再选择的问题就要使用分支结构。
3.2.2 形式
单分支:
if(判断条件){
代码。。。
}
多分支:
if(判断条件){
代码1。。。
}else{
代码2。。。
}
嵌套分支:
if(判断条件1){
代码1。。。
}else if(条件2){
代码2。。。
} else if(判断条件3){
代码3。。。
}else{
代码4。。。
}
接收用户输入的原价。满1000打9折。满2000打8折。满5000打5折。
package day999;
import java.util.Scanner;
public class ttt {
public static void main(String[] args) {
System.out.println("输入总原价");
double price = new Scanner(System.in).nextDouble();
double now = f(price);
System.out.println(now);
}
public static double f(double p){
if(p>5000){
p=p*0.5;
}else
if(p>2000){
p=p*0.8;
}else if(p>1000){
p=p*0.9;
}
return p;
}
}
3.2.4 练习2:统计学员得分
90分以上 优秀
80~89 良好
70~79 中等
60~69 及格
60分以下 不及格
package game;
import java.util.Scanner;
public class aa {
public static void main(String[] args) {
double score = new Scanner(System.in).nextDouble();
if (score >= 100 || score <= 0) {
System.out.println("请输入0~100以内的值");
}
if (score > 90 && score <= 100) {
System.out.println("优秀");
} else if (score >= 80 && score <= 90) {
System.out.println("良好");
} else if (score >= 70 && score <= 79) {
System.out.println("中等");
} else if (score >= 60 && score <= 69) {
System.out.println("及格");
}else if (score < 60) {
System.out.println("不及格");
}
}
}
3.3 [了解]分支结构2:switch
3.3.1 概述
当一个case成立,从这个case向后穿透所有case,包括default,直到程序结束或者遇到break程序才结束。
3.3.2 形式
switch(expr1)中,expr1是一个整数表达式, 整数表达式可以是int基本类型或Integer包装类型,由于byte,short,char都可以隐含转换为int,所以也支持。
注意: jdk1.7以后新增 String
switch(变量或者表达式){
case 1:
case 2:
case 3:
case 4:
default:
}
package day003;
import java.util.Scanner;
public class Test1_数字匹配 {
public static void main(String[] args) {
int i=3;
switch (i) {
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
case 3:
System.out.println("3");
// break;
default:
System.out.println("default");
break;
}
}
}
3.4 循环结构1:for
3.4.1 概述
循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构。
它由循环体中的条件,判断继续执行某个功能还是退出循环。
根据判断条件,循环结构又可细分为先判断后执行的循环结构和先执行后判断的循环结构。
for(开始条件;循环条件;更改条件){
循环体代码…
}
package day999;
public class ttttt {
public static void main(String[] args) {
f1();
}
public static void f1(){
for (int i = 0; i <= 10; i++) {
System.out.print(i);
}
}
}
package day999;
public class ttttt {
public static void main(String[] args) {
f2();
}
public static void f2(){
for(int i=10;i>=0;i--){
System.out.print(i);
}
}
}
package day999;
public class ttttt {
public static void main(String[] args) {
f();
}
public static void f(){
for(int i=8;i<=10000;i=i*10+8){
System.out.println(i);
}
}
}
3.5 拓展
3.5.1 &和&&的区别
当一个&表达式在求值的时候,两个操作数都会被求值,&&更像 是一个操作符的快捷方式。当一个&&表达式求值的时候,先计算第一个操作数,如果它返回true才会计算第二个操作数。如果第一个操作数 取值为fale,第二个操作数就不会被求值
byte a = 1;
// a=(byte) (a+4);//右侧int,左侧byte,大转小,强转。
// a=(byte) (a+4);//右侧int,左侧byte,大转小,强转。
a+=4;//会自动完成数据类型的转换
//注意:a=a+4和a+=4是有区别的!!
System.out.println(a);
3.5.3 求【0,100】中,奇数的个数
3.5.4 求【0,100】中,偶数的和
package cn.tedu.basic;
//测试switch
public class Test1_Switch {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=2;
switch(i) {//整型表达式 byte short int char string
case 1: System.out.println(1);
break;//结束switch结构
case '1': System.out.println('1');
break;
//找到匹配的case,并向后穿透所有的case和dufault
case 2: System.out.println(2);
break;
case 3: System.out.println(3);
break;
case 4: System.out.println(4);
break;
case 5: System.out.println(5);
break;
default:System.out.println(100);
}
}
}
package cn.tedu.fordemo;
//测试for
public class Test2_For {
public static void main(String[] args) {
// 输出1000次你的名字
// for(int a=1;a<=1000;a++) {ctrl+/备注
// System.out.println("邢业部");
// }
// for(int a1=0;a1<=10;a1++) {
// System.out.println(a1);
// }
// for(int i=10;i>=0;i--) {
// System.out.println(i);
// }
// for(int b=8;b<=8888;b=b*10+8) {
// System.out.println(b);
// }
//1到100的偶数,求和
int sum=0;
for(int i=1;i<=100;i++) {
if(i%2==0) {
sum=sum+i;
}
}
System.out.println("100以内的偶数和是"+sum);
}
}
package cn.tedu.fordemo;
//嵌套for循环
public class Test3_For2 {
public static void main(String[] args) {
// for(int i=1;i<=3;i++) {
// System.out.println("i="+i);
// for(int j=1;j<=5;j++) {
// System.out.println("j="+j);
// }
//
// }
// for(int i=1;i<4;i++) {
// for(int j=1;j<5;j++) {
// System.out.print("*");//同行展示
// }
// System.out.println();//换行
// }
}
}
package cn.tedu.fordemo;
//嵌套for练习
public class Test4_For2 {
public static void main(String[] args) {
//5行5列 外循环控制行,内循环控制列
// for(int i=1;i<6;i++) {
// for(int j=1;j<6;j++) {
// System.out.print("*");//同行展示*号
// }
// System.out.println();//换行
// }
// for(int i=1;i<6;i++) {
// for(int j=1;j<=i;j++) {//列数一直在变,不能写死
// System.out.print("*");
// }
// System.out.println();//换行
// }
//九九乘法表
for(int i = 1;i < 10; i++) {
for(int j = 1;j <= i; j++) {//列不固定
System.out.print(i+"*"+j+"="+i*j+" ");//动态拼接
}
System.out.println();//换行
}
}
}