繁体   English   中英

主线程中的异常

[英]Exception in thread Main

我正在尝试创建一个程序来查找2个数字之间所有连续数字的总和,并且我已经创建了代码,但是我遇到了这些错误。 任何帮助或消除这些错误的方法都将倍受青睐。 谢谢

Exception in thread "main" java.util.IllegalFormatPrecisionException: 1
    at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2984)
    at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2729)
    at java.util.Formatter.parse(Formatter.java:2560)
    at java.util.Formatter.format(Formatter.java:2501)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at question15.Question15.main(Question15.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)    


     /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package question15;

/**
 *
 * @author MatthewsMacbook
 */
import java.util.Scanner;
public class Question15 {
private static double sumOfTheNumbers (double num1, double num2)
{
double sum= ((num2* (num2 + 1))/2)-((num1-1)* ((num1-1)+1)/2);
return sum;
}
    public static void main(String[] args)
    {
Scanner keyboard = new Scanner (System.in);
System.out.println("This program finds the sum of all consecutive numbers between 2 numbers");
System.out.println("please enter the first number");
double enteredInt1=keyboard.nextDouble();

System.out.println("please enter the second number");
double enteredInt2=keyboard.nextDouble();
double end= sumOfTheNumbers (enteredInt1, enteredInt2);
System.out.printf(" the sum of the numbers between %.1f"+"and : %.1d is: %.1f units", enteredInt1, enteredInt2, end);

    }

}

您的打印对帐单中有错字。 它的

System.out.printf(" the sum of the numbers between %.1f"+"and : %.1d is: %.1f units", enteredInt1, enteredInt2, end);

它应该是:

System.out.printf(" the sum of the numbers between %.1f"+"and : %.1f is: %.1f units", enteredInt1, enteredInt2, end);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM