1. java.util.Scanner s = new java.util.Scanner(System.in);

    1.接收用户的输入,创建一个键盘扫描器对象

    1. int i = s.nextInt();//int类型
    2. String str = s.next();//字符串类型
    3. float b = s.nextFloat();//浮点类型
    1. public class Project02 {
    2. public static void main(String[] args) {
    3. java.util.Scanner s = new java.util.Scanner(System.in);
    4. System.out.println("Please input the month you want to search");
    5. int month = s.nextInt();
    6. if (month == 3 || month == 4 || month == 5){
    7. System.out.println("Spring");
    8. }
    9. if (month == 6 || month == 7 || month == 8) {
    10. System.out.println("Summer");
    11. }
    12. if (month == 9 || month == 10 || month == 11){
    13. System.out.println("Autumn");
    14. }
    15. if (month == 12 || month == 1 || month == 2){
    16. System.out.println("Winter");
    17. }
    18. }
    19. }
    1. public class Project03 {
    2. public static void main(String[] args) {
    3. java.util.Scanner s = new java.util.Scanner(System.in);
    4. System.out.println("Please input the month you want to search");
    5. int month = s.nextInt();
    6. switch (month)
    7. {
    8. case(3): case(4): case(5):
    9. System.out.println("Spring");
    10. break;
    11. case(6): case(7): case(8):
    12. System.out.println("Summer");
    13. break;
    14. case(9): case(10): case(11):
    15. System.out.println("Autumn");
    16. break;
    17. case(12): case(1): case(2):
    18. System.out.println("Winter");
    19. break;
    20. }
    21. }
    22. }

    以上为Switch与if的对比

    1. public class Project04 {
    2. public static void main(String[] args) {
    3. java.util.Scanner s = new java.util.Scanner(System.in);
    4. System.out.println("Please input a number");
    5. int number_a = s.nextInt();
    6. if (number_a > 0){
    7. System.out.println("The number is a positive number");
    8. } else if (number_a < 0) {
    9. System.out.println("The number is a negative number");
    10. } else if (number_a == 0) {
    11. System.out.println("The number is zero");
    12. }
    13. }
    14. }
    1. public class Fibonacci01 {
    2. public static void main(String[] args) {
    3. int number_a = 1, number_b = 1, output = 0;
    4. java.util.Scanner s = new java.util.Scanner(System.in);
    5. int n = s.nextInt();
    6. if (n<=2){
    7. switch (n){
    8. case (0):
    9. System.out.println("0");break;
    10. case (1): case (2):
    11. System.out.println("1");break;
    12. }
    13. } else {
    14. for (int i=2;i<n;i++)
    15. {
    16. output = number_a + number_b;
    17. number_a = number_b;
    18. number_b = output;
    19. }
    20. System.out.println(output);
    21. }
    22. }
    23. }

    斐波那契数列的java实现方法1

    1. public class Fibonacci02 {
    2. public static void main(String[] args) {
    3. int number[] = new int[400];
    4. int output = 0;
    5. java.util.Scanner s = new java.util.Scanner(System.in);
    6. int n = s.nextInt();
    7. number[0] = 0;
    8. number[1] = number[2] = 1;
    9. if (n <= 2) {
    10. switch (n) {
    11. case (0):
    12. System.out.println("0");
    13. break;
    14. case (1):
    15. case (2):
    16. System.out.println("1");
    17. break;
    18. }
    19. } else {
    20. for (int i = 2; i <= n; i++) {
    21. number[i] = number[i - 2] + number[i - 1];
    22. output = number[i];
    23. }
    24. System.out.println(output);
    25. }
    26. }
    27. }

    斐波那契数列的第二种实现方式