-1

首先,我是 Java 新手,正在学习初学者课程。在某些情况下,eclipse 中是否允许使用 % 符号?在我的代码中,当我使用 printf 方法时,如果我只使用百分之一符号,它会给我一个错误,但是当我使用 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 回答