插入if语句和构造函数

时间:2014-09-09 02:53:54

标签: java

我是Java新手,需要程序帮助。对于我的任务,我必须创建各种活动的摘要。例如"滑雪板的25%的时间"。我的目标是在我的应用程序中的for循环之后创建摘要。我创建了一个变量来存储活动的次数,但是我在尝试执行摘要时遇到了麻烦。我认为你应该使用if语句,但我不确定。任何帮助或建议都会很棒。

这是我的应用

public class app {

public static void main(String[] args) {
  String fName = "Bob";
  String lName= "Marley";
  String firstName ="Zack";
  String lastName= "Morris";
  int age1 = 21;
  int age2 = 20;
  double activity_counter;
  activity_counter = 0;

  for(int j= 1; j <20; j++)
  {
      Student st1 = new Student(fName,lName,age1);
      System.out.println(st1.getName());
      System.out.println(st1.whatsUp());   

  }
  //I believe the if statement goes here

    System.out.println("is surfing the web " +"% of the time");

    System.out.println("is reading " +"% of the time ");

    System.out.println("interacting with friend " +"% of the time ");

}
}

这是我的构造函数

    public class Student {
    String firstName;
    String lastName;
    int age;

    Student(String inf_FirstName, String inf_LastName, int inf_Age)
    {
        firstName=inf_FirstName;
        lastName=inf_LastName;
        age= inf_Age;
    }
    String getName()
    {
        return firstName +" " + lastName;

    }
    int getAge()
    {
        return age;
    }

    String whatsUp()
    {

    String answer1 ="Surfing the web";
    String answer2 = "Reading";
    String answer3 ="Interacting with friends";

    double r;
    int myActivity;
    r= Math.random();
    myActivity=(int)(r*3);
    String returnVal=" ";

    if (myActivity == 1)
    {
        return returnVal=answer1;
    }

    if (myActivity==0)
    {
        return returnVal=answer2;
    }

    if(myActivity==2)
    {
        return returnVal=answer3;
    }

    return returnVal;
    }
}

0 个答案:

没有答案