java 如何将变量分配给静态方法的返回语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6966683/
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
How to assign a variable to the return statement of a static method?
提问by Gen
I cant seem to get the value of the second method please teach me how I'm still a beginner at this. Do I have to use a different variable? And if so how do I assign the return value of that method to the said variable?
我似乎无法获得第二种方法的价值,请教我如何在这方面仍然是初学者。我必须使用不同的变量吗?如果是这样,我如何将该方法的返回值分配给所述变量?
import java.util.Scanner ;
class JavaChip
{
static Scanner s = new Scanner(System.in) ;
static double val ;
public static void main( String[] args )
{
displayLine () ;
System.out.print ("Enter number of cups of Mocha JavaChip: ");
val = getMocha( s.nextDouble() ) ;
System.out.print ("The grams of mocha is: " + val ) ;
System.out.print ("\nThe grams of sugar is: " + val ) ;
displayLine () ;
}
static double getMocha ( double c )
{
return ( 1.215 * c ) ;
}
static double getSugar (double c)
{
val = ( 0.15 * c ) ;
return val ;
}
static void displayLine()
{
System.out.println("\n\n[][][][][][][][][][][][][][][][][][][]") ;
System.out.println("[][][][][][][][][][][][][][][][][][][]") ;
}
}
回答by Peter Lawrey
If you want to use the value of a method you have to call it.
如果你想使用一个方法的值,你必须调用它。
val = getSugar(num);
Using static fields to pass arguments or return values is bad practice. This is likely to cause confusion and be a cause of subtle multi-threaded bugs. Its also more complex than just returning the value like you do with getMocha()
使用静态字段传递参数或返回值是不好的做法。这很可能会引起混乱并导致微妙的多线程错误。它也比仅仅像你那样返回值更复杂getMocha()
回答by Naor
Actually you should saparate your project into two files:
1. File that will include the main function and helper functions of display like displayLine.
2. File that wil represent the business of your project (JavaChip?).
实际上,您应该将您的项目分成两个文件:
1. 将包含显示的主要功能和辅助功能的文件,例如 displayLine。
2. 代表项目业务的文件(JavaChip?)。
Example:
例子:
Program.java:
程序.java:
class Program
{
public static void main( String[] args )
{
displayLine () ;
System.out.print ("Enter number of cups of Mocha JavaChip: ");
Scanner s = new Scanner(System.in) ;
JavaChip jc=new JavaChip();
double val = jc.getMocha( s.nextDouble()) ;
System.out.print ("The grams of mocha is: " + val ) ;
val = jc.getSugar( s.nextDouble());
System.out.print ("\nThe grams of sugar is: " + val ) ;
displayLine () ;
}
static void displayLine()
{
System.out.println("\n\n[][][][][][][][][][][][][][][][][][][]") ;
System.out.println("[][][][][][][][][][][][][][][][][][][]") ;
}
}
JavaChip.java:
JavaChip.java:
class JavaChip
{
public JavaChip()
{
}
public double getMocha ( double c )
{
return ( 1.215 * c ) ;
}
public double getSugar (double c)
{
val = ( 0.15 * c ) ;
return val ;
}
}
回答by Android Killer
Your code is 100% correct.Just you are not calling that getSugar(double value) method. Just do the same as you did for getMocha(double value).Then you can get the return value.
您的代码 100% 正确。只是您没有调用该 getSugar(double value) 方法。就像你对 getMocha(double value) 所做的一样。然后你就可以得到返回值。