1. Member
Join Date
Dec 2010
Posts
31
Rep Power
0

Loop problems

I am trying to get this program to work but the result just doesnt show...im supposed to be practicing loops for my midterm exams but i cant seem to make this work,there are no errors but when i input -1 it doesnt directly exits the program and the overall average doesnt show right...
can anyone help me or point my mistake in this one?

Java Code:
```    public static void main(String [] args)throws Exception {

double gallons,miles;
String gal,mil;

do{

System.out.println("Enter the gallons used(-1 to end): ");
gallons=Integer.parseInt(gal);
System.out.println("Enter the miles: ");
miles=Integer.parseInt(mil);
System.out.println("The miles/gallons for this tank was: " + (gallons/miles));

}
while (gallons !=-1);
System.out.println("The overall miles/gallons is: " + gallons/miles);
}

}```

2. There is quite a bit that you need to fix. First off, I don't see where you are gathering a total. So outside of the loop it will be (-1/miles). You also don't check if the input is -1. Maybe an if to check for the -1 after getting input, and if it is -1, break out.

Java Code:
```get input
if(condition)
break;
end if```

3. Member
Join Date
Dec 2010
Posts
31
Rep Power
0
i will be gathering a total because it would ask me more than one then when i try to exit the program using (-1) i would be able to display the TOTAL...for example:

{
Enter the gallons used (-1 to end): 12.8
Enter the miles driven: 287
The miles/gallons for this tank was: 22.421875

Enter the gallons used (-1 to end): 10.3
Enter the miles driven: 200
The miles/gallons for this tank was: 19.417475

Enter the gallons used (-1 to end): -1

The overall average miles/gallon was: 21.601423
}

i dont get it if i had to use IF statements :\ lol im really confused bout this

4. Well think about what you want it to do, and what it does. Look at the code in each line and figure out what is happening at each step. What happens when the user enters -1? does it have anywhere in the code which forces it to exit the loop? You also aren't keeping a total of of the input being put in as you are going. If you don't the final result will just give you the last entered data. You need to carefully design this to get it working correctly. Try writing out what you want this to do in plain english at each step through the code.

5. Member
Join Date
Dec 2010
Posts
31
Rep Power
0
yea ill try..lemme work on that :) i hope id get this by tonight and ill reply soon as i get better results or make this work

