将值返回到java中的main方法

时间:2015-09-22 23:28:56

标签: java methods

我是java的新手,我还在尝试了解如何返回值然后检索它。在这个任务上,指令是使用getMinutes(),将值返回给main方法。然后在calcMeth()上使用该值来获得计算。代码有效,但是当我运行它时,getMinutes()会运行两次。我想知道如何将getMinutes()的值传递给calcMeth(),而不是两次运行getMinutes()

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


    }

}

2 个答案:

答案 0 :(得分:3)

您在getMinutes方法和main中调用calcMeth,我认为您只想从main方法调用它并将结果传递给{ {1}}作为参数

像...一样的东西。

calcMeth

您需要更改public static void main(String[] args){ int minutes = getMinutes(); sammysMotto2(); calcMeth(minutes); } 以允许值过去,例如......

calcMeth

有关详细信息,请参阅Passing Information to a Method or a Constructor

答案 1 :(得分:0)

每次调用或尝试获取方法的返回值时,只需取消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);


}

}