如何使用户输入20被程序读取为20%或0.20

时间:2017-07-03 08:09:25

标签: java

这是我要求用户输入以下内容的部分。

输入项目名称:Shirt

该商品的原价:700

标记百分比:20

销售税率:7

输出

Item to be sold : Shirt
Original price of the item: 700.00
Price after mark-up: 840.00
Sales tax: 58.80
Final price of item: 898.80

所以我的问题是如何让程序以百分比的形式读取20和7输入。

    import java.util.*;
    import javax.swing.*;

    public class lab3 {
    public static void main(String[] args) {
    JOptionPane jo=new JOptionPane();
    String item=jo.showInputDialog("Item to be sold: ");
    double Oprice=Double.parseDouble(
            jo.showInputDialog("Original price of the item: "));
    double mup=Double.parseDouble(
            jo.showInputDialog("Marked-up percentage: "));   
    double str=Double.parseDouble(
            jo.showInputDialog("Sales Tax Rate: "));


    double pamu=(Oprice*mup)+Oprice;
    double ST=pamu*str;
    double result=pamu+ST;

    String hold= "\n| Item to be sold \t: "+item+"\t |"
                +"\n| Original price of the item \t: "+Oprice+"\t |"
                +"\n| Price after mark-up \t: "+pamu+"\t |"
                +"\n| Sales Tax \t: "+ST+"\t |"
                +"\n| Final price of the item \t: "+result+"\t |";
    jo.showMessageDialog(null, new JTextArea(hold));
    }
    }

这是我的实际代码。抱歉,如果它凌乱。就像我说的还是新的

3 个答案:

答案 0 :(得分:0)

让我们使用Scanner来读取用户:

Scanner sc = new Scanner(System.in);
double percentage = 0.01 * sc.nextInt();

会做到这一点。

答案 1 :(得分:-2)

double pamu = (Oprice*(mup/100)) + Oprice; // enter code here
double ST = pamu*(str/100) ;
double result = pamu+ST ;

我认为这应该有用。

答案 2 :(得分:-3)

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name:");
String item;
keyboard.nextLine();
System.out.println("Original Price of item:");
double originalPrice;
keyboard.nextDouble();
System.out.println("Marked up percentage:");
double markedUpPercentage;
keyboard.nextDouble();
markedUpPercentage = markedUpPercentage/100; //this is what you want
System.out.println("Sales tax rate:");
double salesTaxRate;
keyboard.nextDouble();
salesTaxRate = salesTaxRate/100; //same thing here

//now output
System.out.println("Item to be sold: " + item);
System.out.println("Original price of the item: " + originalPrice);
System.out.println("Price after mark up: " + (originalPrice + originalPrice * markedUpPrice));
//similarly, do for sales tax