gpt4 book ai didi

java - 为负数添加 if else

转载 作者:行者123 更新时间:2023-12-02 01:20:04 26 4
gpt4 key购买 nike

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

}
}

最佳答案

从用户那里获取所有变量值后,定义一个名为profit的 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");}

关于java - 为负数添加 if else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57879502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com