I need help from any person that knows Java
I have writen a program and I am almost done expect stuck on one line of code, I am trying to write a command to make the program always round down
example 103.7 to 103
please anyone that can help would be appreciated
thanx
I dont know in java, but in c++ (which java is based off of) you could do somthing like this:
int a; (a is an integer variable)
double b; (b is a real number variable)
... input getting data for variable b...
a = b;
a will then become a truncated form of b, rounding down regardless of what lies beyond the decimal point.
It is like c++ but you forgot (int)
eg
int a;
double b;
...
a = (int)b;
if you are referring to how it should be in c++, I compiled a program that worked exactly as I described before (including the a = b statement that didn't do any type casting) and it worked perfectly.Originally Posted by muffenme
dont round up if higher then 5?
please show me your code so far.
declaration and method for starters.
If you want help, I will help you with your code, but I won't write it for you. I was a tutor for many things, including Java.
I will tell you if you are getting close, or horribly off. That's how I learned it, and will not contribute to cheating. But I will always contribute to teaching/tutoring and learning.
The easiest and savest way:
int result = (int) Math.floor( value );
Download Phex
This is correct where:Originally Posted by GregorK
roundToInteger( number )
I just wanted our friend to try a bit harder to figure it out is all, heh.
so an example of this method could be:
y = Math.floor(x + .5 );
thanx guys for the help I gave up and just turned it in, I will submit the code for you guys to see what I was missing
what I round up doing to finish the code was cast and make it into an integer but that would knock off one whole number
//Here it is boys, see what you can find that I missed and again thanx for the help
import java.util.Scanner;
import java.text.DecimalFormat;
public class Party
{
public static void main(String[] args)
{
int radius, height,guests;
double volume,gallons;
Scanner scan= new Scanner (System.in);
System.out.println("Enter the radius of the trash can, in inches::");
radius=scan.nextInt();
System.out.println("Enter the height of the liquid in the trash can, in inches:");
height=scan.nextInt();
volume=Math.PI*Math.pow(radius,2)*height;
gallons=volume/231.0;
guests=(int)gallons*3+1; // this is what was giving me the trouble, I added the one back that the casting took off but it seems not all inputs were accurate
DecimalFormat fmt= new DecimalFormat ("0.#");
DecimalFormat fmt1=new DecimalFormat ("0");
System.out.println("Your trash can will hold " + fmt.format(gallons)+" gallons of punch.");
System.out.println("Invite " + fmt1.format(guests) +" guests over!");
}
}
This another way to do it in java.
int a;
double b;
...
a = mod(b,1);
I don't normally use java so I'm not sure
Assuming you're free to declare things as you want (as in, this isn't an assignment, etc.), here's what I'd do:
double guests;
DecimalFormat df = new DecimalFormat("0");
// ...
guests = gallons * 3;
// ...
System.out.println("Guests: " + df.format(guests) );
Otherwise, if guests has to be an integer, just do this:
guests = Math.floor(gallons * 3);
If you're still stuck, try looking through Sun's Java Doc: http://java.sun.com/j2se/1.5.0/docs/api/index.html
P.S. If you want to get really anal, you technically didn't close the scanner (scan.close() ), and the end of your main method should have a System.exit(0); - sometimes, without it, it won't end the program when it's done executing (but, that's mostly if you're dealing with multiple threads, such as when you're making a GUI). Also, the first line in the method should be System.out.println("ducttapeBigSexy is awesome!"); - ok, jk about that last one ;)
Bookmarks