java 将值返回给java中的main方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32728668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:40:53  来源:igfitidea点击:

Returning value to main method in java

javamethods

提问by louie33

I'm new in java and i'm still trying to understand how to return a value and then retrieve it. on this task, the instructions were to use getMinutes(), to return a value to the main method. and then use that value on calcMeth(), to get the calculation. The code works, but when I run it, the getMinutes(), gets run twice. i want to know how to pass the value of getMinutes() to calcMeth() without twice running the getMinutes()

我是 Java 新手,我仍在尝试了解如何返回值然后检索它。在此任务中,指令是使用 getMinutes() 向主方法返回一个值。然后在 calcMeth() 上使用该值来进行计算。代码有效,但是当我运行它时,getMinutes() 会运行两次。我想知道如何在不运行两次 getMinutes() 的情况下将 getMinutes() 的值传递给 calcMeth()

package ch3Hw;

import java.util.Scanner;

public class SammysMotto2 {


    public static void main(String[] args){

            getMinutes();
         sammysMotto2();
        calcMeth();


    }


    public static int getMinutes(){  //method 2
        int mins;

        Scanner keyboard = new Scanner (System.in);

        System.out.println("Enter the number of minutes for Rental");
        mins=keyboard.nextInt();
        return mins;
    }


    public static void calcMeth(){  //method 3
        int minS;
        int hourS;  // hours 
        int priceH;  // price for hours used
        int remMin;     // minutes over an hour
        int priceMin;   // price of minutes used over an hour
        int totalPrice;

        minS=SammysMotto2.getMinutes(); // anytime i called the method, the method runs again
        hourS=minS/60;
        remMin=minS % 60;
        priceH=hourS*40;
        priceMin=remMin*1;
        totalPrice=priceH+priceMin;
        System.out.println("Total price of rental is $" + totalPrice);


    }

    public static void sammysMotto2(){    //Method 2

        String a  ="SsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSs";
        String b="Ss                                              Ss";
        String c="Ss        Sammy makes it fun in the sun.        Ss";


        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(b);
        System.out.println(a);


    }

}

回答by MadProgrammer

You call getMinutesin the mainmethod AND the calcMeth, I think you're suppose to only call it from the mainmethod and pass the result to calcMethas a parameter

你打电话getMinutesmain方法和calcMeth,我想你是假设只从调用它main的方法,并将结果传递到calcMeth作为参数

Something like...

就像是...

public static void main(String[] args){

    int minutes = getMinutes();
    sammysMotto2();
    calcMeth(minutes);

}

And you would need to change your calcMethto allow for a value to be past to it, for example...

并且您需要更改您calcMeth的值以允许超过它的值,例如......

public static void calcMeth(int minS){  //method 3
    int hourS;  // hours 
    int priceH;  // price for hours used
    int remMin;     // minutes over an hour
    int priceMin;   // price of minutes used over an hour
    int totalPrice;

    hourS=minS/60;

See Passing Information to a Method or a Constructorfor more details

有关更多详细信息,请参阅将信息传递给方法或构造函数

回答by BratBart

Just cancel first call for getMinutes() method in your main , each time you call or try to get return value of a method . this method will execute all codes inside it before you got its return value.

每次调用或尝试获取方法的返回值时,只需取消 main 中对 getMinutes() 方法的第一次调用。这个方法会在你得到它的返回值之前执行它里面的所有代码。

import java.util.Scanner;
public class SammysMotto2 {
public static void main(String[] args){
    // getMinutes();
    sammysMotto2();
    calcMeth();


}


public static int getMinutes(){  //method 2
    int mins;

    Scanner keyboard = new Scanner (System.in);

    System.out.println("Enter the number of minutes for Rental");
    mins=keyboard.nextInt();
    return mins;
}


public static void calcMeth(){  //method 3
    int minS;
    int hourS;  // hours 
    int priceH;  // price for hours used
    int remMin;     // minutes over an hour
    int priceMin;   // price of minutes used over an hour
    int totalPrice;

    minS=SammysMotto2.getMinutes(); // anytime i called the method, the method runs again
    hourS=minS/60;
    remMin=minS % 60;
    priceH=hourS*40;
    priceMin=remMin*1;
    totalPrice=priceH+priceMin;
    System.out.println("Total price of rental is $" + totalPrice);


}

public static void sammysMotto2(){    //Method 2

    String a  ="SsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSs";
    String b="Ss                                              Ss";
    String c="Ss        Sammy makes it fun in the sun.        Ss";


    System.out.println(a);
    System.out.println(b);
    System.out.println(c);
    System.out.println(b);
    System.out.println(a);


}

}