Passing Variables between methods?

前端 未结 5 1810
情书的邮戳
情书的邮戳 2021-01-17 00:10

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 symbo

5条回答
  •  执念已碎
    2021-01-17 00:59

    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!

提交回复
热议问题