2016-06-01 19 views
-3

私は、Javaでユーザーに従業員名と給料を尋ねるプログラムを書いています。給料が入力され、画面に表示されると、給与の前に数字の記号を付ける必要があります。どのように私はそれをやって行くだろうか?数字の前に数字記号を付けるには

ありがとうございます!

public static void main(String[] args) { 

     //use default constructor 
     //ask name 
     String name = JOptionPane.showInputDialog("What is Employee #1's name?"); 
     //System.out.println(name); 
     //ask salary 
     double salary = Double.parseDouble(JOptionPane.showInputDialog("What is the employee's salary?")); 
     //System.out.print(salary); 
     //make an employee1 value 
     Employee employee1 = new Employee(name, salary); 
     //print out 
     System.out.println(employee1); 
     System.out.printf("Employee: %s%n", employee1.getName()); 
     System.out.printf("Salary: %.3f%n", employee1.getSalary()); 
//  System.out.printf("Employee: %s", employee1.getName()); 
     JOptionPane.showMessageDialog(null, "This is the Employee's name:" + employee1.getName()); 
     JOptionPane.showMessageDialog(null, "This is the Employee's salary:" + employee1.getSalary()); 
     System.out.printf("%.3f", salary); 


     //employee 2 
     String name1 = JOptionPane.showInputDialog("What is Employee #2's name?"); 
     double salary1 = Double.parseDouble(JOptionPane.showInputDialog("What is the employee's salary?")); 
     //System.out.print(salary); 
     //make an employee2 value 
     Employee employee2 = new Employee(name1, salary1); 
     //print out 
     System.out.println(employee2); 
     System.out.printf("Employee: %s%n", employee2.getName()); 
     System.out.printf("Salary: %.3f%n", employee2.getSalary()); 
//  System.out.printf("Employee: %s", employee2.getName()); 
     JOptionPane.showMessageDialog(null, "This is the Employee's name:" + employee2.getName()); 
     JOptionPane.showMessageDialog(null, "This is the Employee's salary:" + employee2.getSalary()); 
     System.out.printf("%.3f", salary); 


    } 
} 
+0

System.out.printf( "給与:#%.3f%n"、employee2.getSalary()); ? –

+0

番号記号、それはどういう意味ですか?あなたは通貨simbolをお探しですか? –

+0

「番号記号」とは何ですか?通貨記号?あなたのprtinf文字列の一部としてそれを持っていますか? –

答えて

0

ではないが、最もダイナミックな答えは、あなたが本当にちょうどあなたが給料を印刷する直前にどんな文字を追加することができます。

JOptionPane.showMessageDialog(null, "This is the Employee's salary: #" + employee2.getSalary()); 

これはおそらく、ほとんどの場合にうまく動作します。あるいは、ドル記号を表す数字記号の代わりに、代わりに出力文で置き換えることができます。

+0

私はドル記号を意味しました!ごめんなさい!それは完璧に働いた。本当にありがとう。 –

0

https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

'+' - - Y4のY - 結果は常に符号

が含まれますあなたは常に

public static void main(String[] args) 
{ 
    double s = 47.11; 

    System.out.printf("%+.2f%n", s); 

    System.out.println(String.format("%+.2f%n", -s)); 
} 

記号を含むようにあなたの番号をフォーマットすることができます出力

+47,11 
-47,11 
1

あなたが特定の通貨をロケールが必要な場合は、https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html

を見てコードexcert:次のように

static public void displayCurrency(Locale currentLocale) { 

    Double currencyAmount = new Double(9876543.21); 
    Currency currentCurrency = Currency.getInstance(currentLocale); 
    NumberFormat currencyFormatter = 
     NumberFormat.getCurrencyInstance(currentLocale); 

    System.out.println(
     currentLocale.getDisplayName() + ", " + 
     currentCurrency.getDisplayName() + ": " + 
     currencyFormatter.format(currencyAmount)); 
} 

コードの前の行によって生成された出力は、次のとおりです。

French (France), Euro: 9 876 543,21 € 
German (Germany), Euro: 9.876.543,21 € 
English (United States), US Dollar: $9,876,543.21 
0

ジャスト追加この行はメイン関数の冒頭にあります。

String numberSign = "#"; // Whatever you desire 

この場合、必要に応じて連結(これは 'String'の場合は '+')で使用します。

JOptionPane.showMessageDialog(null, "This is the Employee's salary:" + numberSign + " " + employee2.getSalary()); 
関連する問題