Uknown格式转换异常错误?

时间:2017-10-05 16:20:13

标签: java format

我正在努力制作一张桌子。我希望数字在左边是合理的。我是格式化的新手,不知道为什么我会收到错误。

public class Prog122h
{
   public static void main (String[] args)
   {   
      System.out.println("Number     Square     Square Root     Cube     4th Root"); // heading

      for (int number = 1; number <= 20; number++) // repeats finding the square and square root of the number until 40. increments number by 1
      { 
         //finds square using math class
         int square= (int) Math.pow( number, 2);

         // finds square root of the number using math class 
         double squareRoot= (Math.sqrt(number));

         squareRoot= Math.round (squareRoot* 10000.0) / 10000.0; 

         //finds the cube of the number 
         int cube= (int) Math.pow( number, 3);

         //finds the 4th root of the number 
         double fourthRoot= Math.pow( number, 1.0/4 );
         fourthRoot= Math.round (fourthRoot* 10000.0) / 10000.0; 

         //output of table 
         System.out.format("%-,  "+number+"            %-,  "+square+"            %-, "+squareRoot+"     %-,  " +cube+"     %-,  "+fourthRoot);

      }   
   }
} 

1 个答案:

答案 0 :(得分:2)

你走了:

public class Prog122h {
    public static void main(String[] args) {
        System.out.format("%-10s%-10s%-10s%-10s%-10s", "Number", "Square", "Square", "Root Cube", "4th Root"); // heading

        for (int number = 1; number <= 20; number++) // repeats finding the square and square root of the number until 40. increments number by 1
        {
            // finds square using math class
            int square = (int) Math.pow(number, 2);

            // finds square root of the number using math class
            double squareRoot = (Math.sqrt(number));

            squareRoot = Math.round(squareRoot * 10000.0) / 10000.0;

            // finds the cube of the number
            int cube = (int) Math.pow(number, 3);

            // finds the 4th root of the number
            double fourthRoot = Math.pow(number, 1.0 / 4);
            fourthRoot = Math.round(fourthRoot * 10000.0) / 10000.0;

            // output of table
            System.out.format("\n%-10s%-10s%-10s%-10s%-10s", number, square, squareRoot, cube, fourthRoot, fourthRoot);

        }
    }
}

PrintStream.format()是一个可变函数,它需要多个参数而不是一个!