Java 在方法之间传递变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19499785/
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
Passing Variables between methods?
提问by Micheal Collins
So im trying to write a simple java program for college and I'm a complete newbie at this java stuff. I keep getting an error when I compile, "error - could not find symbol" within the method printreciept. I know that it's something like not being able to access the variables within the main. Could anyone help? I know I'll prob have alot of errors if I do fix it but I'd rather start here! P.S. sorry for all of the code :/
所以我试图为大学编写一个简单的 java 程序,而我是这个 java 东西的完全新手。我在编译时不断收到错误消息,“错误 - 找不到符号”在方法 printreciept 中。我知道这就像无法访问 main.js 中的变量一样。有人可以帮忙吗?我知道如果我修复它,我可能会遇到很多错误,但我宁愿从这里开始!PS对不起所有的代码:/
import java.util.Scanner;
public class Order {
public static void main (String[] args) {
String clubcard;
double clubcard_discount;
double special_discount;
double balance;
double final_balance;
int apples;
int oranges;
int apples_cost;
int oranges_cost;
final Scanner scanner = new Scanner( System.in);
System.out.println("How Many Bags of Apples?");
apples = scanner.nextInt( );
System.out.println("How many bags of Oranges?");
oranges = scanner.nextInt( );
System.out.println("Do you have a clubcard? Yes/No");
clubcard = scanner.nextLine();
if(clubcard == "Yes") {
clubcard_discount = clubcard_discount - 1.0;
final_balance = final_balance - (balance / 100 * 10);
}
else if(clubcard == "No") {
special_discount = 0.0;
}
if(apples == 3) {
special_discount = -1.0;
balance = balance - 1.0;
}
}
//Calculating the cost of apples and oranges
public void calculate_apples (final double apples_cost ) {
apples_cost = apples * 1.0;
}
public void calculate_oranges (final double oranges_cost ) {
oranges_cost = oranges * 1.25;
}
//Printing the receipt
public static void printReceipt() {
System.out.println("Bags of apples: " + apples);
System.out.println("Bags of oranges: " + oranges);
System.out.println("Clubcard: " + clubcard);
System.out.println( );
System.out.println("Price for apples: " + apples_cost);
System.out.println("Special discount: " + special_discount);
System.out.println("Price of oranges: " + oranges_cost);
System.out.println("Total: " + balance);
System.out.println("Clubcard discount: " + clubcard_discount);
System.out.println( );
System.out.println("Final price: " + final_balance);
System.out.println( );
System.out.println("Thanks for doing business with CS2500.");
}
}
回答by rgettman
You have declared all your variables as local variables inside the main
method, so they aren't in scope outside main
. To have them accessible to other methods, you can do one of the following:
您已将所有变量声明为main
方法内部的局部变量,因此它们不在main
. 要让其他方法可以访问它们,您可以执行以下操作之一:
- pass them to the methods as parameters
- declare them as
static
class variables outside any methods, but inside the class.
- 将它们作为参数传递给方法
- 将它们声明为
static
任何方法之外的类变量,但在类内部。
回答by Mike Thomsen
You aren't passing the variables, that's the problem. You declared them in main. However, if you declare them as static variables before the main method, that will work.
你没有传递变量,这就是问题所在。你在 main.js 中声明了它们。但是,如果您在 main 方法之前将它们声明为静态变量,那将起作用。
回答by X-Pippes
You can add the variables making them static .
您可以添加使它们成为静态的变量。
public class Order {
static String clubcard;
static double clubcard_discount;
static double special_discount;
static double balance;
static double final_balance;
static int apples;
static int oranges;
static int apples_cost;
static int oranges_cost;
public static void main (String[] args) { ...
Try this and let us know.
试试这个,让我们知道。
回答by Batty
variables declared inside any method are for that method only(local scope). Either declare those methods at class level or pass them as arguments from main(as per use case, if methods being called from main).
在任何方法中声明的变量仅适用于该方法(局部范围)。在类级别声明这些方法或将它们作为参数从 main 传递(根据用例,如果从 main 调用方法)。
回答by iluvthee07
The variables you are passing are visible only inside main. The function printReceipt() is unable to see the variables because they are out of its scope of visibility.
您传递的变量仅在 main 中可见。函数 printReceipt() 无法看到变量,因为它们超出了它的可见范围。
Here you have few options you can try and the program will work:
在这里,您可以尝试几个选项,该程序将起作用:
Declare the variables as the data members of the public class Order rather than keeping them as members of the main() function (best option).
public class Order{ static String clubcard; static double clubcard_discount; static double special_discount; static double balance; static double final_balance; static int apples; static int oranges; static int apples_cost; static int oranges_cost; //main() and other functions... }
将变量声明为公共类 Order 的数据成员,而不是将它们保留为 main() 函数的成员(最佳选择)。
public class Order{ static String clubcard; static double clubcard_discount; static double special_discount; static double balance; static double final_balance; static int apples; static int oranges; static int apples_cost; static int oranges_cost; //main() and other functions... }
OR
或者
Pass the data members as arguments to the PrintReceipt() function (though this may make you function a bit messy).
public static void printReceipt(int apples, int oranges, .... .... ){
//...defining your function
}
将数据成员作为参数传递给 PrintReceipt() 函数(尽管这可能会使您的函数有点混乱)。
public static void printReceipt(int apples, int oranges, .... .... ){
//...defining your function
}
Hope this helps!
希望这可以帮助!