Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我有一个简单的股票交易程序来计算收益或损失,当我运行这个程序时,什么都没有显示_Java_Linux_Eclipse - Fatal编程技术网

Java 我有一个简单的股票交易程序来计算收益或损失,当我运行这个程序时,什么都没有显示

Java 我有一个简单的股票交易程序来计算收益或损失,当我运行这个程序时,什么都没有显示,java,linux,eclipse,Java,Linux,Eclipse,该程序的目的是计算简单股票市场交易的收益和/或损失,最后需要显示以下信息:;股份数量、购买金额、出售金额、支付的交易费和净利润。以下是我迄今为止编写的代码: public class StockTransaction { public static void main(String[]args) { String name; int numberShares; double buyPrice,sellPrice; Scan

该程序的目的是计算简单股票市场交易的收益和/或损失,最后需要显示以下信息:;股份数量、购买金额、出售金额、支付的交易费和净利润。以下是我迄今为止编写的代码:

public class StockTransaction {

    public static void main(String[]args) {
        String name;
        int numberShares;
        double buyPrice,sellPrice;
        Scanner input = new Scanner(System.in);
        System.out.print("What's your name?");
        name=input.nextLine();
        System.out.print("How many shares bought?");
        numberShares=input.nextInt();
        System.out.print("Buy price?");
        buyPrice=input.nextDouble();
        System.out.print("Sale price?");
        sellPrice=input.nextDouble();
        input.close();
        System.out.println(name + "here is the information of your stock transactions:");
        System.out.println("Number of shares:" + numberShares);
        System.out.println("Amount of purchase:" + buyPrice*numberShares);
        System.out.println("Amount of sell:" + sellPrice*numberShares);
        System.out.println("Transaction fee paid:" + 15 + 15);
        System.out.println("Net profit:" + (sellPrice*numberShares-buyPrice));
    }
}
我觉得不错(有几个小的输出问题)

在用户名后插入空格 将数字相加并用字符串连接时,需要在数字周围加括号

更改此项:

 System.out.println("Transaction fee paid:" + 15 + 15);
致:


再看看关于“净利润”的那句话,它在你的课堂上也有一个bug,你看到了有太多花括号的副作用。为了简化,您的代码如下所示:

class StockTransaction {

    public static main() {
        // this is the content of the main() function
    }

    {
        // this is where your code is.
    }

}
import java.util.Scanner;

public class StockTransaction
{
    public static void main(String[]args) {
        String name;
        int numberShares;
        double buyPrice,sellPrice;
        Scanner input = new Scanner(System.in);
        System.out.print("What's your name?");
        name=input.nextLine();
        System.out.print("How many shares bought?");
        numberShares=input.nextInt();
        System.out.print("Buy price?");
        buyPrice=input.nextDouble();
        System.out.print("Sale price?");
        sellPrice=input.nextDouble();
        input.close();
        System.out.println(name + "here is the information of your stock transactions:");
        System.out.println("Number of shares:" + numberShares);
        System.out.println("Amount of purchase:" + buyPrice*numberShares);
        System.out.println("Amount of sell:" + sellPrice*numberShares);
        System.out.println("Transaction fee paid:" + 15 + 15);
        System.out.println("Net profit:" + (sellPrice*numberShares-buyPrice));
    }
}
在Java中,任何方法之外的一块大括号代码充当实例初始值设定项。这意味着,当您显式创建对象时,例如使用以下代码:

new StockTransaction();
实例初始值设定项中的代码将与任何构造函数一起运行。无论用户选择使用什么构造函数创建对象,这都是运行一些初始化代码的一种方便方法

但是,您会注意到,
main()
函数前面是关键字
static
static
告诉编译器可以在没有对象实例的情况下调用该方法。它属于
StockTransaction
类,而不是该类的特定实例

当程序启动时,
main()
在没有
StockTransaction
类实例的情况下被调用。因为没有要初始化的实例,所以没有理由调用代码

所有这些都是我所认为的更高级的java用法,所以如果它没有意义的话,不用担心…随着你对这门语言有了更多的经验,它会在以后的日子里发生

同时,让程序运行所需要做的就是将代码移动到
main()
函数中,并去掉那些多余的花括号。应该是这样的:

class StockTransaction {

    public static main() {
        // this is the content of the main() function
    }

    {
        // this is where your code is.
    }

}
import java.util.Scanner;

public class StockTransaction
{
    public static void main(String[]args) {
        String name;
        int numberShares;
        double buyPrice,sellPrice;
        Scanner input = new Scanner(System.in);
        System.out.print("What's your name?");
        name=input.nextLine();
        System.out.print("How many shares bought?");
        numberShares=input.nextInt();
        System.out.print("Buy price?");
        buyPrice=input.nextDouble();
        System.out.print("Sale price?");
        sellPrice=input.nextDouble();
        input.close();
        System.out.println(name + "here is the information of your stock transactions:");
        System.out.println("Number of shares:" + numberShares);
        System.out.println("Amount of purchase:" + buyPrice*numberShares);
        System.out.println("Amount of sell:" + sellPrice*numberShares);
        System.out.println("Transaction fee paid:" + 15 + 15);
        System.out.println("Net profit:" + (sellPrice*numberShares-buyPrice));
    }
}

@raffian您可能希望撤消该编辑,他正在以静态初始值设定项块的形式运行代码,这可能与问题有关。请更改代码中的大括号,使其看起来像现在的问题“post edit”,并查看它是否运行。@par good catch,但它是静态init块吗?自我的编辑以来编辑的NES请参见以下答案以获取线索:同时参见您使用的Java版本?