日期:2014-05-20  浏览次数:20874 次

java基础题、关于输入月日来判断星座的、求教
我这个判断星座的代码:只能在代码中输入日期才能判断啊!
以下是我的代码!
我希望这个程序在运行时随便我们输入日期:
各位大侠帮帮忙:
 public class xz{ 
   
  //10.星座
  static void getXingZuo(int month,int day)
  {
   
  if(day > 31 || day<1)
  System.out.println("日期输入错误");
  else
  {
  switch(month)
  {
  case 1:
  if(day<22)
  System.out.println("星座是:摩羯座");
  else
  System.out.println("星座是:水瓶座");
  break;
  case 2:
  if(day<22)
  System.out.println("星座是:水瓶座");
  else
  System.out.println("星座是:双鱼座");
  break;
  case 3:
  if(day<22)
  System.out.println("星座是:双鱼座");
  else
  System.out.println("星座是:白羊座");
  break;
  case 4:
  if(day<22)
  System.out.println("星座是:白羊座");
  else
  System.out.println("星座是:金牛座");
  break;
  case 5:
  if(day<22)
  System.out.println("星座是:金牛座");
  else
  System.out.println("星座是:双子座");
  break;
  case 6:
  if(day<22)
  System.out.println("星座是:双子座");
  else
  System.out.println("星座是:巨蟹座");
  break;
  case 7:
  if(day<22)
  System.out.println("星座是:巨蟹座");
  else
  System.out.println("星座是:狮子座");
  break;
  case 8:
  if(day<22)
  System.out.println("星座是:狮子座");
  else
  System.out.println("星座是:处女座");
  break;
  case 9:
  if(day<22)
  System.out.println("星座是:处女座");
  else
  System.out.println("星座是:天枰座");
  break;
  case 10:
  if(day<22)
  System.out.println("星座是:天枰座");
  else
  System.out.println("星座是:天蝎座");
  break;
  case 11:
  if(day<22)
  System.out.println("星座是:天蝎座");
  else
  System.out.println("星座是:射手座");
  break;
  case 12:
  if(day<22)
  System.out.println("星座是:射手座");
  else
  System.out.println("星座是:摩羯座");
  break;
  default:
  System.out.println("日期输入错误");
  }
  }
  }
  public static void main(String[] args){
  getXingZuo(12,1);
  }
  }

------解决方案--------------------
getXingZuo(args[0],args[1]);

java getXingZuo 12 1
------解决方案--------------------
Java code

import java.util.Scanner;


public class ConstallationDemo {
    static void getConstallation(int month,int day)
    {

        if(day > 31 || day<1)
            System.out.println("日期输入错误");
        else
        {
            switch(month)
            {
                case 1:
                    if(day<22)
                        System.out.println("星座是:摩羯座");
                    else
                        System.out.println("星座是:水瓶座");
                    break;
                case 2:
                    if(day<22)
                        System.out.println("星座是:水瓶座");
                    else
                        System.out.println("星座是:双鱼座");
                    break;
                case 3:
                    if(day<22)
                        System.out.println("星座是:双鱼座");
                    else
                        System.out.println("星座是:白羊座");
                    break;
                case 4:
                    if(day<22)
                        System.out.println("星座是:白羊座");
                    else
                        System.out.println("星座是:金牛座");
                    break;
                case 5:
                    if(day<22)
                        System.out.println("星座是:金牛座");
                    else
                        System.out.println("星座是:双子座");
                    break;
                case 6:
                    if(day<22)
                        System.out.println("星座是:双子座");
                    else
                        System.out.println("星座是:巨蟹座");
                    break;
                case 7:
                    if(day<22)
                        System.out.println("星座是:巨蟹座");
                    else
                        System.out.println("星座是:狮子座");
                    break;
                case 8:
                    if(day<22)
                        System.out.println("星座是:狮子座");
                    else
                        System.out.println("星座是:处女座");
                    break;
                case 9:
                    if(day<22)
                        System.out.println("星座是:处女座");
                    else
                        System.out.println("星座是:天枰座");
                    break;
                case 10:
                    if(day<22)
                        System.out.println("星座是:天枰座");
                    else
                        System.out.println("星座是:天蝎座");
                    break;
                case 11:
                    if(day<22)
                        System.out.println("星座是:天蝎座");
                    else
                        System.out.println("星座是:射手座");
                    break;
                case 12:
                    if(day<22)
                        System.out.println("星座是:射手座");
                    else
                        System.out.println("星座是:摩羯座");
                    break;
                default:
                    System.out.println("日期输入错误");
            }
        }
    }
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入你的出生月份");
        int month=scanner.nextInt();
        System.out.print("请输入你的出生日期");
        int day=scanner.nextInt();
        getConstallation(month,day);
    }
}