gpt4 book ai didi

java - 未知格式转换异常错误?

转载 作者:行者123 更新时间:2023-12-02 02:34:31 25 4
gpt4 key购买 nike

我正在尝试制作一张 table 。我希望数字向左对齐。我是格式化新手,不确定为什么会收到错误。

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

}
}
}

最佳答案

给你:

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()是一个可变参数函数,它需要多个参数而不是一个!

关于java - 未知格式转换异常错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46590425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com