-5

我是 Java 新手,如果printMyProgress()可以在不声明任何变量的情况下编写方法,我很感兴趣(String name = "Nat"; int time=0;).

public class Q7 {
String getMyName() {
  String name = "Nat";
    return name;  
}
int getMyTimeSoFar() {
    int time=0;
    return time;
}

 void printStudentProgress(String name, int time) {
     String minute ="";
     if (time==0||time>1) 
     minute = "minutes";
     else minute = "minute";
     print (name + " took " +time+ " "+  minute+" to reach Q7 in the Exam");     
 }

   void printMyProgress() {
       String name = "Nat";
       int time=0;
       printStudentProgress(name, time);  
   }
}
4

3 回答 3

0

是的; 像这样:

void printMyProgress() {
    printStudentProgress("Nat", 0);  
} 

或者

void printMyProgress() {
    printStudentProgress(getMyName(), getTimeSoFar());  
} 
于 2013-11-03T05:34:19.403 回答
0
public class Q7 {
    String getMyName() {
        return "Nat";  
    }
    int getMyTimeSoFar() {
        return 0;
    }

    void printStudentProgress(String name, int time) {
         String minute ="";
         if (time==0||time>1) 
          minute = "minutes";
         else 
          minute = "minute";
          System.out.println (name + " took " +time+ " "+  minute+" to reach Q7 in the Exam");     
    }

    void printMyProgress()
    {
     printStudentProgress(getMyName(), getMyTimeSoFar());  
    }

    public static void main(String [] args)
    {
       Q7 q7 = new Q7();

       q7.printMyProgress();
    }   
}
于 2013-11-03T05:37:23.380 回答
0

是的,可以编写此方法而无需在其中声明任何变量。您可以在全局范围内声明变量:

public class Q7
{
    String name = "Nat";
    int time = 0;

    String getMyName()
    {
        return name;
    }

    int getMyTimeSoFar()
    {
        return time;
    }

    void printStudentProgress(String name, int time) 
    {
        String minute ="";

        if (time == 0 || time > 1) 
        {
            minute = "minutes";
        }
        else 
        {
            minute = "minute";
        }

        System.out.println(name + " took " + time + " " + minute + " to reach Q7 in the
            Exam");
    }

    void printMyProgress()
    {
        printStudentProgress(name, time);
    }
}
于 2013-11-03T05:37:37.303 回答