6. Member
Join Date
Dec 2010
Posts
31
Rep Power
0
i still dont get it :( any more tips?

7. Member
Join Date
Dec 2010
Posts
31
Rep Power
0
Java Code:
```    public static void main(String[]args)throws Exception{

double nm1,nm2;
String num1,num2;

do{

System.out.println("Enter 1st number: ");
nm1=Integer.parseInt(num1);
if (nm1==-1)
System.out.println("Exits Program: ");
else

System.out.println("Enter 2nd number: ");
nm2=Integer.parseInt(num2);
System.out.println("total is: " +(nm1+nm2));

}

while(nm1 != -1);
System.out.println("Exits: " );

}}```
i made this dummy and somehow i still cant get the overall sum of nm1 and nm2 :\

8. You should really be using more meaningful names. Perhaps totalGas, totalMileage, gas, and mileage. Better names make easier to read code.

Ok, so first things first; you get user input. What do you want to do with said input? Is there a chance that anything special should happen based on the input? Or should it just use the input for calculations no matter what? What happens if they use the sentinel as input? What does the statement
Java Code:
`System.out.println("Exits Program: ");`
do? Does it exit? Why not? Do you want it to exit the program? Why?

How can you get out of a loop? Are there any keywords that sound like perhaps that may be what they do?

After getting the inputs and not receiving the sentinel(-1), what should you do with the inputs? Should you increase the total? How can you add some amount to a variable?

Java Code:
```int x = 5;
???
System.out.println(x);```
What statement(besides 'x=10;') can you use to make the 3rd line print out 10?

Use this to add to the totalGas and totalMileage with each pass through the loop. This is just some food for thought. You need to really be thinking about all the problems that can occur, and what you want to code to do. All details/situations must be handled. The computer does what you say to do; not what you think/want it to do. The more exact the instructions, the more it will behave how you want.

9. Member
Join Date
Dec 2010
Posts
31
Rep Power
0
what i am trying to do here is get the total of everything after i exit/input the sentinel loop...i cant seem to get it,and ive been working with this for the whole night...

i know this is just simple for you guys but im still new to java :\
any more simpler thoughts on this one

10. Member
Join Date
Dec 2010
Posts
31
Rep Power
0

Help

Java Code:
```    public static void main(String [] args)throws Exception {

double gallons,mile,mpg;
String gal,mil;

do{

System.out.println("Enter the gallons used(-1 to end): ");
gallons=Integer.parseInt(gal);

if (gallons!=-1)
{
System.out.println("Enter the miles: ");
mile=Integer.parseInt(mil);
System.out.println("The miles/gallons for this tank was: "  + (mile/gallons));

}

else
{

System.out.println("The overall gallon is: " );

}}
while(gallons!=1);

}}```
all i need now is to print the combined miles per gallon obtained for all tankfuls.
Java Code:
```  else
{

System.out.println("The overall gallon is: " );

}}```
if i put (mile/gallons) i get error on MILE..anyone please?

11. Member
Join Date
Aug 2011
Posts
95
Rep Power
0
This does not solve your problem, but it will get you past the compile error:
Java Code:
`double gallons, mile = 0, mpg;`
sunde887 gave several really useful and insightful hints above. You might want to read them again, and think about them.

12. Member
Join Date
Dec 2010
Posts
31
Rep Power
0
its not that i didnt read what sunde wrote but its just that i didnt understand what he meant about everything...
sigh all i want to do is understand how to total everything up and loops by not making it too much complicated :\

13. Member
Join Date
Aug 2011
Posts
95
Rep Power
0
Here is a simple example that shows some of the main concepts:
Java Code:
```public class Rocks {
public static void main(String[] args) {

int totalWeightOfAllRocks = 0;
for (int weightOfThisRock : new int[] {6,4,3,8,3,7,4,5}) {
System.out.println("This rock weighs " + weightOfThisRock + " lbs.");
totalWeightOfAllRocks = totalWeightOfAllRocks + weightOfThisRock;
}
System.out.println("We had, in total " + totalWeightOfAllRocks + " pounds of rocks.");
}
}```
Here's the output:
Java Code:
```This rock weighs 6 lbs.
This rock weighs 4 lbs.
This rock weighs 3 lbs.
This rock weighs 8 lbs.
This rock weighs 3 lbs.
This rock weighs 7 lbs.
This rock weighs 4 lbs.
This rock weighs 5 lbs.
We had, in total 40 pounds of rocks.```

14. Member
Join Date
Aug 2011
Posts
95
Rep Power
0
This part is really important:
Originally Posted by sunde887
Java Code:
```int x = 5;
???
System.out.println(x);```
What statement(besides 'x=10;') can you use to make the 3rd line print out 10?

15. Member
Join Date
Dec 2010
Posts
31
Rep Power
0
thanks,ill get to it with that example :) starting to understand

16. Member
Join Date
Dec 2010
Posts
31
Rep Power
0
hey but how do you stop the loop with it?i mean,what if the user would input more than 5 mile/gallons

17. Member
Join Date
Dec 2010
Posts
31
Rep Power
0
more like a infinite loop until some user would stop the process by entering a certain input value

18. Member
Join Date
Dec 2010
Posts
31
Rep Power
0
Java Code:
``` */import java.io.*;
public class lola {
public static void main(String[]args)throws Exception{
int lo,la=0,bot;
String o,a,b;

do{
System.out.println("Enter LO: ");
lo=Integer.parseInt(o);
if (lo!=-1)
{
System.out.println("Enter Bot: ");
bot=Integer.parseInt(b);
la=la+bot+lo;
}
else
{
System.out.println("lalala" + (la));

}}
while(lo!=-1);
System.out.println("ambot " + (la));

}
}```
i know the coding is messed up and the data type names are stupid but i managed to make it work somehow...any words if i done this right?

19. Member
Join Date
Aug 2011
Posts
95
Rep Power
0
If it consistently produces the results you want for all reasonable cases, then it is "correct."

It does not, of course, produce "gallons per mile" reports. But hey; what were you driving, a tank?!?

20. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
6
Why do you use BufferedReader and not Scanner?

Page 1 of 2 12 Last

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•