我是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);
}
}
答案 0 :(得分:0)
是;像这样:
void printMyProgress() {
printStudentProgress("Nat", 0);
}
OR
void printMyProgress() {
printStudentProgress(getMyName(), getTimeSoFar());
}
答案 1 :(得分: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();
}
}
答案 2 :(得分: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);
}
}