[英]Returning value to main method in java
I'm new in java and i'm still trying to understand how to return a value and then retrieve it. 我是Java的新手,但我仍在尝试了解如何返回值然后检索它。 on this task, the instructions were to use getMinutes(), to return a value to the main method. 在此任务上,指令是使用getMinutes()将值返回给main方法。 and then use that value on calcMeth(), to get the calculation. 然后在calcMeth()上使用该值来获取计算结果。 The code works, but when I run it, the getMinutes(), gets run twice. 该代码有效,但是当我运行它时,getMinutes()会运行两次。 i want to know how to pass the value of getMinutes() to calcMeth() without twice running the 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);
}
}
You call getMinutes
in the main
method AND the calcMeth
, I think you're suppose to only call it from the main
method and pass the result to calcMeth
as a parameter 您可以在main
方法和calcMeth
调用getMinutes
,我想您只是想从main
方法中调用它,并将结果作为参数传递给calcMeth
Something like... 就像是...
public static void main(String[] args){
int minutes = getMinutes();
sammysMotto2();
calcMeth(minutes);
}
And you would need to change your calcMeth
to 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 Constructor for more details 有关更多详细信息,请参见将信息传递给方法或构造函数 。
Just cancel first call for getMinutes() method in your main , each time you call or try to get return value of a method . 每次调用或尝试获取方法的返回值时,只需取消对main中的getMinutes()方法的首次调用即可。 this method will execute all codes inside it before you got its return value. 在获得返回值之前,此方法将执行其中的所有代码。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.