How do I use printf to round to tenths for this code?  {public static void main(String[] args){ double cups;double ounces; //Create ScannerScanner input = new Scanner(System.in); //ask and receive users 1st ingredient{System.out.print("Enter the first ingredient: ");String firstIngredient = input.nextLine();} //ask and receive users 2nd ingredient{System.out.print("Enter the second ingredient: ");String secondIngredient = input.nextLine();} //ask and receive users 3rd ingredient{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)  ; ??}   } }

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

 

//ask and receive users 1st ingredient

{

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

String firstIngredient = input.nextLine();

}

 

//ask and receive users 2nd ingredient

{

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

String secondIngredient = input.nextLine();

}

 

//ask and receive users 3rd ingredient

{

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

Expert Answer

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:

System.out.printf("%.1f", answer);

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

//ask and receive users 1st ingredient

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

String firstIngredient = input.nextLine();

//ask and receive users 2nd ingredient

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

String secondIngredient = input.nextLine();

//ask and receive users 3rd ingredient

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

Check out a sample Q&A here.

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

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: In a file called RemoveVowels.java, write a program that: Asks the user to enter a word of text. Pr...

A: A java program to print the word with all vowels removed is given below;Program:File name: "RemoveVo...

question_answer

Q: Apply backtracking to the problem of finding a Hamiltonian circuit in the graph below:

A: Hamilton circuit, also called as Hamilton cycle forms a close loop by visiting each node exactly onc...

question_answer

Q: Siobhan’s laptop has 8GB of RAM. Her operating system takes up 2GB, andher application programs use ...

A: It also depends that whether the graphic card is installed or not otherwise an 8 Gb of RAM itself is...

question_answer

Q: How can you check if a public computer has a USB keylogger installed

A: Universal Serial Bus (USB) keyloggers:These keyloggers are typically in the form of Universal Serial...

question_answer

Q: How would I go about writing this program?

A: Create a class and get int type data from a user, to print the number of lines on console.Use three ...

question_answer

Q: How to use the i.t. simulation viewer?

A: IT simulation viewers are the software that allows one to view and run simulations created in the si...

question_answer

Q: Translate the following pseudocode for finding the minimum value from a set of inputs.Set a Boolean ...

A: For solving this question first, we must know what pseudocode code is and what exactly question is a...

question_answer

Q: How do I solve this problem that deals with counting and subtracting calories in Java without using ...

A: Create a new class with name FitnessPlanner1.java, then take the inputs from user.Calculate the VO2,...

question_answer

Q: In C++ Create a function called checkSquares. It should take in two numbers bottom and top. It shoul...

A: The checkSquares function is created in C++ that will take two numbers as a bottom number and a top ...