简体   繁体   English

如何使用来自不同方法的变量

[英]How do I use variables from different methods

This is a grocery store problem that asks the user for what they are buying, how much of it and the price.这是一个杂货店问题,询问用户他们购买的是什么、数量和价格。 The final price would then be calculated for each item (I have yet to do that for item 2 and 3)然后将为每个项目计算最终价格(我还没有为项目 2 和 3 这样做)

I want to know how to use quantity1 and price1 from my getInfo() method into my calc() method.我想知道如何将我的getInfo()方法中的quantity1price1用于我的calc()方法。

The error code I get is:我得到的错误代码是:

   assignment_2.java:62: error: cannot find symbol
          double total1 = quantity1 * price1;
                          ^
      symbol:   variable quantity1
      location: class assignment_2
    assignment_2.java:62: error: cannot find symbol
          double total1 = quantity1 * price1;
                                      ^
      symbol:   variable price1
      location: class assignment_2

My code:我的代码:

import java.util.Scanner;
public class assignment_2
{
   public static void main(String[] args)
   {
      // Call methods here
      getInfo();
      calc();
   }

   public static void getInfo()
   {
      Scanner input = new Scanner(System.in);

      > Here I'm asking the user for the name of item 1
      // Item 1
      System.out.println("Name of item 1: ");
      String item1 = input.nextLine();

      > Here I'm asking the user for the quantity of item 1
      System.out.println("Quantity of item 1: ");
      int quantity1 = input.nextInt();

      > Here I'm asking the user for the price of item 1
      System.out.println("Price of item 1: ");
      double price1 = input.nextDouble();

      // Item 2
      Scanner input2 = new Scanner(System.in);

      System.out.println("Name of item 2: ");
      String item2 = input2.nextLine();

      System.out.println("Quantity of item 2: ");
      int quantity2 = input2.nextInt();

      System.out.println("Price of item 2: ");
      double price2 = input2.nextDouble();

      // Item 3
      Scanner input3 = new Scanner(System.in);


      System.out.println("Name of item 3: ");
      String item3 = input3.nextLine();

      System.out.println("Quantity of item 3: ");
      int quantity3 = input3.nextInt();

      System.out.println("Price of item 3: ");
      double price3 = input3.nextDouble();

      // Calculations
      // double total1 = quantity1 * price1;
      // System.out.println("Item 1 total: " + total1);

      // double total2 = quantity2 * price2;
      // System.out.println("Item 2 total: " + total2);

      // double total3 = quantity3 * price3;
      // System.out.println("Item 3 total: " + total3);     
   }
   public static void calc()
   {
      double total1 = quantity1 * price1;
      System.out.println("Item 1 total: " + total1);
   }
}

Pass the param into your method将参数传递到您的方法中

public static void getInfo() {
      // Item 3
      Scanner input3 = new Scanner(System.in);
      System.out.println("Name of item 3: ");
      String item3 = input3.nextLine();

      System.out.println("Quantity of item 3: ");
      int quantity3 = input3.nextInt();

      System.out.println("Price of item 3: ");
      double price3 = input3.nextDouble();
      double total = calc(quantity, price3);
}
public static double calc(int quantity, double price)
   {
      double total = quantity * price;
      System.out.println("Item 1 total: " + total1);
      return total
   }

Your variables that in getInfo() method are available just from that method.您在getInfo()方法中的变量仅可从该方法获得。 You could pass variables as parameters to calc(int quantiy, double price) or define your variables as class visible(global) variables.您可以将变量作为参数传递给calc(int quantiy, double price)或将变量定义为 class 可见(全局)变量。 I strictly recomend to you check;我严格建议您检查; scope of variables in java . java 中的变量 scope

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

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