1个错误! “Main.java:30:错误:找不到符号”

时间:2014-09-06 23:01:13

标签: java

我似乎无法获得我为Java编程类编写的代码。

/*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?");
                ^

2 个答案:

答案 0 :(得分:4)

在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,所以我不建议这样做。

答案 1 :(得分:3)

您必须导入 JOptionPane课程;将其添加到文件顶部:

import javax.swing.JOptionPane;