Passing Variables between methods?

2020-04-01 01:24发布

问题:

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 :/

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

  }

 }

回答1:

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:

  • pass them to the methods as parameters
  • declare them as static class variables outside any methods, but inside the class.


回答2:

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.



回答3:

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.



回答4:

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



回答5:

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.

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...
    
    }
    

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 
    

    }

Hope this helps!