Reputation: 71
Im trying to add an if else statement so when the profit is negative it tells them its a negative profit and when its positive it tells them its positive. Where would i put the if else statement and how would i format so it works.
import java.util.Scanner;
class Assignment2
{
public static void main(String[] args) //header of the main method
{
Scanner in = new Scanner(System.in);
String First;
String Last;
String StockCode;
double ShareNumber;
double StockBuy;
double StockSell;
final double charge =.02;
System.out.println("Enter your First name");
First = in.nextLine();
System.out.println("Enter your Last name");
Last = in.nextLine();
System.out.println("Enter the stock code");
StockCode = in.nextLine();
System.out.println("How much did the stocks cost?");
StockBuy = in.nextInt();
System.out.println("How many shares did you buy?");
ShareNumber = in.nextInt();
System.out.println("Customer Name:" + First.toUpperCase() + ' ' + Last.toUpperCase());
System.out.println("Stock Code:" + StockCode );
System.out.println("Stock Cost:" + ((StockBuy * charge) + (ShareNumber*StockBuy)));
}
}
Upvotes: 0
Views: 1144
Reputation: 60
after getting all variables values from the user define a variable of type double called profit = some mathematical calculations based on the variables you got then
if(profit>0) {System.out.println("positive profit");}
else if(profit<0) {System.out.println("negative profit");}
else if(profit==0) {System.out.println("zero profit");}
Upvotes: 2
Reputation: 11
Did some of your code get lost in transit? It seems like there's a lot of white space at the end, and I notice you haven't used StockSell
.
EDIT: Someone reformatted your code, so the white space is gone but the lost in transit question remains
If no, you would have to come up with some calculation for profit. Since profit is a key indicator that you might want to display at some other point, I'd declare a variable double profit
with your other declarations near the top. Once you calculate profit, you can place an if-else comparison of the variable profit
with 0
wherever is most convenient afterwards.
Upvotes: 0