0
// 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
4

4 回答 4

1

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

于 2011-10-02T06:18:51.300 回答
1

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

// 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) {

构造函数是私有的。

static您的主要方法中也缺少您。

于 2011-10-02T06:19:31.967 回答
1

这两个错误非常简单:

第一的:

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变量,但这不起作用,因为该变量不在new语句的范围内。

第二:

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

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

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

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

于 2011-10-02T06:26:37.817 回答
0

您的文件中有两个类

  1. 存货
  2. 凸轮

你需要定义

  1. name类库存中的变量
  2. totalValue凸轮类中的变量

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

于 2011-10-02T06:22:44.043 回答