Can you show me better a faster ways to accomplish these tasks.

• March 16th, 2013, 08:39 AM
OUTSTANDAGE
Can you show me better a faster ways to accomplish these tasks.
This is a long post. I don't blame you if you don't read it. I'm very new to Java programming. I took an intro to Java class to learn more. My teacher gives us assignments and I'm able to accomplish them, however I just wish he would take more time to look at our code. I want to know better approaches that will help me solve problems more efficiently and with fewer code. My friend told to re-write some of the java built in functions so I can better understand how the language works. I took his challenge and decided to write a program that rounds decimal numbers. Can you show me faster ways to write this program? Its very long and probably shouldn't be.

the program let's the user continually enter decimal numbers to round if they don't chose to exit

I just put in the code that actually does the rounding

In short, it takes the double, iterates an int to the first whole number that greater then the double, subtracts it by one, iterates that int by .10 and then compares that iteration to 5, if it's less then 5 it rounds down, if it's greater then 5 it rounds up. Then uses another loop to iterate an integer to equal the double value and then prints that iteration as the final integer.

and the code does work

int i = 0;
while(i <= userDouble + 1)// Loop finds the first whole number that's greater then the double
{
if(i > userDouble){
double tens = i - 1;// takes the greater int and subtracts one to get the closet whole the less then the double.
double round = tens;
int iTwo = 1;
while(tens <= userDouble)// iterates the int directly below the double until it equals the double
{
tens = tens + .10;
iTwo++; // keeps track of home many times it added . 10 to double tens
if(tens > userDouble){
if(iTwo < 5){ // when iTw0 is greater then initial double compare the iteration to 5.
int w = 1;
while(w <= round)// enters new loop and iterates an integer to equal the the double tens
{
if(w == round){ // checks when int w equals double round and then prints int w.
int rounded = w;
System.out.print(rounded);
}
w++;
}
}else{ // if iTwo is greater then 5 enter this loop
int w = 1;
while(w <= round) // iterate int w to eqaul double round
{
if(w == round){
int rounded = w + 1; // print w + 1 to round up
System.out.print(rounded);
}
w++;
}
}
}
}

i++;
}
i++;
}
• March 16th, 2013, 09:12 AM
Norm
Re: Can you show me better a faster ways to accomplish these tasks.