English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

class Six Thousand {

public static void main(string args[]) {
double rate, money, payment;
int years;

rate = 0.054;
money= 5000;
payment = 6000;
years = 0;
// payment = (money * rate * years) + money;
years = (payment - money) / (money * rate);
whatsLeft = (payment - money) % (money * rate);

if (whatsLeft) {

System.out.print("It will take ");
System.out.print(years);
System.out.println(" years to get the money.");

how do write the if statement part, i'm trying to find out the remainder?

2007-08-04 08:19:42 · 1 answers · asked by Calvin & Hobbes 4 in Computers & Internet Programming & Design

and add one year

2007-08-04 08:25:23 · update #1

1 answers

A couple of issues:

You can't have a space in your class name. Try "SixThousand."

You can't assign a double to an int without explicitly casting it, i.e.: years = (int) ( (payment - money ...);

You can't say "if (number)" in Java, the way you can in C (where numbers other than zero are treated as "true"). You have to explicitly compare the number to something. If you mean "if (whatsLeft > 0)" or something like that, you can write that.

I'm not sure what you're trying to do with the calculation, but after the "whatsLeft" calculation, the variables have these values:

rate = 0.054
money = 5000.0
payment = 6000.0
years = 3
whatsLeft = 190.0

If (per your added text) you want to increase the number of years by 1, then you can write:

if (whatsLeft > 0.0) years++;

Comparing double values is a bit tricky, though. Sometimes numbers will end up with very tiny values due to limits of precision (e.g., "0.0000000001 cents") that are technically greater than zero but really should be treated as equal to zero because they are a round-off artifact.

You probably want to say if whatsLeft is over a half-penny, or something like that:

if (whatsLeft >= 0.005) years++;

PS - It's quite refreshing to see a programming question posted by someone who is actually trying to solve the problem on their own, rather than a "please write my entire assignment for me" one.

2007-08-04 09:57:14 · answer #1 · answered by McFate 7 · 0 0

fedest.com, questions and answers