8.运算符和分支结构
8.1 运算符
关系运算符
> < >= <= == !=
关系运算符结果有且只有 true false
逻辑运算符
&& 同真则真,有假即假
|| 有真即真,同假为假
!
自增自减运算符
8.2 从键盘上获取用户输入的数据
1.import java.util.Scanner;
2. 获取Scanner的变量, 扫描器的一个变量
Scanner sc = new Scanner(System.in);
3. 使用Scanner其中的方法
获取int类型数据 sc.nextInt();
获取double类型数据 sc.nextDouble();
获取String类型字符串数据 sc.next(); sc.nextLine();
获取一个char类型字符数据 sc.next().charAt(0); sc.nextLine().charAt(0);
8.3 分支结构
-
if
-
if-else
-
if-else if
4. switch-case
-
条件运算符/三目运算符
-
分支结构嵌套
8.4 局部变量
1. 局部变量有且只能在当前所处的代码块中使用,超出当前代码块范围无效
2. 在作用域范围以内,任何的地方可以直接使用对应的变量
3. 在一个变量的作用域范围以内,不能重定义同名变量,否则报错
4. 局部变量未赋值不能使用
8.5 练习题
1. 完成条件:判断是否是闰年
能被4整除,但是不能被100整除,或者可以被400整除
a. year % 4 == 0
b. year % 100 != 0
c. year % 400 == 0
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
2. 完成条件:判断一个字符是不是大写英文字符
A ~ Z
'A' <= ch <= 'Z' 错误的!!!
ch >= 'A' && ch <= 'Z'
3. 完成条件:判断一个字符是不是英文字符
a. 英文字符中大写靠前,小写靠后
b. 大写和小写英文字符之间有其他字符
(ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
4. 使用if判断完成对应90后00后判断,用户输入年龄,判断情况。
5. 用户输入一个数值,判断是不是偶数
6. 用户输入自己的工资,判断是否需要缴税
7.输入两个整数,打印两数之差的绝对值
import java.util.Scanner;
class HomeWork4 {
public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
System.out.println("请输入第一个整数:");
Scanner sc1 = new Scanner(System.in);
num1 = sc1.nextInt();
System.out.println("请输入第二个整数:");
Scanner sc2 = new Scanner(System.in);
num2 = sc2.nextInt();
int ret = num1 > num2 ? num1 - num2 : num2 - num1;
System.out.println("绝对值是:" + ret);
}
}
8. 展示0 ~ 100以内的所有偶数
class HomeWork7 {
public static void main(String[] args) {
for(int i = 0;i < 100;i += 2) {
System.out.println(i);
}
}
}
9.展示0 ~ 100以内的所有奇数
class HomeWork7 {
public static void main(String[] args) {
for(int i = 1;i < 100;i += 2) {
System.out.println(i);
}
}
}
10. 计算1 - 150 的和
class HomeWork9 {
public static void main(String[] args) {
int sum = 0;
for(int i = 1;i <= 150;i++) {
sum = sum + i;
}
System.out.println("1-150的和是:" + sum);
}
}
11.逢7过
import java.util.Scanner;
class HomeWork10 {
public static void main(String[] args) {
System.out.println("请输入逢七过要截止的数:");
int end = 0;
Scanner sc = new Scanner(System.in);
end = sc.nextInt();
System.out.println("截止到" + end + "需要跳过的数是:");
for(int i = 1;i <end;i++) {
if(i % 7 == 0 || i % 10 == 7) {
System.out.println(i);
}
}
}
}
12.多种方法打印26个小写字母
#for循环
class HomeWork11 {
public static void main(String[] args) {
for(char ch = 'a';ch <= 'z';ch ++) {
System.out.println(ch);
}
}
}
#while循环
class HomeWork11 {
public static void main(String[] args) {
char ch = 'a';
while (ch <= 'z') {
System.out.println(ch);
ch ++;
}
}
}
#do-while循环
class HomeWork11 {
public static void main(String[] args) {
char ch = 'a';
do {
System.out.println(ch);
ch ++;
}while (ch <= 'z');
}
}
13.例如: 输入 5 8; 计算 5 + 6 + 7 + 8; 输出 26.
import java.util.Scanner;
class HomeWork12 {
public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
int sum = 0;
System.out.println("请输入两个数:");
Scanner sc = new Scanner(System.in);
num1 = sc.nextInt();
num2 = sc.nextInt();
for(int i = num1;i <= num2;i++) {
sum += i;
}
System.out.println(sum);
}
}
14.整数逆序输出, 例如输入一个整数12345,输出5 4 3 2 1
import java.util.Scanner;
class HomeWork13 {
public static void main(String[] args ) {
int a = 0;
int b = 0;
int num = 0;
System.out.println("输入一个整数:");
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
while(num > 0) {
a = num % 10;
b = b * 10 + a;
num = num / 10;
}
System.out.println(b);
}
}
15. *****
*****
*****
*****
*****
class HomeWork14 {
public static void main(String[] args) {
char ch = '*';
for(int i = 0;i < 5;i ++) {
for(int j = 0;j < 5;j ++) {
System.out.print(ch);
}
System.out.println();
}
}
}
16. *
**
***
****
*****
class HomeWork15 {
public static void main(String[] args) {
for(int i = 0;i < 5;i ++) {
for(int j = 0;j < i+1;j ++) {
System.out.print("*");
}
System.out.println();
}
}
}
17. *
***
*****
*******
*********
class HomeWork16{
public static void main( String[] args) {
for(int i = 0;i < 5;i ++) {
for(int j = 0;j <= 5-i;j ++) {
System.out.print(" ");
}
for(int k = 0;k <= i*2;k ++) {
System.out.print("*");
}
System.out.println();
}
}
}
18. *
***
*****
*******
*********
*******
*****
***
*
class HomeWork17 {
public static void main(String[] args) {
for(int i = 1;i <= 5;i ++) {
for(int j = 1;j <= 5-i;j ++)
System.out.print(" ");
for(int k = 1;k <= 2*i-1;k ++)
System.out.print("*");
System.out.println();
}
for(int i = 1;i <= 4;i ++){
for(int j = 1;j <= i;j ++)
System.out.print(" ");
for(int k = 7;k >= 2*i-1;k --)
System.out.print("*");
System.out.println();
}
}
}