2016-06-01 6 views
-3

Ich schreibe ein Programm in Java, das den Benutzer nach einem Angestelltennamen und dem Gehalt fragt. Ich muss ein Nummernzeichen vor das Gehalt setzen, sobald es eingegeben und auf dem Bildschirm angezeigt wird. Irgendeine Idee, wie ich das machen würde?Wie man ein Nummernzeichen an eine Nummer anfügt

Vielen Dank im Voraus!

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 ("Gehalt: #% .3f% n", employee2.getSalary()); ? –

+0

nummer zeichen, was es bedeutet? Suchst du nach Währung simbol? –

+0

Was ist ein "Nummernzeichen"? Währungszeichen? Haben Sie es einfach als Teil Ihrer Prtinf-Saite? –

Antwort

0

Obwohl nicht die dynamischste Antwort, man kann wirklich nur hinzufügen, was Charakter rechts, bevor Sie das Gehalt drucken:

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

Dies würde wahrscheinlich für die meisten Fälle gut funktionieren. Oder wenn Sie anstelle des Zahlenzeichens ein Dollarzeichen verwenden, können Sie es stattdessen einfach in der Ausgabeanweisung ersetzen.

+0

Ich meinte ein Dollarzeichen! Es tut uns leid! Das hat perfekt funktioniert. Vielen Dank. –

0

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

'+' - - y4 y - Das Ergebnis wird immer auch ein Zeichen

Sie Ihre Nummer Format können immer ein Zeichen enthalten

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

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

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

Ausgabe

+47,11 
-47,11 
1

Wenn Sie bestimmte Währungen locale benötigen, schauen Sie sich https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html

-Code 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)); 
} 

Der Ausgang von den vorhergehenden Zeilen Code erzeugt wird, wie folgt:

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

einfach hinzufügen Diese Zeile unten am Anfang der Hauptfunktion.

dann verwenden Sie es mit verketten (es ist ein '+' für String), wo erforderlich.

z.B.

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