为什么我会收到这些错误?

发布于 2024-12-08 02:18:56 字数 4551 浏览 0 评论 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
// 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;

  }

  }

This is the output when I try to compile this code:

  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

I don't understand why I keep getting these errors. I feel like I am close to finishing the problem, but find that I need help getting over this bit of the problem.

Okay, I have made a few changes, but now I get these 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

饭团 2024-12-15 02:18:56

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

You have declared a variable totalValue inside the main function of the Inventory class. It is not available for class Cam as an instance (this) variable.

人事已非 2024-12-15 02:18:56

main.c 中缺少变量 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

Your missing declaration for the variable name in 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

Also:

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

The constructor is private.

Your are also missing static in your main method.

稚然 2024-12-15 02:18:56

这两个错误非常简单:

第一:

Inventory.java:25:错误:找不到符号
Cam 相机 = new Cam(名称, 商品编号, 商品库存, 商品价格, 总价值);
                     ^
符号:变量名
地点:类库存

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

第二:

Inventory.java:98:错误:找不到符号
      this.totalValue = 总值;
           ^
 符号:变量totalValue

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

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

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

The two errors are pretty simple:

First:

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

This sais it cannot find the variable called name, which is true. There is no name variable in the scope when you try to construct the Cam. We know you have a name variable a bit further, but that does not work, because the variable isn't in the scope of the newstatement.

Second:

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

It sais it can't find a value called totalValue in the Cam class, which is true as well. Check out the member list of Cam and you will see that there is no totalValue. I guess you want to remove it from the constructor, because you are calculating total value depending on itemStock and itemPrice.

Notes:
If you solved this, (and probably more compilation errors) you will notice that your application will compile, but not run. This is because of you forget to declare your main-method static.

If you have solved this you will notice that all the Cam objects you constructed, will contain the data you entered for the previous Cam. This is because of you are constructing the Cam before prompting the data. You started good: Declare fields for the data you want to prompt. When the user entered all the data of one Camera, construct the Camera.

窝囊感情。 2024-12-15 02:18:56

文件中有两个类

  1. 您的Inventory
  2. Cam

您需要

  1. 在 Class Inventory 中定义变量 name
  2. 变量 totalValue 在 Class Cam 中

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

There are two classes in your file

  1. Inventory
  2. Cam

You need to define

  1. variable name in Class Inventory
  2. variable totalValue in Class Cam

Also your in Inventory.java the call to Cam constructor seems to be at a wrong place, you might want to make it the last line of While loop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文