嗨,我还是 java 新手,在方法之间传递值时遇到了麻烦。我所做的所有研究仍然无法正确解决。无论如何,任何意见或建议都将受到欢迎。为了节省您阅读下面代码的时间,我有两个返回值的方法,我将它们传递给 showTime()。但是,使用当前格式,我只能得到零。除了现在放弃之外的任何建议。
import java.util.Scanner;
public class First
{
static int sH, sM;
public static void main(String args[]){
getHour();
getMinute();
showTime(sH,sM);
}
static int getHour(){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the hour: ");
int setHour = input.nextInt();
if(setHour <= 24){
System.out.println("You entered " +setHour+ " for the hour.");
}else{
System.out.println("Please enter the hour number from 0 to 24");
getHour();
}
return sH;
}
static int getMinute(){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the mintues: ");
int setMinute = input.nextInt();
if(setMinute <= 60){
System.out.println("You entered " +setMinute+ " for the minutes.");
}else{
System.out.println("Please enter the hour number from 0 to 60");
getMinute();
}
return sM;
}
private static void showTime(int sH, int sM){
System.out.println(+sH+":"+sM);
}
}