可变问题

时间:2013-12-09 19:09:40

标签: java variables

因此,这个项目要求我“组织”一个秘密特工,并通过随机事件 - 如生日,性别,姓名,城市等来做到这一点......我一直遇到这个代码的生日部分有问题..我想我必须制作一个临时变量,但我不确定......我以线性方式阅读它,从int->字符串看起来没问题,其中有一个布尔值。任何帮助?

public static Agent GetRandomAgent()
{
    Agent randomAgent = new Agent();

    WordList maleName = new WordList("MaleNames.txt");
    WordList femaleName = new WordList("FemaleNames.txt");
    WordList cityBorn = new WordList("Cities.txt");
    WordList cityNow = new WordList ("Cities.txt");
    WordList major = new WordList ("Majors.txt");

    //Setter methods
    //Sets the gender of the agent generated 


    double setGender = (Math.random()); 

    Random r = new Random();

    if(Math.random()*100 > 50)
    {
        randomAgent.gender = "female";
    }
    else //if not female, then male
    {
        randomAgent.gender = "male";
    }

    public String setBirthday()
    //randomAgent.birthday = new String getBirthday; 
    {
        int birthdayYear = 1900 + (int)(Math.random() * ((2012 - 1900) + 1));
        int birthdayMonth = 1 + (int)(Math.random() * ((12 - 1) + 1));

        switch(birthdayMonth) {
        //set for 30-day months: April(4), June(6), September(9), November(11)
        case 4:
        case 6:
        case 9:
        case 11:
            birthdayDay = 1 + (int)(Math.random() * ((30 - 1) + 1));
            break;

        case 2:
            if (isLeapYear(birthdayYear)) {
              birthdayDay = 1 + (int)(Math.random() * ((29 - 1) + 1));
            } else {
              birthdayDay = 1 + (int)(Math.random() * ((28 - 1) + 1));
            }
            break;

        //Set for 31-day months: January(1), March(3), May(5), July(7), August(8), October(10), December(12)
          default:
            birthdayDay = 1 + (int)(Math.random() * ((31 - 1) + 1));
          break;
        }

        //Convert the random integers back into Strings (easier to use)
        String year = Integer.toString(birthdayYear);
        String month = Integer.toString(birthdayMonth);
        String day = Integer.toString(birthdayDay);

        //Account to make sure there is a zero before the date (clarity)
        if (birthdayMonth < 10) {
            month = "0" + birthdayMonth;
        }

        if (birthdayDay < 10) {
            day = "0" + birthdayDay;
        }

        String suffix; 
        if ((day.equals("01") || (day.equals("21") || (day.equals("31")))))
        {
            suffix = "st";
        }

        if ((day.equals("02") || (day.equals("22"))))
        {
            suffix = "nd";
        }

        if (day == "03" || day == "23")
        {
                    suffix = "rd";
        }
        else
        { 
            suffix = "th";
        }
        return this.birthday = (day + suffix + " " + month + " of " + year); 
      }

      public static boolean isLeapYear;
      int year; 
      {
        Calendar rightNow = Calendar.getInstance();
        rightNow.set(Calendar.YEAR, year);
        int numberOfDays = rightNow.getActualMaximum(Calendar.DAY_OF_YEAR);

        if (numberOfDays > 365) {
            return true;
        }
        return false;
      }

1 个答案:

答案 0 :(得分:2)

代码有些问题:

  1. 您使用setBirthday()方法签名收到错误的原因是因为GetRandomAgent()尚未关闭。设置randomAgent.birthday
  2. 后请注意结束括号
  3. birthdayDay未在setBirthday()初始化。
  4. isLeapYear(int)的方法签名不正确。
  5. 正如Smit所说,你需要将字符串与.equals()而不是==进行比较。
  6. 记住所有这些......

    import java.util.Calendar;
    import java.util.Random;
    
    public class Agent
    {
    
        String gender;
        String birthday;
    
        public static Agent GetRandomAgent()
        {
            Agent randomAgent = new Agent();
    
            WordList maleName = new WordList("MaleNames.txt");
            WordList femaleName = new WordList("FemaleNames.txt");
            WordList cityBorn = new WordList("Cities.txt");
            WordList cityNow = new WordList("Cities.txt");
            WordList major = new WordList("Majors.txt");
    
            //Setter methods
            //Sets the gender of the agent generated 
            double setGender = (Math.random());
    
            Random r = new Random();
    
            if(Math.random() * 100 > 50)
            {
                randomAgent.gender = "female";
            }
            else //if not female, then male
            {
                randomAgent.gender = "male";
            }
            randomAgent.birthday = randomAgent.setBirthday();
        }
    
        public String setBirthday()
        {
            int birthdayYear = 1900 + (int) (Math.random() * ((2012 - 1900) + 1));
            int birthdayMonth = 1 + (int) (Math.random() * ((12 - 1) + 1));
            int birthdayDay;
            switch(birthdayMonth)
            {
                //set for 30-day months: April(4), June(6), September(9), November(11)
                case 4:
                case 6:
                case 9:
                case 11:
                    birthdayDay = 1 + (int) (Math.random() * ((30 - 1) + 1));
                    break;
    
                case 2:
                    if(isLeapYear(birthdayYear))
                    {
                        birthdayDay = 1 + (int) (Math.random() * ((29 - 1) + 1));
                    }
                    else
                    {
                        birthdayDay = 1 + (int) (Math.random() * ((28 - 1) + 1));
                    }
                    break;
    
                //Set for 31-day months: January(1), March(3), May(5), July(7), August(8), October(10), December(12)
                default:
                    birthdayDay = 1 + (int) (Math.random() * ((31 - 1) + 1));
                    break;
            }
    
            //Convert the random integers back into Strings (easier to use)
            String year = Integer.toString(birthdayYear);
            String month = Integer.toString(birthdayMonth);
            String day = Integer.toString(birthdayDay);
    
            //Account to make sure there is a zero before the date (clarity)
            if(birthdayMonth < 10)
            {
                month = "0" + birthdayMonth;
            }
    
            if(birthdayDay < 10)
            {
                day = "0" + birthdayDay;
            }
    
            String suffix;
            if((day.equals("01") || (day.equals("21") || (day.equals("31")))))
            {
                suffix = "st";
            }
    
            if((day.equals("02") || (day.equals("22"))))
            {
                suffix = "nd";
            }
    
            if(day.equals("03") || day.equals("23"))
            {
                suffix = "rd";
            }
            else
            {
                suffix = "th";
            }
            return this.birthday = (day + suffix + " " + month + " of " + year);
        }
    
        public static boolean isLeapYear(int year)
        {
            Calendar rightNow = Calendar.getInstance();
            rightNow.set(Calendar.YEAR, year);
            int numberOfDays = rightNow.getActualMaximum(Calendar.DAY_OF_YEAR);
    
            if(numberOfDays > 365)
            {
                return true;
            }
            return false;
        }
    }