Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.util.IllegalFormatPrecisionException:2_Java - Fatal编程技术网

“线程中的异常”;“主要”;java.util.IllegalFormatPrecisionException:2

“线程中的异常”;“主要”;java.util.IllegalFormatPrecisionException:2,java,Java,正在尝试运行程序时获取标题中的错误。。。但它也能编译,所以我真的很困惑。我试图检查其他帖子关于这个错误,但我无法让他们解决我的问题 我认为这与格式有关,在我打印整数之前,我有一个(String.format),但是在一篇文章中读到不这样做。有什么问题吗 import java.util.Scanner; public class prog3 { public static void main (String[] args) { /*----- // L

正在尝试运行程序时获取标题中的错误。。。但它也能编译,所以我真的很困惑。我试图检查其他帖子关于这个错误,但我无法让他们解决我的问题

我认为这与格式有关,在我打印整数之前,我有一个(String.format),但是在一篇文章中读到不这样做。有什么问题吗

import java.util.Scanner;
public class prog3 {

    public static void main (String[] args)
    {
    /*-----
    //      Liam Lockhart
    //      Program 3
    //      Calculates number of boxes and revenue
    //      Calculates number of loose parts and revenue from them
    //-----*/

    //DecimalFormat df = new DecimalFormat("#.00");

double boxRevenue;
double loosePartsRevenue;
double totalRevenue;

    //assigning variables

Scanner input = new Scanner(System.in); //Takes user input

System.out.println();

System.out.println("Please enter the amount of containers.");
int containers = input.nextInt();

System.out.println("Please enter the amount of rows in each container.");
int rows = input.nextInt();

System.out.println("Please enter the amount of columns in each container.");
int columns = input.nextInt();

int totalParts = containers * rows * columns;

int boxes = totalParts / 12;

int looseParts = totalParts % 12;

boxRevenue = boxes * 11.50;

loosePartsRevenue = looseParts * 4.75;

totalRevenue = (loosePartsRevenue)*(boxRevenue);

System.out.println();

System.out.println("Boxes:");

System.out.println("%.2d ", "boxes");

System.out.println("Loose Parts:");
您的错误在这里(它甚至没有编译):

可能您的
String.format(“%.2d”,框)
确实抛出了
java.util.IllegalFormatPrecisionException:2

因此,要么保留
字符串.format
,但将其更改为:

String formattedBoxes = String.format("%d", boxes);
System.out.println(formattedBoxes);
或者只需打印
整数即可

System.out.println(boxes);

包com.amex.csrt.crm.service

导入java.util.Scanner

公共类程序3{

public static void main(String[] args) {
    /*-----
    //      Liam Lockhart
    //      Program 3
    //      Calculates number of boxes and revenue
    //      Calculates number of loose parts and revenue from them
    //-----*/

    // DecimalFormat df = new DecimalFormat("#.00");

    double boxRevenue;
    double loosePartsRevenue;
    double totalRevenue;

    // assigning variables

    Scanner input = new Scanner(System.in); // Takes user input

    System.out.println();

    System.out.println("Please enter the amount of containers.");
    int containers = input.nextInt();

    System.out.println("Please enter the amount of rows in each container.");
    int rows = input.nextInt();

    System.out.println("Please enter the amount of columns in each container.");
    int columns = input.nextInt();

    int totalParts = containers * rows * columns;

    int boxes = totalParts / 12;

    int looseParts = totalParts % 12;

    boxRevenue = boxes * 11.50;

    loosePartsRevenue = looseParts * 4.75;

    totalRevenue = (loosePartsRevenue) * (boxRevenue);

    System.out.println();

    System.out.println("Boxes:");

    System.out.println("%.2d "+ boxes);
}
}

输入: 101210

输出:

请输入容器的数量。 请输入每个容器中的行数。 请输入每个容器中的列数

方框:
%.2d 100

这解决了它!非常感谢。
%.2d
是一个格式说明符(尽管输入错误),它不应该显示在输出中,而应该格式化输出。有关更多信息,请使用以下选项:System.out.printf(“%2d”,框);这将解决问题…您使用所需的正确格式说明符。这应该可以解决您的问题
public static void main(String[] args) {
    /*-----
    //      Liam Lockhart
    //      Program 3
    //      Calculates number of boxes and revenue
    //      Calculates number of loose parts and revenue from them
    //-----*/

    // DecimalFormat df = new DecimalFormat("#.00");

    double boxRevenue;
    double loosePartsRevenue;
    double totalRevenue;

    // assigning variables

    Scanner input = new Scanner(System.in); // Takes user input

    System.out.println();

    System.out.println("Please enter the amount of containers.");
    int containers = input.nextInt();

    System.out.println("Please enter the amount of rows in each container.");
    int rows = input.nextInt();

    System.out.println("Please enter the amount of columns in each container.");
    int columns = input.nextInt();

    int totalParts = containers * rows * columns;

    int boxes = totalParts / 12;

    int looseParts = totalParts % 12;

    boxRevenue = boxes * 11.50;

    loosePartsRevenue = looseParts * 4.75;

    totalRevenue = (loosePartsRevenue) * (boxRevenue);

    System.out.println();

    System.out.println("Boxes:");

    System.out.println("%.2d "+ boxes);
}