简体   繁体   English

将值传递给方法

[英]Issue passing values into methods

Hi Im still new to java and Im having trouble working out passing values between methods. 嗨我仍然是Java的新手,我很难在方法之间传递值。 I still carnt get it right with all the researh I've done. 对于我所做的所有研究,我仍然是正确的。 Anyhow any comments or advice will be welcome. 无论如何,欢迎提出任何意见或建议。 To save you reading though the code bellow I have two methods that return a value and I the pass them into showTime(). 为了节省你阅读下面的代码,我有两个返回值的方法,我将它们传递给showTime()。 however with the current format I only get zero's. 但是使用当前格式我只得到零。 Any advice apart from give up now. 除了现在放弃之外的任何建议。

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);
}
 }

You are not saving your values in sH or sM in your methods, and you are returning them. 您没有在方法中将值保存在sHsM中,而是返回它们。 So, the values returned will be 0 only . 因此,返回的值0 only

int setHour = input.nextInt();

this should be: - 这应该是: -

sH = input.nextInt();

Also, you actually don't need to return them, if you have declared them as static variables outside your main method. 此外,如果您已将它们声明为main方法之外的静态变量,则实际上不需要返回它们。


A better idea is not to use static variables . 更好的想法是不使用static variables Declare your sH and sM variables inside your main method , as local variables. main method声明sHsM变量,作为局部变量。

Now when you return the values from getHour() , you can assign them to the local variables. 现在,当您从getHour()返回值时,可以将它们分配给局部变量。 And then work with them. 然后与他们合作。

So, your main method can be modified to: - 因此,您的main方法可以修改为: -

public static void main(String args[]) {
    int sH = getHour();
    int sM = getMinute();
    showTime(sH, sM);
}

And in getHour() , change your return sH; getHour() ,改变你的return sH; to return setHour; return setHour; . Similarly in getMinute() method. 同样在getMinute()方法中。

And in the else part, change getHour() invocation to return getHour(); 在else部分,更改getHour()调用以return getHour();

In your getMinute() and getHour() methods you need to return the value where you store the users input. getMinute()getHour()方法中,您需要返回存储用户输入的值。 Also in this instance there's no need for static int sH and sM as everything is being handled by the same event, unless you wanted to access them later from a different event. 同样在这种情况下,不需要静态int sH和sM,因为所有内容都由同一事件处理,除非您想稍后从其他事件访问它们。

public static void main(String args[]){

//Store the returnb value of getHour in sH
int sH = getHour();

//Store the returnb value of getMinute() in sM
int sM = getMinute();

//Display the stored values
showTime(sH,sM);
}

private 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.");

    //return the users input
    return setHour;
}else{
    System.out.println("Please enter the hour number from 0 to 24");
    getHour();
}

return 0;

}

private 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.");

    //return the users input
    return setMinute;
}else{
    System.out.println("Please enter the hour number from 0 to 60");
    getMinute();
}

    return 0;

}

private static void showTime(int sH, int sM){
System.out.println(+sH+":"+sM);
}
}

Your variables sH and sM are never assigned a value, you're just returning them unchanged. 您的变量sH和sM永远不会被赋值,您只是将它们保持不变。 Try returning setHour and setMinute instead, passing those to showTime . 尝试返回setHoursetMinute ,将它们传递给showTime

You didn't assigne values in methods to your static properties. 您没有将方法中的值分配给静态属性。

 static int sH, sM;

so thats why you are getting 0. 这就是为什么你得到0。

sH = input.nextInt(); sH = input.nextInt(); sM = input.nextInt(); sM = input.nextInt();

You have the static variables sH and sM which are being passed into showTime to print the time, but they are never assigned a value. 你有传递给showTime的静态变量sHsM来打印时间,但它们从未被赋值。 In fact you do not even need them at all. 事实上,你根本不需要它们。

Instead you should have your getHour function return the setHour variable, and your getMinute method return the setMinute variable. 相反,你应该让你的getHour函数返回setHour变量,你的getMinute方法返回setMinute变量。

Then you can simply call the showTime method like so: 然后你可以简单地调用showTime方法:

public static void main(String args[]) {
    int hour = getHour();
    int minute = getMinute();
    showTime(hour, minute);
}

There is also an issue within your getHour and getMinute methods. 您的getHourgetMinute方法中也存在问题。 They are recursively calling themselves when an invalid input is entered, yet the recursive method call isn't being returned. 当输入无效输入时,它们以递归方式调用自身,但不返回递归方法调用。 Thus the program logic will simply continue once the recursion is finished and return the original, invalid data. 因此,一旦递归完成,程序逻辑将继续,并返回原始的无效数据。 This can be solved by changing: 这可以通过改变来解决:

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();
}

to: 至:

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");
  return getHour();
}

Obviously also do this for your getMinute method too. 显然也是为你的getMinute方法做这个。

there are quite a few mistakes in your code the following code has to be replaced 您的代码中存在相当多的错误,必须替换以下代码

public class First
{

static int sH, sM;
public static void main(String args[]){


First.sH = getHour();

First.sM = getMinute();
showTime(abc.sH,abc.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.");
    return setHour;
}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.");
    return setMinute;
}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(First.sH+":"+First.sM);
}
 }

Your return sh and return sm were returning nothing which should be replaced by return setHour and return setMinute 你的返回sh和返回sm没有返回任何应该由return setHour替换并返回setMinute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM