我似乎无法让我为 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/