第2章 判断语句
笔记
比较运算符
(1) 大于>
(2) 小于 <
(3) 大于等于>=
(4) 小于等于<=
(5) 等于==
(6) 不等于!=
条件表达式
(1) 与&&
(2) 或||
(3) 非!
判断闰年:year % 4 == 0 && year % 100 != 0 || year % 400 == 0
switch语句
swtich
语句中如果不加break
语句,则从上到下匹配到第一个case
后,会顺次执行后面每个case
中的语句。如果与所有case
都不匹配,则执行default
语句
switch(day) {
case 1:
name = "Monday";
break;
case 2:
name = "Tuesday";
break;
case 3:
name = "Wednesday";
break;
case 4:
name = "Thursday";
break;
case 5:
name = "Friday";
break;
case 6:
name = "Saturday";
break;
case 7:
name = "Sunday";
break;
default:
name = "not valid";
}
if 语句
基本if-else
语句:当条件成立时,执行某些语句;否则执行另一些语句。
if (a > 5) {
System.out.println("a > 5");
} else {
System.out.println("a <= 5");
}
else
语句可省略
if (a > 5) {
System.out.println("a > 5");
}
当只有一条语句时,大括号可以省略:
if (a > 5)
System.out.println("a > 5");
if-else
语句内部也可以是if-else
语句,即可嵌套。
if (a > 5) {
if (a > 10)
System.out.println("a > 10");
else
System.out.println("a > 5 and a <= 10");
}
多条if-else
语句
if (a > 15) {
System.out.println("a > 15");
} else if (a > 10) {
System.out.println("a > 10 and a <= 15");
} else if (a > 5) {
System.out.println("a > 5 and a <= 10");
} else {
System.out.println("a <= 5");
}
习题
AcWing 665. 倍数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt(), b = scanner.nextInt();
scanner.close();
if (a % b == 0 || b % a == 0)
System.out.println("Sao Multiplos");
else
System.out.println("Nao sao Multiplos");
}
}
AcWing 660. 零食
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double[] price = {0, 4, 4.5, 5, 2, 1.5};
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt(), n = scanner.nextInt();
scanner.close();
System.out.printf("Total: R$ %.2f\n", price[id] * n);
}
}
AcWing 659. 区间
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
scanner.close();
if (a > 75 && a <= 100)
System.out.println("Intervalo (75,100]");
else if (a > 50 && a <= 75)
System.out.println("Intervalo (50,75]");
else if (a > 25 && a <= 50)
System.out.println("Intervalo (25,50]");
else if (a >= 0 && a <= 25)
System.out.println("Intervalo [0,25]");
else
System.out.println("Fora de intervalo");
}
}
AcWing 664. 三角形
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble(), b = scanner.nextDouble(), c = scanner.nextDouble();
scanner.close();
if (a + b > c && a + c > b && b + c > a)
System.out.printf("Perimetro = %.1f\n", a + b + c);
else
System.out.printf("Area = %.1f\n", (a + b) * c / 2.0);
}
}
判断三角形的方法
条件$a + b > c$,$a + c > b$,$b+c>a$同时成立
AcWing 667. 游戏时间
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt(), b = scanner.nextInt();
scanner.close();
if (a == b)
System.out.println("O JOGO DUROU 24 HORA(S)");
else
System.out.printf("O JOGO DUROU %d HORA(S)\n", (b - a + 24) % 24);
}
}
AcWing 669. 加薪
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
scanner.close();
int rate = 0;
if (a <= 400) rate = 15;
else if (a <= 800) rate = 12;
else if (a <= 1200) rate = 10;
else if (a <= 2000) rate = 7;
else rate = 4;
System.out.printf("Novo salario: %.2f\n", a * (1 + rate / 100.0));
System.out.printf("Reajuste ganho: %.2f\n", a * (rate / 100.0));
System.out.printf("Em percentual: %d %%\n", rate);
}
}
AcWing 670. 动物
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String a = scanner.next(), b = scanner.next(), c = scanner.next();
scanner.close();
if (a.equals("vertebrado")) {
if (b.equals("ave")) {
if (c.equals("carnivoro"))
System.out.println("aguia");
else
System.out.println("pomba");
} else {
if (c.equals("onivoro"))
System.out.println("homem");
else
System.out.println("vaca");
}
} else {
if (b.equals("inseto")) {
if (c.equals("hematofago"))
System.out.println("pulga");
else
System.out.println("lagarta");
} else {
if (c.equals("hematofago"))
System.out.println("sanguessuga");
else
System.out.println("minhoca");
}
}
}
}
AcWing 657. 选择练习1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int d = scanner.nextInt();
scanner.close();
if (b > c && d > a && c + d > a + b && c > 0 && d > 0 && a % 2 == 0)
System.out.println("Valores aceitos");
else
System.out.println("Valores nao aceitos");
}
}
AcWing 671. DDD
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
scanner.close();
if (a == 61) System.out.println("Brasilia");
else if (a == 71) System.out.println("Salvador");
else if (a == 11) System.out.println("Sao Paulo");
else if (a == 21) System.out.println("Rio de Janeiro");
else if (a == 32) System.out.println("Juiz de Fora");
else if (a == 19) System.out.println("Campinas");
else if (a == 27) System.out.println("Vitoria");
else if (a == 31) System.out.println("Belo Horizonte");
else System.out.println("DDD nao cadastrado");
}
}
AcWing 662. 点的坐标
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x = scanner.nextDouble();
double y = scanner.nextDouble();
scanner.close();
if (x == 0 && y == 0) System.out.println("Origem");
else if (x == 0) System.out.println("Eixo Y");
else if (y == 0) System.out.println("Eixo X");
else if (x > 0 && y > 0) System.out.println("Q1");
else if (x < 0 && y > 0) System.out.println("Q2");
else if (x < 0 && y < 0) System.out.println("Q3");
else System.out.println("Q4");
}
}
AcWing 666. 三角形类型
import java.util.Scanner;
public class Main {
public static double a, b, c;
public static void sort(double x, double y, double z) {
double max = Math.max(Math.max(x, y), z);
double min = Math.min(Math.min(x, y), z);
double mid = x + y + z - max - min;
a = max;
b = mid;
c = min;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x = scanner.nextDouble();
double y = scanner.nextDouble();
double z = scanner.nextDouble();
scanner.close();
sort(x, y, z);
if (a >= b + c) System.out.println("NAO FORMA TRIANGULO");
else{
if (a * a == b * b + c * c) System.out.println("TRIANGULO RETANGULO");
else if (a * a > b * b + c * c) System.out.println("TRIANGULO OBTUSANGULO");
else System.out.println("TRIANGULO ACUTANGULO");
if (a == b && b == c) System.out.println("TRIANGULO EQUILATERO");
else if (a == b || a == c || b == c) System.out.println("TRIANGULO ISOSCELES");
}
}
}
AcWing 668. 游戏时间2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt(), b = scanner.nextInt();
int c = scanner.nextInt(), d = scanner.nextInt();
scanner.close();
if (a == c && b == d) System.out.println("O JOGO DUROU 24 HORA(S) E 0 MINUTO(S)");
else {
int hour = c - a, minute = d - b;
if (minute < 0) {
hour--;
minute = 60 + minute;
}
if (hour < 0) hour += 24;
System.out.printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", hour, minute);
}
}
}
AcWing 672. 税
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
scanner.close();
if (a > 2000) {
double total = 0;
if (a <= 3000) total += (a - 2000) * 0.08;
else {
total += (3000 - 2000) * 0.08;
if (a <= 4500) total += (a - 3000) * 0.18;
else {
total += (4500 - 3000) * 0.18;
total += (a - 4500) * 0.28;
}
}
System.out.printf("R$ %.2f\n", total);
} else {
System.out.println("Isento");
}
}
}
AcWing 663. 简单排序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] a = new int[3];
int[] b = new int[3];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) a[i] = scanner.nextInt();
scanner.close();
b[0] = a[0];
b[2] = a[0];
for (int i = 1; i < 3; i++)
if (a[i] < b[0]) b[0] = a[i];
else if (a[i] > b[2]) b[2] = a[i];
b[1] = a[0] + a[1] + a[2] - b[0] - b[2];
for (int i = 0; i < 3; i++)
System.out.println(b[i]);
System.out.println("");
for (int i = 0; i < 3; i++)
System.out.println(a[i]);
}
}
AcWing 658. 一元二次方程公式
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
scanner.close();
double delta = b * b - 4 * a * c;
if (delta < 0 || a == 0) System.out.println("Impossivel calcular");
else {
double p = - b / (2 * a);
double q = Math.sqrt(delta) / (2 * a);
double x1 = p + q, x2 = p - q;
System.out.printf("R1 = %.5f\n", x1);
System.out.printf("R2 = %.5f\n", x2);
}
}
}
AcWing 661. 平均数3
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double[] a = new double[4];
int[] coef = {2, 3, 4, 1};
double average = 0;
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < a.length; i++) {
a[i] = scanner.nextDouble();
average += coef[i] * a[i];
}
average /= 10.0;
System.out.printf("Media: %.1f\n", average);
if (average >= 7.0) System.out.println("Aluno aprovado.");
else if (average < 5.0) System.out.println("Aluno reprovado.");
else {
System.out.println("Aluno em exame.");
double y = scanner.nextDouble();
System.out.printf("Nota do exame: %.1f\n", y);
double z = (average + y) / 2.0;
if (z >= 5.0) System.out.println("Aluno aprovado.");
else System.out.println("Aluno reprovado.");
System.out.printf("Media final: %.1f\n", z);
}
scanner.close();
}
}