Week4
W4L1
三元运算符 (ternary operator)
public static void main(String[] args) {
boolean isCar = true;
boolean wasCar = isCar ? true : false;
System.out.println(wasCar);
}
Methods
modifier return-type method-name (parameter-list){
body of the method;
}
Control Flow
If-then / If-else
Switch
While / Do While
W4L2 ex1
import java.util.Scanner;
public class W4L2_ex1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
if (num1<=21 | num2<=21) {
if (num1<=21 & num2<=21) {
if (num2 > num1) {
System.out.println(num2);
} else {
System.out.println(num1);
}
} else if (num1 <= 21) {
System.out.println(num1);
} else {
System.out.println(num2);
}
} else {
System.out.println("-1");
}
}
}
W4L2 ex2
import java.util.Scanner;
public class W4L2_ex2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int init = scanner.nextInt();
int numInfect = scanner.nextInt();
int population = scanner.nextInt();
int day = 1;
while (init<population) {
day += 1;
init += init*numInfect;
}
System.out.println(day);
}
}
W4 CW1 4.1
import java.util.Scanner;
public class W4_CW1_41 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int s1 = scanner.nextInt();
int s2 = scanner.nextInt();
int s3 = scanner.nextInt();
if (s1>0 & s2>0 & s3 >0) {
boolean c1 = s1*s1 +s2*s2 == s3*s3;
boolean c2 = s2*s2 +s3*s3 == s1*s1;
boolean c3 = s3*s3 +s1*s1 == s2*s2;
if (c1 | c2 | c3) {
System.out.println("true");
} else {
System.out.println("false");
}
} else {
System.out.println("false");
}
}
}
W4 CW1 4.2
import java.util.Scanner;
public class W4_CW1_42 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int count = 1;
while (n != 1) {
if (n % 2 == 0) {
n = n/2;
} else {
n = 3*n+1;
}
count += 1;
}
System.out.println(count);
}
}
标签:Java,Scanner,nextInt,int,System,public,学校,Week4,scanner
From: https://www.cnblogs.com/POLAYOR/p/16742289.html