As I was coding this and finally got it working, yes I am new with Java programming, and programming in general, I was wondering if theres not a better way of coding this, especially with the variables? Maybe not, just curious.
// 2.35: Daily Driving Cost Calculator // Calculates your daily driving cost import java.util.Scanner; // Imports the class Scanner public class DrivingCost { public static void main( String[] args ) { //Creating scanner to accept user input Scanner input = new Scanner( System.in ); double miles; //Obtains users total miles driven double gasprice; //Obtains users cost per gallon of gas double milespergal; //Obtains users miles per gallon double parkingfees; //Obtains users daily parking fees double tolls; //Obtains users daily driving tolls double gallonsused; // Holds gallons used double gasmoney; // Holds Gas Money amt double total; // Holds total of all sums System.out.print( "Enter total miles driven daily: "); //Prompt miles = input.nextDouble(); //Captures User Input System.out.print( "Enter cost per gallon of gas: "); //Prompt gasprice = input.nextDouble(); //Captures User Input System.out.print( "Enter your average miles per gallon: "); //Prompt milespergal = input.nextDouble(); //Captures User Input System.out.print( "Enter daily parking fees: "); //Prompt parkingfees = input.nextDouble(); //Captures User Input System.out.print( "Enter any tolls per day: "); //Prompt tolls = input.nextDouble(); //Captures User Input gallonsused = miles / milespergal; //Calculates total gallons of gas used gasmoney = gallonsused * gasprice; total = gasmoney + parkingfees + tolls; System.out.printf( "Your total daily driving cost is %.2f\n", total ); //Displays total amt } //End main method } //End class DrivingCost


LinkBack URL
About LinkBacks
Reply With Quote
Now that that's out of the way...java is an object-oriented language. Not writing programs in an object oriented fashion means you loose the power (and fun) associated with OOP. For something this simple it may seem overkill, but what if you want to build this into something more...placing everything in the main method can get ugly real quick.