我试图添加一个if else语句,以便当利润为负数时告诉他们它为负利润,而当利润为正数时告诉他们它为正。我在哪里放置if else语句,以及如何格式化以便工作。
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)));
}
}
最佳答案
从用户获得所有变量值之后,定义一个类型为double的变量,称为Profit =基于您获得的变量的一些数学计算,然后
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");}