简体   繁体   中英

Exception in thread Main

i am trying to create a program to find the sum of all consecutive numbers between 2 numbers and I have created a code but I am getting these errors. any assistance or ways to get rid of these errors would be much apreciated. thanks

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

    }

}

You have a typo in your print statement. It's

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

It should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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