How can you add a new line at the the end of of the output string in Java. The output is correct but it is requiring a new line I have done the System.out.println() after the 3rd method call, but it is still wrong. Here is the code with out the print line at the end and the results showing. The second picture is with the print line and showing the what is wrong as well import java.util.Scanner; public class LabProgram { //method drivingCost definition public static double drivingCost(double drivenMiles,double milesPerGallon,double dollarsPerGallon) { double yourValue; yourValue = (drivenMiles/milesPerGallon)*dollarsPerGallon; System.out.printf("%.2f ", yourValue); return yourValue; } public static void main(String[] args) { double milesPerGallon; double dollarsPerGallon; Scanner sc = new Scanner(System.in); milesPerGallon = sc.nextDouble(); dollarsPerGallon = sc.nextDouble(); drivingCost(10, milesPerGallon, dollarsPerGallon); drivingCost(50, milesPerGallon, dollarsPerGallon); drivingCost(400, milesPerGallon, dollarsPerGallon); } }
How can you add a new line at the the end of of the output string in Java. The output is correct but it is requiring a new line I have done the System.out.println() after the 3rd method call, but it is still wrong. Here is the code with out the print line at the end and the results showing. The second picture is with the print line and showing the what is wrong as well
import java.util.Scanner;
public class LabProgram {
//method drivingCost definition
public static double drivingCost(double drivenMiles,double milesPerGallon,double dollarsPerGallon)
{
double yourValue;
yourValue = (drivenMiles/milesPerGallon)*dollarsPerGallon;
System.out.printf("%.2f ", yourValue);
return yourValue;
}
public static void main(String[] args) {
double milesPerGallon;
double dollarsPerGallon;
Scanner sc = new Scanner(System.in);
milesPerGallon = sc.nextDouble();
dollarsPerGallon = sc.nextDouble();
drivingCost(10, milesPerGallon, dollarsPerGallon);
drivingCost(50, milesPerGallon, dollarsPerGallon);
drivingCost(400, milesPerGallon, dollarsPerGallon);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images