代码中的枚举错误

时间:2015-07-12 13:11:20

标签: java

我写了这段代码 但是当我尝试编译它时说:

 error: class, interface, or enum expected
 }
 ^
1 error

我该如何解决?我不明白。括号有问题吗?枚举意味着什么?我检查了括号,一切似乎都是有序的。

import java.util.Scanner;

public class Horoscope {
  public static void main(String[] args) {

    System.out.println("Please enter month of Birth: ");
    Scanner month = new Scanner(System.in);
    System.out.println("Please enter day of Birth: ");
    Scanner day = new Scanner(System.in);
    System.out.println("Your horoscope is ");

    if ((month == 3 && day >= 21 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) {
        System.out.print("Aries");
     }
   if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20)) {
        System.out.print("Taurus");
     }
   if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20)) {
        System.out.print("Gemini");
     }
   if ((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22)) {
       System.out.print("Cancer");
     }
  if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22)) {
        System.out.print("Leo");
     }
  if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22)) {
        System.out.print("Virgo");
     }
  if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22)) {
        System.out.print("Libra");
     }
  if ((month == 10 && day >= 23 && day <= 31) || (month == 11 && day >= 1 && day <= 21)) {
        System.out.print("Scorpio");
     }
  if ((month == 11 && day >= 22 && day <= 30) || (month == 12 && day >= 1 && day <= 21)) {
        System.out.print("Sagittarius");
     }
  if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)) {
        System.out.print("Capricorn");
     }
  if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 18)) {
        System.out.print("Aquarius");
     }
  if ((month == 2 && day >= 19 && day <= 29) || (month == 3 && day >= 1 && day <= 20)) {
        System.out.print("Pisces");
    }
}
}
}
}

由于

2 个答案:

答案 0 :(得分:1)

通过使用Scanner month = new Scanner(System.in),您只需告诉编译器仅使用in stream。你必须说int mnth = month.nextInt() 更改此部分

System.out.println("Please enter month of Birth: ");
Scanner month = new Scanner(System.in);
System.out.println("Please enter day of Birth: ");
Scanner day = new Scanner(System.in);
System.out.println("Your horoscope is ");

int month=0 , day=0;
Scanner scanner= new Scanner(System.in);
System.out.println("Please enter month of Birth: ");
month = scanner.nextInt();
System.out.println("Please enter day of Birth: ");
day = scanner.nextInt();
System.out.println("Your horoscope is ");

答案 1 :(得分:1)

在开始编写代码之前,您应该学习很少的东西。

这是您正确的代码。

import java.util.Scanner;

public class Horoscope {

    public static void main(String[] args) {

        System.out.println("Please enter month of Birth: ");
        Scanner sc = new Scanner(System.in);
        int month = sc.nextInt();
        System.out.println("Please enter day of Birth: ");
        int day = sc.nextInt();
        System.out.println("Your horoscope is ");

        if ((month == 3 && day >= 21 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) {
            System.out.print("Aries");
        }
        if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20)) {
            System.out.print("Taurus");
        }
        if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20)) {
            System.out.print("Gemini");
        }
        if ((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22)) {
            System.out.print("Cancer");
        }
        if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22)) {
            System.out.print("Leo");
        }
        if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22)) {
            System.out.print("Virgo");
        }
        if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22)) {
            System.out.print("Libra");
        }
        if ((month == 10 && day >= 23 && day <= 31) || (month == 11 && day >= 1 && day <= 21)) {
            System.out.print("Scorpio");
        }
        if ((month == 11 && day >= 22 && day <= 30) || (month == 12 && day >= 1 && day <= 21)) {
            System.out.print("Sagittarius");
        }
        if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)) {
            System.out.print("Capricorn");
        }
        if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 18)) {
            System.out.print("Aquarius");
        }
        if ((month == 2 && day >= 19 && day <= 29) || (month == 3 && day >= 1 && day <= 20)) {
            System.out.print("Pisces");
        }
    }
}