为空格键添加键侦听器

时间:2017-10-03 20:35:10

标签: java

我正在制作一个模拟股票的程序,但我遇到了一个问题。我尝试了一些我在Stack Overflow上搜索过的解决方案,但是没有它们似乎有用。

import java.lang.*;
import java.util.Scanner;
import java.util.Random;

// Code written by Jaden Lee

public class Stock {
    public static void clrScrn() {
        for (int i = 0; i < 1; i++) {
            System.out.println("");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Hello. I am the Stock-Bot.");
        System.out.println("How much do you want to start with?");
        double start = scanner.nextDouble();
        double balance = start;
        double max = 0;
        System.out.printf("You will start with %s dollars.", balance);
        while (balance > 0.00) {
            clrScrn();
            System.out.printf("You currently have %s dollars \n", balance);
            double num = 0;
            while (num < 10 && num > -10) {
                Random rand = new Random();
                int n = rand.nextInt(3)-1;
                num += Math.random()*n;
            }
            if (num > 10) {
                if (balance > 0 && balance <= 10) {
                    balance += 0.1;
                } else if (balance > 10 && balance <= 100) {
                    balance += 1;
                } else if (balance > 100 && balance <= 1000) {
                    balance += 10;
                } else if (balance > 1000 && balance <= 10000) {
                    balance += 100;
                } else if (balance > 10000 && balance <= 100000) {
                    balance += 1000;
                } else if (balance > 100000 && balance <= 1000000) {
                    balance += 10000;
                } else if (balance > 1000000 && balance <= 10000000) {
                    balance += 100000;
                }
            } else if (num < -10) {
                if (balance > 0 && balance <= 10) {
                    balance -= 0.1;
                } else if (balance > 10 && balance <= 100) {
                    balance -= 1;
                } else if (balance > 100 && balance <= 1000) {
                    balance -= 10;
                } else if (balance > 1000 && balance <= 10000) {
                    balance -= 100;
                } else if (balance > 10000 && balance <= 100000) {
                    balance -= 1000;
                } else if (balance > 100000 && balance <= 1000000) {
                    balance += 10000;
                } else if (balance > 1000000 && balance <= 10000000) {
                    balance += 100000;
                }
            }
            if (balance > max) {
                max = balance;
            }
            Thread.sleep(10);
        }
        max = Math.round(max);
        float profit = (float) ((max/start)*100)-100;
        System.out.println("You lost.");
        System.out.printf("Your greatest point of your stock was %s dollars per stock. \n", max);
        System.out.printf("You could have increased you stock value %s percent in size. \n", profit);
    }
}

我正在制作这个程序来模拟股票。我需要一个监听 space 的关键监听器,如果点击它,它需要停止程序并卖掉股票。

0 个答案:

没有答案