条件运算符
任务1 泰若星球上的季节
package zh.codegym.task.task04.task0411;
public class Solution {
public static void main(String[] args) {
checkSeason(12);
checkSeason(4);
checkSeason(7);
checkSeason(10);
}
public static void checkSeason(int month) {
if(month >= 3 && month <= 5){
System.out.println("春季");
}else if(month >= 6 && month <= 8){
System.out.println("夏季");
}else if(month >= 9 && month <= 11){
System.out.println("秋季");
}else{
System.out.println("冬季");
}
//在此编写你的代码
}
}
任务2 正数和负数
package zh.codegym.task.task04.task0412;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sNum = reader.readLine();
int num = Integer.parseInt(sNum);
if (num > 0){
num *= 2;
}else if(num < 0){
num += 1;
}
System.out.println(num);
//在此编写你的代码
}
}
任务3 星期几
package zh.codegym.task.task04.task0413;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sNum = reader.readLine();
int num = Integer.parseInt(sNum);
if(num == 1){
System.out.println("星期一");
}else if(num == 2){
System.out.println("星期二");
}else if(num == 3){
System.out.println("星期三");
}else if(num == 4){
System.out.println("星期四");
}else if(num == 5){
System.out.println("星期五");
}else if(num == 6){
System.out.println("星期六");
}else if(num == 7){
System.out.println("星期日");
}else{
System.out.println("一周中没有这一天");
}
//在此编写你的代码
}
}
任务4
package zh.codegym.task.task04.task0414;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int days = Integer.parseInt(reader.readLine());
if (days % 4 == 0) {
if ((days % 100 == 0) && (days % 400 != 0)) {
days = 365;
} else {
days = 366;
}
} else {
days = 365;
}
System.out.println("这一年包含的天数:" + days);
}
}
任务5
package zh.codegym.task.task04.task0415;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sA = reader.readLine();
String sB = reader.readLine();
String sC = reader.readLine();
int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
int c = Integer.parseInt(sC);
if(a + b > c && a + c > b && b + c > a){
System.out.println("三角形可能存在。");
}else{
System.out.println("三角形不可能存在。");
}
//在此编写你的代码
}
}
任务6 蒙眼过马路
package zh.codegym.task.task04.task0416;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sNum = reader.readLine();
double num = Double.parseDouble(sNum);
num = num % 5;
if(num >= 0 && num < 3){
System.out.println("绿色");
}else if(num >= 3 && num < 4){
System.out.println("黄色");
}else{
System.out.println("红色");
}
//在此编写你的代码
}
}
任务7 是否有一对?
package zh.codegym.task.task04.task0417;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sA = reader.readLine();
String sB = reader.readLine();
String sC = reader.readLine();
int a = Integer.parseInt(sA);
int b = Integer.parseInt(sB);
int c = Integer.parseInt(sC);
if(a == b && b == c){
System.out.println(a + " " + b + " " + c);
}else if(a == b){
System.out.println(a + " " + b );
}else if(b == c){
System.out.println(b + " " + c);
}else if(a == c){
System.out.println(a + " " + c);
}
//在此编写你的代码
}
}