货币格式化程序不适用于 System.out.format
Currency Formatter not working with System.out.format
我知道我做错了什么,只是不知道是什么。
我需要所有的美元价值都采用美国货币格式。
据我所知,将 System.out.format 与货币格式化程序一起使用存在问题。
我在 java 方面没有太多经验,可能有更好的方法来做到这一点。
输出应如下所示。
import java.util.*;
import java.lang.*;
import java.text.*;
public class Proj2
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
NumberFormat currency = NumberFormat.getCurrencyInstance();
String input = "";
int degree = 0;
int year = 0;
int age = 0;
int retirement = 0;
final int doctorate = 84396;
final int masters = 69732;
final int bachelor = 59124;
final int highSchool = 35256;
final int noDegree = 25636;
System.out.print("1 = No Degree");
System.out.print("\n");
System.out.print("2 = High school Degree");
System.out.print("\n");
System.out.print("3 = Bachelor's Degree");
System.out.print("\n");
System.out.print("4 = Masters Degree");
System.out.print("\n");
System.out.print("5 = Doctorate Degree");
System.out.print("\n");
System.out.print("\n");
System.out.print("Enter the number that corresponds to your degree: ");
input = s.nextLine();
degree = Integer.parseInt(input);
if (degree < 1 || degree > 5)
{
System.out.print("You entered an invalid menu choice.");
System.out.print("Please re-run the program and enter in a valid choice.");
System.exit(0);
}
System.out.print("\n");
System.out.print("Enter the current year: ");
input = s.nextLine();
year = Integer.parseInt(input);
System.out.print("Enter your current age: ");
input = s.nextLine();
age = Integer.parseInt(input);
System.out.print("Enter planned age of retirementd: ");
input = s.nextLine();
retirement = Integer.parseInt(input);
System.out.print("\n");
System.out.print("\n");
if (degree == 1)
{
System.out.print("Based on current statistics and no degree:");
System.out.print("\n");
System.out.print("Your annual salary will be " +currency.format(noDegree));
System.out.print("\n");
System.out.format("Retiring at age %d in %d you will make a total of %d", retirement, (year + (retirement - age)), currency.format(noDegree * (retirement - age)));
System.out.print("\n");
System.out.format("With a HighSchool degree, you can earn %d more than if you had no Degree.", currency.format(highSchool * (retirement - age)));
System.out.print("\n");
System.out.format("With a Bachelor's degree, you can earn %d more than if you had no Degree.", currency.format(bachelor * (retirement - age)));
System.out.print("\n");
System.out.format("With a Masters degree, you can earn %d more than if you had a no Degree.", currency.format(masters * (retirement - age)));
System.out.print("\n");
System.out.format("With a Doctarites degree, you can earn %d more than if you had a no Degree.", currency.format(doctorate * (retirement - age)));
System.out.print("\n");
}
else if (degree == 2)
{
System.out.print("Based on current statistics and High School degree:");
System.out.print("\n");
System.out.print("Your annual salary will be " +currency.format(highSchool));
System.out.print("\n");
System.out.format("Retiring at age %d in %d you will make a total of %d", retirement, (year + (retirement - age)), currency.format(highSchool * (retirement - age)));
System.out.print("\n");
System.out.format("That is %d more than if you had no Degree.", currency.format((highSchool * (retirement - age)) - (noDegree * (retirement - age))));
System.out.print("\n");
System.out.format("With a Bachelor's degree, you can earn %d more than if you just had a High School degree.", currency.format(bachelor * (retirement - age)));
System.out.print("\n");
System.out.format("With a Masters degree, you can earn %d more than if you just had a High School degree.", currency.format((masters * (retirement - age)) - (highSchool * (retirement - age))));
System.out.print("\n");
System.out.format("With a Doctarites degree, you can earn %d more than if you just had a High School degree.", currency.format((masters * (retirement - age)) - (highSchool * (retirement - age))));
System.out.print("\n");
}
else if (degree == 3)
{
System.out.print("Based on current statistics and Bachelor's degree:");
System.out.print("\n");
System.out.print("Your annual salary will be " +currency.format(bachelor));
System.out.print("\n");
System.out.format("Retiring at age %d in %d you will make a total of %d", retirement, (year + (retirement - age)), currency(bachelor * (retirement - age)));
System.out.print("\n");
System.out.format("That is %d more than if you had no Degree.", currency.format((bachelor * (retirement - age)) - (noDegree * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a High School degree.", currency.format((bachelor * (retirement - age)) - (highSchool * (retirement - age))));
System.out.print("\n");
System.out.format("With a Masters degree, you can earn %d more than if you just had a High School degree.", currency.format((masters * (retirement - age)) - (bachelor * (retirement - age))));
System.out.print("\n");
System.out.format("With a Doctarites degree, you can earn %d more than if you just had a High School degree.", currency.format((masters * (retirement - age)) - (bachelor * (retirement - age))));
System.out.print("\n");
}
else if (degree == 4)
{
System.out.print("Based on current statistics and Masters degree:");
System.out.print("\n");
System.out.print("Your annual salary will be " +currency.format(masters));
System.out.print("\n");
System.out.format("Retiring at age %d in %d you will make a total of %d", retirement, (year + (retirement - age)), currency.format(masters * (retirement - age)));
System.out.print("\n");
System.out.format("That is %d more than if you had no Degree.", currency.format((masters * (retirement - age)) - (noDegree * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a High School degree.", currency.format((masters * (retirement - age)) - (highSchool * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a Bachelor's degree.", currency.format((masters * (retirement - age)) - (bachelor * (retirement - age))));
System.out.print("\n");
System.out.format("With a Doctarites degree, you can earn %d more than if you just had a High School degree.", currency.format((masters * (retirement - age)) - (bachelor * (retirement - age))));
System.out.print("\n");
}
else if (degree == 5)
{
System.out.print("Based on current statistics and Doctarates degree:");
System.out.print("\n");
System.out.print("Your annual salary will be " +currency.format(doctorate));
System.out.print("\n");
System.out.format("Retiring at age %d in %d you will make a total of %d", retirement, (year + (retirement - age)), currency.format(doctorate * (retirement - age)));
System.out.print("\n");
System.out.format("That is %d more than if you had no Degree.", currency.format((doctorate * (retirement - age)) - (noDegree * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a High School degree.", currency.format((doctorate * (retirement - age)) - (highSchool * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a Bachelor's degree.", currency.format((doctorate * (retirement - age)) - (bachelor * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a Bachelor's degree.", currency.format((doctorate * (retirement - age)) - (masters * (retirement - age))));
System.out.print("\n");
}
}
}
currency.format()
的输出类型是 String
。
将 %d 更改为 %s。
例如,
改变这个:"With a HighSchool degree, you can earn %d more than if you had no Degree."
为此:"With a HighSchool degree, you can earn %s more than if you had no Degree."
我知道我做错了什么,只是不知道是什么。 我需要所有的美元价值都采用美国货币格式。 据我所知,将 System.out.format 与货币格式化程序一起使用存在问题。 我在 java 方面没有太多经验,可能有更好的方法来做到这一点。
输出应如下所示。
import java.util.*;
import java.lang.*;
import java.text.*;
public class Proj2
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
NumberFormat currency = NumberFormat.getCurrencyInstance();
String input = "";
int degree = 0;
int year = 0;
int age = 0;
int retirement = 0;
final int doctorate = 84396;
final int masters = 69732;
final int bachelor = 59124;
final int highSchool = 35256;
final int noDegree = 25636;
System.out.print("1 = No Degree");
System.out.print("\n");
System.out.print("2 = High school Degree");
System.out.print("\n");
System.out.print("3 = Bachelor's Degree");
System.out.print("\n");
System.out.print("4 = Masters Degree");
System.out.print("\n");
System.out.print("5 = Doctorate Degree");
System.out.print("\n");
System.out.print("\n");
System.out.print("Enter the number that corresponds to your degree: ");
input = s.nextLine();
degree = Integer.parseInt(input);
if (degree < 1 || degree > 5)
{
System.out.print("You entered an invalid menu choice.");
System.out.print("Please re-run the program and enter in a valid choice.");
System.exit(0);
}
System.out.print("\n");
System.out.print("Enter the current year: ");
input = s.nextLine();
year = Integer.parseInt(input);
System.out.print("Enter your current age: ");
input = s.nextLine();
age = Integer.parseInt(input);
System.out.print("Enter planned age of retirementd: ");
input = s.nextLine();
retirement = Integer.parseInt(input);
System.out.print("\n");
System.out.print("\n");
if (degree == 1)
{
System.out.print("Based on current statistics and no degree:");
System.out.print("\n");
System.out.print("Your annual salary will be " +currency.format(noDegree));
System.out.print("\n");
System.out.format("Retiring at age %d in %d you will make a total of %d", retirement, (year + (retirement - age)), currency.format(noDegree * (retirement - age)));
System.out.print("\n");
System.out.format("With a HighSchool degree, you can earn %d more than if you had no Degree.", currency.format(highSchool * (retirement - age)));
System.out.print("\n");
System.out.format("With a Bachelor's degree, you can earn %d more than if you had no Degree.", currency.format(bachelor * (retirement - age)));
System.out.print("\n");
System.out.format("With a Masters degree, you can earn %d more than if you had a no Degree.", currency.format(masters * (retirement - age)));
System.out.print("\n");
System.out.format("With a Doctarites degree, you can earn %d more than if you had a no Degree.", currency.format(doctorate * (retirement - age)));
System.out.print("\n");
}
else if (degree == 2)
{
System.out.print("Based on current statistics and High School degree:");
System.out.print("\n");
System.out.print("Your annual salary will be " +currency.format(highSchool));
System.out.print("\n");
System.out.format("Retiring at age %d in %d you will make a total of %d", retirement, (year + (retirement - age)), currency.format(highSchool * (retirement - age)));
System.out.print("\n");
System.out.format("That is %d more than if you had no Degree.", currency.format((highSchool * (retirement - age)) - (noDegree * (retirement - age))));
System.out.print("\n");
System.out.format("With a Bachelor's degree, you can earn %d more than if you just had a High School degree.", currency.format(bachelor * (retirement - age)));
System.out.print("\n");
System.out.format("With a Masters degree, you can earn %d more than if you just had a High School degree.", currency.format((masters * (retirement - age)) - (highSchool * (retirement - age))));
System.out.print("\n");
System.out.format("With a Doctarites degree, you can earn %d more than if you just had a High School degree.", currency.format((masters * (retirement - age)) - (highSchool * (retirement - age))));
System.out.print("\n");
}
else if (degree == 3)
{
System.out.print("Based on current statistics and Bachelor's degree:");
System.out.print("\n");
System.out.print("Your annual salary will be " +currency.format(bachelor));
System.out.print("\n");
System.out.format("Retiring at age %d in %d you will make a total of %d", retirement, (year + (retirement - age)), currency(bachelor * (retirement - age)));
System.out.print("\n");
System.out.format("That is %d more than if you had no Degree.", currency.format((bachelor * (retirement - age)) - (noDegree * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a High School degree.", currency.format((bachelor * (retirement - age)) - (highSchool * (retirement - age))));
System.out.print("\n");
System.out.format("With a Masters degree, you can earn %d more than if you just had a High School degree.", currency.format((masters * (retirement - age)) - (bachelor * (retirement - age))));
System.out.print("\n");
System.out.format("With a Doctarites degree, you can earn %d more than if you just had a High School degree.", currency.format((masters * (retirement - age)) - (bachelor * (retirement - age))));
System.out.print("\n");
}
else if (degree == 4)
{
System.out.print("Based on current statistics and Masters degree:");
System.out.print("\n");
System.out.print("Your annual salary will be " +currency.format(masters));
System.out.print("\n");
System.out.format("Retiring at age %d in %d you will make a total of %d", retirement, (year + (retirement - age)), currency.format(masters * (retirement - age)));
System.out.print("\n");
System.out.format("That is %d more than if you had no Degree.", currency.format((masters * (retirement - age)) - (noDegree * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a High School degree.", currency.format((masters * (retirement - age)) - (highSchool * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a Bachelor's degree.", currency.format((masters * (retirement - age)) - (bachelor * (retirement - age))));
System.out.print("\n");
System.out.format("With a Doctarites degree, you can earn %d more than if you just had a High School degree.", currency.format((masters * (retirement - age)) - (bachelor * (retirement - age))));
System.out.print("\n");
}
else if (degree == 5)
{
System.out.print("Based on current statistics and Doctarates degree:");
System.out.print("\n");
System.out.print("Your annual salary will be " +currency.format(doctorate));
System.out.print("\n");
System.out.format("Retiring at age %d in %d you will make a total of %d", retirement, (year + (retirement - age)), currency.format(doctorate * (retirement - age)));
System.out.print("\n");
System.out.format("That is %d more than if you had no Degree.", currency.format((doctorate * (retirement - age)) - (noDegree * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a High School degree.", currency.format((doctorate * (retirement - age)) - (highSchool * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a Bachelor's degree.", currency.format((doctorate * (retirement - age)) - (bachelor * (retirement - age))));
System.out.print("\n");
System.out.format("That is %d more than if you had a Bachelor's degree.", currency.format((doctorate * (retirement - age)) - (masters * (retirement - age))));
System.out.print("\n");
}
}
}
currency.format()
的输出类型是 String
。
将 %d 更改为 %s。
例如, 改变这个:"With a HighSchool degree, you can earn %d more than if you had no Degree." 为此:"With a HighSchool degree, you can earn %s more than if you had no Degree."