在方法之间传递变量? [英] Passing Variables between methods?

查看:82
本文介绍了在方法之间传递变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试为大学编写一个简单的Java程序,而我在这门Java方面是一个完全的新手.编译方法printreciept中的错误-找不到符号"时,我不断收到错误消息.我知道这就像无法访问主变量.有人可以帮忙吗?我知道如果我修复它,很可能会出现很多错误,但我宁愿从这里开始! P.S.对不起所有代码:/

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

  }

 }

推荐答案

您已在main方法内将所有变量声明为局部变量,因此它们不在main范围内.要使它们可被其他方法访问,可以执行以下操作之一:

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:

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

这篇关于在方法之间传递变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