Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

1. ## 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++;
}  Reply With Quote

3. ## Re: Can you show me better a faster ways to accomplish these tasks.  Reply With Quote 