-1

まず、私はJavaが初めてで、初心者向けのコースを受講しています。場合によっては % 記号の使用が Eclipse で許可されていませんか? 私のコードでprintfメソッドを使用したときにパーセント記号を1つだけ使用するとエラーが発生しますが、2を使用すると問題なく動作します。それは本来の方法で実行されますが、私が抱えている別の問題は、このコードがコンソールとダイアログボックスの両方に出力され、Eclipseを最小化すると何らかの理由でダイアログボックスがEclipseに表示されないことです。私のデスクトップ。Jgraspで試してみると、これは起こりません。なぜこれが起こるのですか?

public class Project6
{
   public static void main(String[] args)
  {    
    double diamondCost;     // Cost of diamond
    double settingCost;     // Cost of setting diamond
    int numOrdered;         // Number of diamonds ordered
    double baseCost;        // settingCost + diamondCost
    double totalCost;      // Total cost of diamond including labor and tax
    double laborCost;      // Cost of jewler's labor
    double stateTax;       // State tax  
    double luxuryTax;      // Luxury tax
    double finalAmountDue;  // totalCost*numOrdered
    double stateRate=0.10;   
    double luxuryRate=0.20;  
    double laborRate=0.05;    

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the cost of the diamond");
    diamondCost = keyboard.nextDouble();
    System.out.print("Enter the cost of setting the diamond");
    settingCost = keyboard.nextDouble();
    System.out.print("Enter the amount of diamonds you want to order");
    numOrdered = keyboard.nextInt();

    baseCost = diamondCost + settingCost;
    luxuryTax = calcExtraCost(baseCost, luxuryRate);
    stateTax = calcExtraCost(baseCost, stateRate);
    laborCost = calcExtraCost(baseCost, laborRate);
    totalCost = baseCost+luxuryTax+stateTax+laborCost;
    finalAmountDue = calcExtraCost(totalCost, numOrdered);

    System.out.println("Jasmine Jewelry:TOTAL COST BREAKDOWN");
    System.out.printf("Diamond Cost: ----- $%.2f\n", diamondCost);
    System.out.printf("Setting Cost: ----- $%.2f\n", settingCost);
    System.out.printf("State Tax @ 10%%: ----- $%.2f\n", stateTax);
    System.out.printf("Luxury Tax @ 20%%: ----- $%.2f\n", luxuryTax);
    System.out.printf("Labor Cost @ 5%%: ----- $%.2f\n", laborCost);
    System.out.printf("Total Price each: ----- $%.2f\n", totalCost);
    System.out.println("Number ordered: " + numOrdered);
    System.out.printf("Final Amount Due: $%.2f", finalAmountDue);

    DecimalFormat formatter = new DecimalFormat("0.00");

           JOptionPane.showMessageDialog(null, "Jasmine Jewelry: TOTAL COST BREAKDOWN\n" + "Diamond Cost: ----- $" + 
    formatter.format(diamondCost) + "\n" +"Setting Cost: ----- $" +    formatter.format(settingCost) +  "\n" +
    "State Tax @ 10%: ----- $" + formatter.format(stateTax) + "\n" + "Luxury Tax @ 20%: ----- $" +
    formatter.format(luxuryTax) + "\n" + "Labor Cost @ 5%: ----- $" + formatter.format(laborCost) + "\n" + 
    "Total cost each: ----- $" + formatter.format(totalCost) + "\n\n" +"Number ordered: " + numOrdered 
    + "\n\nTotal Amount Due: $" + formatter.format(finalAmountDue));

    keyboard.close(); // To close scanner object
    System.exit(0);

  } // End main method

 static double calcExtraCost(double diamond, double rate) 
 {
    double extraCharge = diamond*rate;
    return extraCharge;

  } // End method calcExtraCost
} // End class Project6
4

1 に答える 1

0

まず最初に、printf は % を使用して、後で f.ex で変数で埋められる位置をマークします。君の

System.out.printf("Diamond Cost: ----- $%.2f\n", diamondCost);

% 文字の位置に変数 diamondCost を挿入します。% 文字を出力したい場合は、%% を使用する必要があります。

次に、MessageDialog の問題は、MessageDialog に親がないことに関連しているようです。

おそらく、すべてのウィンドウの上部に JOptionPane を表示する方法がそれに関連しています。

于 2013-11-05T16:51:13.877 に答える