java - 1 个错误! "Main.java:30: error: cannot find symbol"

标签 java

我似乎无法让我为 Java 编程类(class)编写的代码正常工作。

/*Coby Bushong
Java Programming
Professor Lehman
August 9th 2014*/

import java.util.Scanner; // Needed for the Scanner class

/*This program will calculate the commission of a stock broker
at a 2% commission given the number of shares at a contant rate.*/

public class StockCommission 
{
   public static void main(String[] args)
   {
      //Constants
      final double STOCK_VALUE = 21.77;       //price per share
      final double BROKER_COMMISSION = 0.02;  //broker commission rate

      //variables
      double shares;     //To hold share amount
      double shareValue; //To hold total price of shares
      double commission; //To hold commission
      double total;      //To hold total cost

      //create scanner for keyboard input
      Scanner Keyboard = new Scanner(System.in);

      //creates dialog box for user input
      String inputShares;
      inputShares = JOptionPane.showInputDialog(null, "How many stocks do you own?");
      shares = Integer.parseInt(inputShares);

      //calculate total
      shareValue = shares * STOCK_VALUE;

      //calculate commission
      commission = shareValue * BROKER_COMMISSION;

      //calculate total
      total = commission + shareValue;

      //display results
      System.out.println("Shares Owned:" + shares);
      System.out.println("Value of Shares:         $" + shareValue);
      System.out.println("Comission:         $" + commission);
      System.out.println("Total:       $" + total);
   }
}

我收到此错误:

Errors: 1

StockCommission.java:30: error: cannot find symbol
  inputShares = JOptionPane.showInputDialog(null, "How many stocks do you own?");
                ^

最佳答案

在 Java 中,您必须指定要在哪里查找类。在这里,该类是JOptionPane。它位于 javax.swing 包中。

您可以在here中阅读有关使用包的信息。 。基本上,你必须要么

  • 使用完全限定的类名:

    inputShares = javax.swing.JOptionPane.showInputDialog(null, "How many stocks do you own?");
    
  • 在文件顶部导入类或整个包:

    import javax.swing.JOptionPane;
    

    import javax.swing.*;
    

    一般考虑后一种导入整个包的方法bad practice ,所以我不建议这样做。

关于java - 1 个错误! "Main.java:30: error: cannot find symbol",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25705566/

相关文章:

java - 将 @RequestParam 作为列表是不可能的吗?

java - 如何进行 Vaadin 按钮鼠标悬停

java - 在DB中删除或插入后如何触发Jtable更新?

java - 我们可以将csv文件写入S3而不在spring boot中在本地创建文件吗?

java - 为什么我的 Android 应用程序中的计算(Eclipse)出现错误?

java - 变量的内存分配和生命周期

java - 尝试在 Java 中的数组上使用 .stream() 时出现“找不到符号错误”

java.lang.NullPointerException 在 org.openqa.selenium.support.pagefactory.findElement(DefaultElementLocator.java :69)

java - 我们可以将应用程序上下文 get bean 类型转换为 Class<?>

java - 在 Java 和 Android 中通过套接字获取服务器响应