-
Looking for help on two codes:
Hello everyone, I am new to the forum, nice to meet you!
I am new to java programming and I am still learning the ropes. I joined here hopefully to get good feedback.
What I have here are two codes I need help with. They are pretty much done except for a small error in each one. I was hoping
someone could help me understand where I am missing what. Please understand I am new to this and don't yet understand all
the terminology yet.
Any help would be appreciated!
-
Re: Looking for help on two codes:
After the first time through the loop, the mpgtank and totalmpg should be different, for some reason I can't get it to change:
Code Java:
import java.util.Scanner;
public class Lab5a {
public static void main( String args[] ) {
Scanner input = new Scanner( System.in );
double milesdriven = 1.1;
double gallonsused = 1.1;
double mpgtank = 1.1;
double totalmpg = 1.1;
System.out.print("Enter miles since tank was full (-999 to quit) ");
milesdriven = input.nextDouble();
while(milesdriven!=-999)
{
System.out.print( "Enter gallons used in refilling tank:" );
gallonsused = input.nextDouble();
mpgtank = milesdriven / gallonsused;
totalmpg = milesdriven / gallonsused;
System.out.printf( "MPG this tankful: %1.2f ", mpgtank );
System.out.println();
System.out.printf("Total MPG: %1.2f", totalmpg);
System.out.println();
System.out.print("Enter miles since tank was full (-999 to quit) ");
milesdriven = input.nextDouble();
{
System.out.print( "Enter gallons used in refilling tank:" );
gallonsused = input.nextDouble();
mpgtank = milesdriven / gallonsused;
totalmpg = milesdriven / gallonsused;
System.out.printf( "MPG this tankful: %1.2f ", mpgtank );
System.out.println();
System.out.printf("Total MPG: %1.2f", totalmpg);
System.out.println();
System.out.print("Enter miles since tank was full (-999 to quit) ");
milesdriven = input.nextDouble();
while(milesdriven!=-999)
{
System.out.print( "Enter gallons used in refilling tank:" );
gallonsused = input.nextDouble();
mpgtank = milesdriven / gallonsused;
totalmpg = milesdriven / gallonsused;
System.out.printf( "MPG this tankful: %1.2f ", mpgtank );
System.out.println();
System.out.printf("Total MPG: %1.2f", totalmpg);
System.out.println();
System.out.println("Press any key to continue . . .");
}
}}}}
-
Re: Looking for help on two codes:
This second code is an Easter Egg hunt game. The user is supposed to have the ability to enter Q to quit the game, I do no understand how to put this in and where:
Code Java:
import java.util.Scanner;
public class EggHunt{
public static void main(String args[]){
Scanner input = new Scanner( System.in );
String gender;
String type;
int countboys=0;
int countgirls=0;
int point=0;
System.out.println("Enter gender of competitor: ");
gender = input.next();
System.out.println("Enter egg type: A, B, or C");
type = input.next();
if(type.equalsIgnoreCase("A"))
point=5;
else if(type.equalsIgnoreCase("B"))
point=10;
else
point=30;
if(gender.equalsIgnoreCase("M"))
countboys+=point;
else
countgirls+=point;
System.out.println("Enter gender of competitor: ");
gender = input.next();
System.out.println("Enter egg type: A, B, or C");
type = input.next();
if(type.equalsIgnoreCase("A"))
point=5;
else if(type.equalsIgnoreCase("B"))
point=10;
else
point=30;
if(gender.equalsIgnoreCase("G"))
countgirls+=point;
else
countboys+=point;
System.out.println();
System.out.println("The boys scored "+countboys+" and girls scored "+ countgirls);
if(countboys>countgirls)
System.out.println("Boys are the champions");
else if(countgirls>countboys)
System.out.println("Girls are the champions");
else
System.out.println("There is a tie between girls and boys");
} }
-
Re: Looking for help on two codes:
Not sure I follow your code, but shouldn't you be totalling your miles driven somewhere in there?
-
Re: Looking for help on two codes:
-
Re: Looking for help on two codes:
I believe is just supposed to be "miles so far" this is what the assignment says
"Develop a Java application that will allow input of the miles driven and gallons used for each thankful. The program should calculate and display the miles per gallons obtained for each tankful and print the combined miles per gallon obtained for all tankfuls so far, each time a miles driven and gallons pair is entered."
-
Re: Looking for help on two codes:
sorry I did post this question on another forum yesterday, I found this one this morning and liked the layout a little better. Sorry for not making
clear. Just trying learn java programming, and I looked for different sources.
-
Re: Looking for help on two codes:
Can you post the output from the first program and explain what is wrong with it?
-
Re: Looking for help on two codes:
This is the output from Eclipse:
Enter miles since tank was full (-999 to quit) 200.7
Enter gallons used in refilling tank:16.3
MPG this tankful: 12.31
Total MPG: 12.31
Enter miles since tank was full (-999 to quit) 120
Enter gallons used in refilling tank:5
MPG this tankful: 24.00
Total MPG: 24.00
Enter miles since tank was full (-999 to quit)
After the first time through the loop, the mpgtank and totalmpg should be different. I may not have the loop in place at all.
-
Re: Looking for help on two codes:
Quote:
After the first time through the loop, the mpgtank and totalmpg should be different.
Where in your code do you change the values of the variables?
Do you forget to reset one of them?
Or do you reset both of them?
What are the items that go into computing each of them?
-
Re: Looking for help on two codes:
the user is supposed to enter a new set of numbers and have them recalculated based on the old "mpg this tank full" and "total mpg"
The first set is correct, but those two outputs are supposed to change when u enter in a new set of numbers
-
Re: Looking for help on two codes:
What are the items/numbers that go into computing the values of each of them?
-
Re: Looking for help on two codes:
i am not sure what u are asking me here, i'm sorry
-
Re: Looking for help on two codes:
An example: to compute how fast a car is traveling you need the distance traveled and how long it took to travel that distance.
What values/numbers do you need to compute each of the following:
mpgtank
totalmpg
-
Re: Looking for help on two codes:
should be total miles and total gallons
-
Re: Looking for help on two codes:
Quote:
total miles and total gallons
Then you have what you need to compute the values.
What is the difference between the computations for the two values to display? Do they both use the same values? Or are there different values for each?
-
Re: Looking for help on two codes:
there will be different values for each, but whenever i try to put that in the code, eclipse will not take it. I just can't figure out where i would put
totalmpg = totalgallons/totalmiles
totaltank = totalgallons/totalmiles
where does this go in my current code??
-
Re: Looking for help on two codes:
The equations can not use the same variables if you want to compute different values. You need to have separate variables for the tank and for the total.
-
Re: Looking for help on two codes:
could u give me an example of what you mean? I am sorry i am just not up on the terms yet
-
Re: Looking for help on two codes:
Just write in English what the steps are you want the program to do. I'll start it:
begin loop
Ask user to enter miles driven this tank
read milesdriven
ask user to enter gallons to fill the tank
read gallons
....
end loop
Now fill in the steps where the ... to get the two numbers you need to print out?
-
Re: Looking for help on two codes:
ok so i shouldn't have to repeat this code:
Code Java:
System.out.print("Enter miles since tank was full (-999 to quit) ");
milesdriven = input.nextDouble();
while(milesdriven!=-999)
{
System.out.print( "Enter gallons used in refilling tank:" );
gallonsused = input.nextDouble();
mpgtank = milesdriven / gallonsused;
totalmpg = totalmiles / totalgallons;
System.out.printf( "MPG this tankful: %1.2f ", mpgtank );
System.out.println();
System.out.printf("Total MPG: %1.2f", totalmpg);
System.out.println();
-
Re: Looking for help on two codes:
Yes, you should be able to do what you need in one loop.
-
Re: Looking for help on two codes:
thank you very much for all your help, i will keep working at it
-
Re: Looking for help on two codes:
Ok. Come on back if you have more problems/questions.
-
Re: Looking for help on two codes:
thank you again for your help norm, i got a 100% on that assignment based on all your help! sending more thank yous!