【问题标题】:Why am I getting these errors? [closed]为什么我会收到这些错误? [关闭]
【发布时间】:2011-11-29 07:22:37
【问题描述】:
// Inventory.java part 1
// this program is to calculate the value of the inventory of the Electronics Department's cameras

import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class Inventory
{
public void main(String[] args)
{
   // create Scanner to obtain input from the command window
   Scanner input = new Scanner (System.in);

   int itemNumber; // first number to multiply
   int itemStock; // second number to multiply
   double itemPrice; //
   double totalValue; // product of number1 and number2



while(true){   // infinite loop
           // make new Camera object
Cam camera = new Cam(name, itemNumber, itemStock, itemPrice, totalValue);

   System.out.print("Enter Department name: "); //prompt
   String itemDept = input.nextLine(); // read name from user

    if(itemDept.equals("stop"))  // exit the loop
        break;


   while(true){
   System.out.print("Enter item name: "); // prompt
   String name = input.nextLine(); // read first number from user
   input.nextLine();
      if(name != ("camera"))
         System.out.print("Enter valid item name:"); // prompt
         name = input.nextLine(); // read first number from user
         input.nextLine();
      break;


   }

   System.out.print("Enter number of items on hand: "); // prompt
   itemStock = input.nextInt(); // read first number from user
   input.nextLine();
      while( itemStock <= -1){
         System.out.print("Enter positive number of items on hand:"); // prompt
         itemStock = input.nextInt(); // read first number from user
         input.nextLine();
         } /* while statement with the condition that negative numbers are entered
         user is prompted to enter a positive number */

   System.out.print("Enter item Price: "); // prompt
   itemPrice = input.nextDouble(); // read second number from user
   input.nextLine();
      while( itemPrice <= -1){
        System.out.print("Enter positive number for item price:"); // prompt
        itemPrice = input.nextDouble(); // read first number from user
        input.nextLine();
         } /* while statement with the condition that negative numbers are entered
         user is prompted to enter a positive number */




   totalValue = itemStock * itemPrice; // multiply numbers

   System.out.println("Department name:" + itemDept); // display Department name
   System.out.println("Item number: " + camera.getItemNumber()); //display Item number
   System.out.println("Product name:" + camera.getName()); // display the item
   System.out.println("Quantity: " + camera.getItemStock());
   System.out.println("Price per unit" + camera.getItemPrice());
   System.out.printf("Total value is: $%.2f\n", camera.getTotalValue()); // display product

   } // end while method

} // end method main

}/* end class Inventory */

class Cam{

   private String name;
   private int itemNumber;
   private int itemStock;
   private double itemPrice;
   private String deptName;

   private Cam(String name, int itemNumber, int itemStock, double itemPrice, double totalValue) {
  this.name = name;
  this.itemNumber = itemNumber;
  this.itemStock = itemStock;
  this.itemPrice = itemPrice;
  this.totalValue = totalValue;
 }

 public String getName(){
  return name;
  }


 public double getTotalValue(){
  return itemStock * itemPrice;
  }

  public int getItemNumber(){
  return itemNumber;

  }

  public int getItemStock(){
  return itemStock;

  }

  public double getItemPrice(){
  return itemPrice;

  }

  }

这是我尝试编译这段代码时的输出:

  C:\Java>javac Inventory.java
    Inventory.java:25: error: cannot find symbol
    Cam camera = new Cam(name, itemNumber, itemStock, itemPrice, totalValue);
                         ^
symbol:   variable name
location: class Inventory
Inventory.java:98: error: cannot find symbol
      this.totalValue = totalValue;
          ^
 symbol: variable totalValue
2 errors

我不明白为什么我不断收到这些错误。我觉得我快要解决这个问题了,但发现我需要帮助来解决这个问题。

好的,我做了一些更改,但现在出现以下错误:

    C:\Java>javac Inventory.java
    Inventory.java:68: error: variable itemNumber might not have been initialized
    Cam camera = new Cam(name, itemNumber, itemStock, itemPrice, totalValue);

                                  ^
    Inventory.java:68: error: variable totalValue might not have been initialized
    Cam camera = new Cam(name, itemNumber, itemStock, itemPrice, totalValue);
                                                                    ^
    2 errors

【问题讨论】:

  • 请检查开始和结束花括号 { .. }
  • 它明确表示itemNumber 和totalValue 可能未初始化!为什么不初始化这些变量?猜猜你也必须初始化itemPrice 和totalValue ..

标签: java find symbols inventory


【解决方案1】:

您已经在 Inventory 类的主函数中声明了一个变量 totalValue。它不适用于类 Cam 作为实例 (this) 变量。

【讨论】:

    【解决方案2】:

    您在 main 中缺少变量 name 的声明。

    // create Scanner to obtain input from the command window
    Scanner input = new Scanner (System.in);
    
    String name; //missing declaration
    int itemNumber; // first number to multiply
    int itemStock; // second number to multiply
    double itemPrice; //
    double totalValue; // product of number1 and number2
    

    还有:

    class Cam{
    
        private String name;
        private int itemNumber;
        private int itemStock;
        private double itemPrice;
        private String deptName;
        private double totalValue; //missing field
    
        private Cam(String name, int itemNumber, int itemStock, double itemPrice, double totalValue) {
    

    构造函数是私有的。

    您的 main 方法中也缺少 static。

    【讨论】:

      【解决方案3】:

      这两个错误很简单:

      第一:

      Inventory.java:25: error: cannot find symbol
      Cam camera = new Cam(name, itemNumber, itemStock, itemPrice, totalValue);
                           ^
      symbol:   variable name
      location: class Inventory
      

      这表示它找不到名为name 的变量,这是真的。当您尝试构造Cam 时,范围内没有名称变量。我们知道您还有一个 name 变量,但这不起作用,因为该变量不在 newstatement 的范围内。

      第二:

      Inventory.java:98: error: cannot find symbol
            this.totalValue = totalValue;
                 ^
       symbol: variable totalValue
      

      它说它在Cam 类中找不到名为totalValue 的值,这也是正确的。查看Cam的成员列表,你会发现没有totalValue。我猜您想将其从构造函数中删除,因为您正在根据 itemStock 和 itemPrice 计算总值。

      注意事项:
      如果你解决了这个问题(可能还有更多的编译错误),你会注意到你的应用程序会编译,但不会运行。这是因为你忘记声明你的main-方法static。

      如果您解决了这个问题,您会注意到您构建的所有Cam 对象都将包含您为之前的Cam 输入的数据。这是因为您在提示数据之前正在构造Cam。你开始很好:为你想要提示的数据声明字段。当用户输入一个相机的所有数据后,构造Camera。

      【讨论】:

      • 那么我将如何修复第一个错误。我知道你说这很简单,但我似乎无法让它发挥作用。我已经尝试移动代码行的位置,但无法弄清楚我应该在哪里放置代码
      • 在您尝试构建凸轮之前,必须有一个名为 name 的变量。
      【解决方案4】:

      你的文件中有两个类

      1. 库存
      2. 摄像头

      你需要定义

      1. 类清单中的变量 name
      2. Cam 类中的变量totalValue

      另外,您在 Inventory.java 中对 Cam 构造函数的调用似乎在错误的位置,您可能希望将其设为 While 循环的最后一行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-07
        • 2013-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多