Question
Asked Oct 14, 2019
49 views

How do I use printf to round to tenths for this code?

{

public static void main(String[] args)

{

double cups;

double ounces;

//Create Scanner

Scanner input = new Scanner(System.in);

{

System.out.print("Enter the first ingredient: ");

String firstIngredient = input.nextLine();

}

{

System.out.print("Enter the second ingredient: ");

String secondIngredient = input.nextLine();

}

{

System.out.print("Enter the third ingredient: ");

String thirdIngredient = input.nextLine();

}

//Ask user for amount of cups of ingredient 1

{

System.out.print("Enter the number of cups for the first ingredient: ");

double cup1 = input.nextDouble();

}

//Ask user for amount of cups of ingredient 2

{

System.out.print("Enter the number of cups for the second ingredient: ");

double cup2 = input.nextDouble();

}

//Ask user for amount of cups of ingredient 3

{

System.out.print("Enter the number of cups for the third ingredient: ");

double cup3 = input.nextDouble();

}

{

double totalCups = cup1+cup2+cup3;

}

{

double answer = totalCups*8;

}

{

System.out.println("The recipe uses " + firstIngredient + ", " + secondIngredient + ", and " + thirdIngredient  );

}

{

//System.out.printf( "%.1f" + answer)  ; ??

}

}

}

check_circle

Step 1

Printf() method is used in java to format the string in output stream. This method takes the string in arguments along with the formatting command and results in the formatted output string. In the given java code, a recipe is made using three ingredients. The ingredients are entered as double type. Printf is to be used to round the result to tenth place.

Step 2

The printf method used in the program to round the result to tenth place is written as:

The complete java code is given below that will round the answer to tenth using printf.

import java.util.Scanner;

public class Main

{

public static void main(String[] args)

{

double cups;

double ounces;

//Create Scanner

Scanner input = new Scanner(System.in);

System.out.print("Enter the first ingredient: ");

String firstIngredient = input.nextLine();

System.out.print("Enter the second ingredient: ");

String secondIngredient = input.nextLine();

System.out.print("Enter the third ingredient: ");

String thirdIngredient = input.nextLine();

//Ask user for amount of cups of ingredient 1

System.out.print("En...

Want to see the full answer?

See Solution

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in