POLAYOR

学校Java Week4

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

import java.util.Scanner;

public class W4_roommateA {
    public static void main(String[] args) {
        System.out.print("Please Input the Limit:");

        Scanner scanner = new Scanner(System.in);

        int limit = scanner.nextInt();
        int num1 = 0;
        int num2 = 1;
        int num3 = num1 + num2;

        System.out.print(num1);
        if (limit >= 1) {
            System.out.print(" " + num2);
        }
        while (limit > 1) {
            System.out.print(" " + num3);
            num1 = num2;
            num2 = num3;
            num3 = num1 + num2;
            if (num3 > limit) {
                break;
            }
        }
    }
}

posted on 2022-09-29 17:14  POLAYOR  阅读(16)  评论(0编辑  收藏  举报

导航