简体   繁体   English

在方法之间传递变量?

[英]Passing Variables between methods?

So im trying to write a simple java program for college and I'm a complete newbie at this java stuff. 所以我试着为大学写一个简单的java程序,我在这个java的东西是一个完整的新手。 I keep getting an error when I compile, "error - could not find symbol" within the method printreciept. 我编译时遇到错误,“错误 - 在方法printreciept中找不到符号”。 I know that it's something like not being able to access the variables within the main. 我知道它无法访问main中的变量。 Could anyone help? 有人可以帮忙吗? I know I'll prob have alot of errors if I do fix it but I'd rather start here! 我知道如果我确实修理它会有很多错误,但我宁愿从这里开始! PS sorry for all of the code :/ 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.");

  }

 }

You have declared all your variables as local variables inside the main method, so they aren't in scope outside main . 您已将所有变量声明为main方法中的局部变量,因此它们不在main之外的范围内。 To have them accessible to other methods, you can do one of the following: 要使其他方法可以访问它们,您可以执行以下操作之一:

  • pass them to the methods as parameters 将它们作为参数传递给方法
  • declare them as static class variables outside any methods, but inside the class. 将它们声明为任何方法之外的static类变量,但在类中。

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方法之前将它们声明为静态变量,那么这将起作用。

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. 试试这个并告诉我们。

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调用方法)。

The variables you are passing are visible only inside main. 您传递的变量仅在main中可见。 The function printReceipt() is unable to see the variables because they are out of its scope of visibility. 函数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). 将变量声明为公共类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). 将数据成员作为参数传递给PrintReceipt()函数(尽管这可能会使您的函数有点混乱)。

    public static void printReceipt(int apples, int oranges, .... .... ){ public static void printReceipt(int apples,int oranges,....){

      //...defining your function 

    } }

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM