• Post Reply Bookmark Topic Watch Topic
  • New Topic

Rounding error  RSS feed

 
Rachel Anderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Here is my code:


import java.util.Scanner;

public class Population
{
public static void main(String[] args){
double organism;
int days;
System.out.print("Enter the number of days the organisms will multiply: ");
days = input.nextInt();
while(days < 1){
System.out.print("Invalid. Enter 1 or more: ");
days = input.nextInt();
}
System.out.println("Day Organisms");
System.out.println("-----------------------------");
System.out.println("1"+ " " +organism);
for( int i = 2; i <= days; i++){
organism = organism*(increase + 1);
System.out.print(i+" "+organism);
System.out.println();
}
}
} double increase;
Scanner input = new Scanner(System.in);
System.out.print("Enter the starting number organisms: ");
organism = input.nextDouble();
while(organism < 2){
System.out.print("Invalid. Must be at least 2. Re-enter: ");
organism = input.nextDouble();
}
System.out.print("Enter the daily increase: ");
increase = input.nextDouble();
while(increase < 0){
System.out.print("Invalid. Enter a non-negative number: ");
increase = input.nextDouble();
}

At day 3 my output is 39.11111111111111111111
It is supposed to be 39.2

please help.....

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Rachel.

Could you post your code again, making sure it's the same as the code you ran, and that it's complete? Also, please UseCodeTags (← that's a link).
 
Rachel Anderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying

I think that is the full code...I am very new...That is how I put it into compiler...how should it be more complete?
I looked at code tags. I really appreciate trying to learn it properly. How could I write it more java akin?
Willing to learn, listen and really appreciate your help
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is how I would use code tags and what happens when I format your code:



So far it's okay, except, where are input and increase initialized?

But now the rest of your code is outside of the class.



Does this go inside main()?
 
Rachel Anderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tried to fix some things to get it to all be in one class and I tried to 'wrap' following your example. Thank you for taking the time to read and respond. It's really helpful and I appreciate it.

double increase;
Scanner input = new Scanner(System.in);
System.out.print("Enter the starting number organisms: ");
organism = input.nextDouble();
while(organism < 2)
{

System.out.print("Invalid. Must be at least 2. Re-enter: ");
organism = input.nextDouble();
}


System.out.print("Enter the daily increase: ");
increase = input.nextDouble();
while(increase < 0)
{
System.out.print("Invalid. Enter a non-negative number: ");
increase = input.nextDouble();
}

System.out.print("Enter the number of days the organisms will multiply: ");
days = input.nextInt();
while(days < 1){
System.out.print("Invalid. Enter 1 or more: ");
days = input.nextInt();
}
System.out.println("Day Organisms");
System.out.println("-----------------------------");
System.out.println("1"+ " " +organism);
for( int i = 2; i <= days; i++)
{
organism = organism*(increase + 1);
System.out.print(i+" "+organism);
System.out.println();
}
}
}

Here is the cut and paste

Expected outcome

1&rarr&rarr20.0↵
2&rarr&rarr28.0↵
3&rarr&rarr39.2↵
4&rarr&rarr54.88↵
5&rarr&rarr76.83200000000001↵
6&rarr&rarr107.56480000000002↵

Actual

-----------------------------↵
1········20.0↵
2·····28.0↵
3·····39.199999999999996↵
4·····54.87999999999999↵
5·····76.83199999999998↵
6·····107.56479999999996↵
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's still a problem when you are posting your code. First, you're not posting all of it. Include the entire class. Second, when you are posting the code, you are not using code tags. Click the link on my first post and follow the instructions.

After that is correct, we'll need to see the input you used to get the output you posted.
 
Rachel Anderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this is starting to get better..thanks for helping me


 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the code. Now, what are the numbers you're entering when the program is run?
 
Rachel Anderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running this on My Pearson Learning Compiler for school

I don't put any numbers in.

I just type it like you see it and hit submit and it gives me the error

I have been working on this for a week or so

I think I have a floating decimal problem (something with math)

I am not able to get the round numbers

These very specific decimals are being counted as wrong input

It has to be rounded

instead of 39.1111111116

It has to say 39.2

Could I show you the question or the error?

Either way if we get this figured out (it's due Sunday at Midnight)

I really have learned a lot and even more on here getting the [code] correct

Thanks for taking the time to have these blogs and help out

I think I need a lot more practice though
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel, please BeForthrightWhenCrossPostingToOtherSites <- click the link

http://www.java-forums.org/new-java/94936-rounding-error.html
 
Rachel Anderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
on that site the answer is marked "solved"


that code also doesn't work...I will still take the help if I can get it. I really would like to learn something here
I didn't know it was illegal to tap multiple sites. As a newbee maybe that can be rectified? Like I said my goal here is to learn something. Not to waste people's time
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Anderson wrote:on that site the answer is marked "solved" . . . my goal here is to learn something. Not to waste people's time
… and is it solved?

I would say it isn't because you have lots of commented‑out lines. which should probably be deleted. You also should have one statement in the main method. Does that code actually compile? Don't use System.exit. Don't use printf with the + operator. Use printf with the percent tags. One of the percent tags will do the rounding for you.

You need to tell people on both fora that you are posting twice; then they can check and not waste any time. Please tell us what actually goes wrong. We can't help otherwise. I don't know what you have done with the code tags, but I shall try to correct it
 
Rachel Anderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a better solution



This answer was offered


the response was "that doesnt work either so i did this and it worked"



I have tried to insert the above in the original code and I am not placing it correctly or it doesn't work
I took this piece



and replaced it with this





Any ideas? I didn't post this anywhere else. Thanks for your help. If nothing else I at least learned how to properly post. That's something.


 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corrected code tags for you. In order to get it work, I think easiest way is to select your code and click button "Code".
 
Rachel Anderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I appreciate that. Every little bit helps. I'm getting closer
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that is not how you increase organisms. You asked for a percentage and then didn't multiply by a percentage. If I enter 25% you will increase your organisms by 25×.
 
Rachel Anderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what line in this code asks for a percent?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I must have read that wrongly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!