Home > Blockchain >  How do I fix multiple negative (-) symbol outputs?
How do I fix multiple negative (-) symbol outputs?

Time:02-05

I'm creating a program to answer a prompt:

Sites like Zillow get input about house prices from a database and provide nice summaries for readers. Write a program with two inputs, current price and last month's price (both integers). Then, output a summary listing the price, the change since last month, and the estimated monthly mortgage computed as (currentPrice * 0.051) / 12. End the last output with a newline.

It wants me to have an output of:

"This house is $200000. The change is $-10000 since last month. The estimated monthly mortgage is $850.0."** using the inputs : 200000, 210000

This house is $350000. The change is $40000 since last month. The estimated monthly mortgage is $1487.5.** using the inputs : 350000, 310000

and:

This house is $1000000. The change is $900000 since last month. The estimated monthly mortgage is $4250.0.** using the inputs : 1000000, 100000

I've managed to make a program that can give me results but for some reason it messes up with the - symbol in front of my number for "The change is $_______". For the first output it gives me $10000, the second gives me $-40000, and the third input gives me $-900000. Could someone help me or explain what I can do or why it gives me these results?

Below is my code:

import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);  
        int currentPrice = input.nextInt();
        int lastMonthsPrice = input.nextInt();
        int LMP = lastMonthsPrice - currentPrice;
        double EMP = (currentPrice * 0.051) / 12;
          
        System.out.print("This house is $"   currentPrice   ". ");
        System.out.println("The change is $"   LMP   " since last month.");
        System.out.println("The estimated monthly mortgage is $"   EMP   ".");
    }
}

I've tried putting a negative symbol(-) infront after the dollar sign in "The change is $" but it just adds two negatives for output 2 and 3 after it finishes running

Example of the line of code:

System.out.println("The change is $-"   LMP   " since last month.");

Then I get results like these:

This house is $200000. The change is $-10000 since last month.
The estimated monthly mortgage is $850.0.
This house is $350000. The change is $--40000 since last month.
The estimated monthly mortgage is $1487.5.
This house is $1000000. The change is $--900000 since last month.
The estimated monthly mortgage is $4250.0.

CodePudding user response:

Swap lastMonthsPrice with currentPrice. Your subtracting formula is wrong.

CodePudding user response:

You're subtracting the wrong way. The change in price is the current price minus the previous price.

int LMP = currentPrice - lastMonthsPrice;
  • Related