我的java程序输出出错

时间:2014-04-09 02:27:26

标签: java while-loop

我正在尝试编写一个java程序,但是输出有问题。

以下是输入信息时出现的错误。

Enter social security number:12345678

输入salary3000
请输入下一个安全号码或-1以退出:12345666
输入salary2122
请输入下一个安全号码或-1以退出:900000000
输入salary3000
请输入下一个安全号码或-1以退出:-1

线程“main”中的异常java.util.UnknownFormatConversionException:Conversion =':'
    在java.util.Formatter.checkText(Formatter.java:2547)
    在java.util.Formatter.parse(Formatter.java:2533)
    在java.util.Formatter.format(Formatter.java:2469)
    在java.io.PrintStream.format(PrintStream.java:970)
    在java.io.PrintStream.printf(PrintStream.java:871)
    在Salaries.output(Salaries.java:57)
    在Salaries.main(Salaries.java:19)

到目前为止,这是我的代码..

import java.util.Scanner; 公共类薪水{

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner (System.in);

    int[] ssNumbers = new int [10];
    double[] salaries = new double [10];
    double[] nSalaries = new double [10];
    int c;

    c = inputData (ssNumbers, salaries);
    raise (salaries, c);
    output (ssNumbers, salaries, nSalaries);
}

public static int inputData (int[]ssn, double[]sals){

    int c = 0;

    Scanner input = new Scanner (System.in);
    int ssNum;

    System.out.print("Enter social security number:");
    ssNum = input.nextInt();

    while (ssNum != -1) //using while loop.
    {
        ssn[c] =  ssNum;

        System.out.print("Enter salary");
        sals[c] = input.nextDouble();
        c++;

        System.out.print("Please input next security numbers or -1 to quit:");
        ssNum = input.nextInt(); 
    }
        return c;

}
public static void raise (double[] salaries, int c)

{
    double[] salaryraise = new double [10];

    for (int i = 0; i < c; i++ )
    salaryraise[i] = salaries[i]*.02;

}
public static void output (int[] ssNumbers, double[] salaries, double[] nSalary )
{
    System.out.printf("%-20s%-20s%-20s%:\n", "Social Security Number", "Salaries", "Salary After Raise");
    for (int i = 0; i < salaries.length; i++)

        System.out.printf("%d %.2f %.2f", ssNumbers[i], salaries[i], nSalary[i]);

    return;
}

}

3 个答案:

答案 0 :(得分:1)

你想删除这行中的“%:”。 (因为它不是有效的说明符)

System.out.printf("%-20s%-20s%-20s%:\n", "Social Security Number", "Salaries", "Salary After Raise");

:)

答案 1 :(得分:0)

尝试更改行:

    System.out.printf("the salary after raise is %f\n:", salaryraise);

要:

    System.out.println("the salary after raise is: " + salaryraise);

答案 2 :(得分:0)

格式末尾的%字符会出错。 应删除它,如下所示:

  System.out.printf("%-20s%-20s%-20s%:\n",
>>System.out.printf("%-20s%-20s%-20s:\n",