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メインの変数の宣言が欠落しています。

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

2 つのエラーは非常に単純です。

初め:

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

2番:

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

クラスで呼び出さtotalValueれた値が見つからないと言っていますが、これも真実です。Camのメンバー リストを確認するCamと、 が存在しないことがわかりますtotalValue。と に応じて合計値を計算しているため、コンストラクターから削除したいと思いitemStockますitemPrice

注:
これを解決すると (さらにコンパイル エラーも発生する可能性があります)、アプリケーションはコンパイルされますが、実行されないことに気付くでしょう。これは、main-methodを宣言するのを忘れているためですstatic

これを解決すると、Cam作成したすべてのオブジェクトに、前のCam. これはCam、データを求める前に を構築しているためです。うまく始めました: プロンプトを表示するデータのフィールドを宣言します。ユーザーが1 つのカメラのすべてのデータを入力したら、 Camera.

于 2011-10-02T06:26:37.817 に答える
0

ファイルには 2 つのクラスがあります

  1. 在庫
  2. カム

定義する必要があります

  1. nameクラス インベントリの変数
  2. totalValueクラス Cam の変数

また、Inventory.java では、コンストラクターへの呼び出しが間違った場所にあるようです。ループCamの最後の行にしたい場合があります。While

于 2011-10-02T06:22:44.043 に答える