2014-09-06 2 views
0

Java 프로그래밍 클래스를 작성하기 위해 코드를 작성하지 못하는 것 같습니다.1 개의 오류! "Main.java:30 : 오류 : 심볼을 찾을 수 없습니다."

/*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); 
    } 
} 

는이 오류 얻을 : 당신은 수입JOptionPane 클래스가

Errors: 1 

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

답변

4

Java에서는 클래스를 찾을 위치를 지정해야합니다. 여기 클래스는 JOptionPane입니다. 이 패키지는 javax.swing 패키지에 있습니다.

here에서 패키지 사용에 관해 읽을 수 있습니다.

import javax.swing.JOptionPane; 
:
inputShares = javax.swing.JOptionPane.showInputDialog(null, "How many stocks do you own?"); 
  • 클래스 또는 파일의 상단에 전체 패키지를 가져 : 기본적으로, 당신은 정규화 된 클래스 이름을 사용하거나

    • 해야합니다

      또는

      import javax.swing.*; 
      

      전체 패키지를 가져 오는 후자의 방법은 일반적으로 bad practice으로 간주되므로 그렇게하지 않는 것이 좋습니다.

  • +0

    와일드 카드 가져 오기가 권장되지 않습니다. – chrylis

    3

    을; 파일 상단에 다음을 추가하십시오.

    import javax.swing.JOptionPane; 
    
    관련 문제