Java 为负数添加if-else

Java 为负数添加if-else,java,string,if-statement,Java,String,If Statement,我试图添加一个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);

我试图添加一个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)));

  }
}

你的一些代码在运输途中丢失了吗?看起来结尾有很多空白,我注意到您没有使用
StockSell

编辑:有人重新格式化了你的代码,所以空格消失了,但在运输途中丢失的问题仍然存在


如果没有,你就得计算利润。由于利润是一个关键指标,您可能希望在其他点显示它,因此我将声明一个变量
double-price
,其他声明位于顶部附近。计算利润后,您可以将变量
利润
0
的if-else比较放在以后最方便的地方。

您的一些代码在运输过程中丢失了吗?看起来结尾有很多空白,我注意到您没有使用
StockSell

编辑:有人重新格式化了你的代码,所以空格消失了,但在运输途中丢失的问题仍然存在


如果没有,你就得计算利润。由于利润是一个关键指标,您可能希望在其他点显示它,因此我将声明一个变量
double-price
,其他声明位于顶部附近。计算利润后,您可以将变量
利润
0
进行if-else比较,然后在最方便的地方进行比较。

从用户处获取所有变量值后,定义一个类型为double的变量,称为利润=基于当时获得的变量进行一些数学计算

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");}
if(利润>0){System.out.println(“正利润”);}

else if(profit从用户处获取所有变量值后,定义一个类型为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");}
if(利润>0){System.out.println(“正利润”);}
否则,如果(利润)