-2

I can't get the output to give any number beside 0.0 What am I doing wrong here?

int num1 = 0;    
double d1 = 3.25, d2 = 0.45;
double numdozen = num1 / 12;
double numlooseeggs = num1 - (numdozen * 12);
double totaldozen = numdozen;
double totalloose = numlooseeggs;

Scanner fromKeyboard = new Scanner(System.in);

System.out.println("How many eggs do you want? They are $3.25/dozen or $.45/egg.");
num1 = fromKeyboard.nextInt();



double sum = totaldozen + totalloose;

System.out.println("$" + sum);
fvu
  • 32,488
  • 6
  • 61
  • 79

3 Answers3

2

double numdozen = num1 / 12;

This initializes numdozen to 0.0, which probably wasn't your intent (if it was your intent, you would simply set it to 0.0).

numlooseeggs is also initialized to 0.0, as well as totaldozen and totalloose.

I don't think you intended all of them to be 0.

Finally double sum = totaldozen + totalloose; is set to 0, regardless of what you assign to num1.

What you probably want to do is calculate the non constant doubles after getting the user input :

num1 = fromKeyboard.nextInt();
double numdozen = (double)num1 / 12;
double numlooseeggs = num1 - (numdozen * 12);
double totaldozen = numdozen;
double totalloose = numlooseeggs;

Note that you should cast num1 to double before dividing it by 12, or numdozen would be 0 if num1 < 12.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Commenting just since the terms weren't explicitly used in this answer, but it might be worth the OP reading through some literature on reference and value types. I just found this related question in a quick search. It's not exactly the same matter, but given that this was a problem, I'm guessing reading here will help with future ones too. http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Matthew Haugen Sep 01 '14 at 00:49
  • Thank you so much! My professor told us there was a way to initialize an int before getting user input and still return the correct output but I've been trying to wrap my head around this all weekend and you made more sense than he did – Egg Dallas Sep 01 '14 at 05:48
1

you are seting everything to nothing

int num1 = 0;                                    0

double d1 = 3.25, d2 = 0.45;

double numdozen = num1 / 12;                     0 / 12 = 0
double numlooseeggs = num1 - (numdozen * 12);    0 - (0 * 12) = 0
double totaldozen = numdozen;                    0
double totalloose = numlooseeggs;                0
1

You're calculating all your values before you take the user input, so num1 is 0, forcing everything else to be 0. Put the calculations after you take input, like this:

int num1 = 0;
// don't do calculations that depend on input yet

Scanner fromKeyboard = new Scanner(System.in);

System.out.println("How many eggs do you want? They are $3.25/dozen or $.45/egg.");
num1 = fromKeyboard.nextInt();

// put the calculations that depend on input here
double d1 = 3.25, d2 = 0.45;
double numdozen = num1 / 12.0;
double numlooseeggs = num1 - (numdozen * 12);
double totaldozen = numdozen;
double totalloose = numlooseeggs;

double sum = totaldozen + totalloose;

System.out.println("$" + sum);
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97