![](/img/trans.png)
[英]Could not find or load main class and Main.java:5: error: cannot find symbol vs code
[英]1 error! “Main.java:30: error: cannot find symbol”
我似乎無法讓他為我的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?");
^
在Java中,您必須指定要在哪里找到一個類。 在這里,該類是JOptionPane
。 它位於javax.swing
包中。
您可以在此處閱讀有關使用軟件包的信息 。 基本上,您必須要么
使用完全限定的類名:
inputShares = javax.swing.JOptionPane.showInputDialog(null, "How many stocks do you own?");
將類或整個包導入文件頂部:
import javax.swing.JOptionPane;
要么
import javax.swing.*;
導入整個軟件包的后一種方法通常被認為是不好的做法 ,因此我不建議您這樣做。
您必須導入 JOptionPane
類。 在文件頂部添加以下內容:
import javax.swing.JOptionPane;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.