Help - Search - Members - Calendar
Full Version: Limit To
BleepingComputer.com > Software > Programming
   
ussr1943
just what it says. right now i am making alot of simple small programs that are useful some ranging from how far away from a wall you are based on sound and the time it takes for echo, conversion tables ect. anyways i was thinking, about this code i was working on, it is a conversion table gallons/liters (note i would like to eventualy be able to just input for one or other and solve it, but thats later on, im excited : ) )
and the loop goes out to 100 gallons. but what would it do if i put the loop specifications to go up to say,
100,000,000,000,000?
what would it do, would it actually output all of that?! or would it not go, or would it make my comp really mad at me ? sad.gif
i don't think this can do much harm to comp but i am interested in knowing what would happen. and i am not sure if i should or not.


*i forgot to mention that i had table increase by 1 ( or ++ w/e floats ur boat)*
groovicus
I am really tired, so maybe I am misunderstanding what you are trying to do. Why would you need loops to do a conversion? As far as making a loop that went up that high, you could not do it with ints. I'll leave the why you cannot use ints up to you.

As far as doing conversions, that is a simple multiplier. For converting gallons to liters, it is simply a matter of multiplying gallons by 3.7854118. For converting liters to gallons, you divide by 3.7854118. I can't see any reason that you would need a loop at all.

Unless you are just trying to figure out all of the conversions per gallon up to 100 gallons?
ussr1943
i have it done, what i made was a table, it keeps out putting every conversion from 1 gallon to 100 gallons, i was woundering what if you made it go longer and if int won't hold it then i would just use long instead of int becuase int hold 32 bits(-2,147,483,648 upto 2,147,483,648,whereas long can hold up to 64 bits,and i'm too lazy to type out the range of that), anyways i was just woundering after i created the table.(i know this process isn't that great, its not efficient and its alot to scroll through but that is how i am learning it right now since i'm new)

*putting in the code, may help if not understandable still*
CODE
/*
  This program displays a conversion
  table for gallons to liters
  uses the for loop
*/


class GaltoLitTable {
public static void main(String args[]) {
  double gallons, liters;
  int counter;
  
  counter = 0; //start counter at 0
  for(gallons = 1; gallons <= 100; gallons++) {
   liters = gallons * 3.7845; // conversion
   System.out.println(gallons+" gallons = "+liters+" liters.");


   counter++; //increment line counter (by 1) with each loop iteration
   //every 10th line print a blank for easy readablity
   if(counter == 10) {
   System.out.println();
   counter = 0; //reset the counter
   }
  }
}
}
groovicus
Do you want any constructive criticism? There are a few programming conventions that I could point out.
ussr1943
i'm intrested in learning, so i'll take whatever you give out. thanks.
(keep in mind i don't know a whole lot, since i just started java a few days ago).
groovicus
Just to be clear, you did not do anything wrong. You could make your code look much cleaner though by combining instructions:
CODE
//convert gallons to liters
for(double gal = 1; gal <= 100; gall++){
   System.out.println(gal+" gallons = "+ gal*3.7845 + " liters.");

   //every 10th line print a blank for easy readablity
   if(gal/10 == 0) System.out.println(" ");
}


I would do it this way because it eliminates two variables. You could also get away with calling gal as an int (in which case you would learn a little about casting). You will also need to look at how a computer does division to understand why the blanks wil be inserted where you want.
ussr1943
good points, and i am just learning about casting so that is interesting.
i think that was a lil bit easyier way to do the convertion right there instead of declaring all that before. hmmm. and i will look up about the division, thanks for all your help.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.