将命令行游戏转换为applet

时间:2014-01-14 07:38:44

标签: java user-interface applet

我创建了一个单类Java游戏,我们需要买入和卖出股票,我刚开始使用Java并且是新手,所以需要帮助+这段代码不是很好。

import java.io.*;
import java.util.Random;
public class stock_holding_game
{
    static Random randomn = new Random();
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 public static void main(String[] args)throws IOException
  {
        double cash = 30.0;
        double stocks[] = {0,10,20,40,80,160,320,640};//cost of stocks (there are 7 stocks each with different price)
        int mystocks[] = {0,0,0,0,0,0,0,0};
        pass("Enter your name");
        String name = br.readLine();
        pass("Hi "+name);
        commandlist();
        boolean booleancheck = true;
        while(booleancheck)
        {
            pass("Please enter your command");
            String command = br.readLine();
            command =" "+command+"  ";
            char c = command.charAt(2);
            switch(c)
            {
                case'h':
                pass("enter the stock ID number");
                int s1 = Integer.parseInt(br.readLine());
                if(s1>0&&s1<=8)
                {
                    pass("Starting price: "+stocks[s1]);                    
                    double t = randomn.nextDouble() *(stocks[s1]/10.0);
                    int add = randomn.nextInt(2);
                    if(add==0)stocks[s1]=stocks[s1]+t;
                    else
                    stocks[s1]=stocks[s1]-t;
                    pass("Current price: "+stocks[s1]);
                }
                else
                {
                    pass("Wrong Number");
                    pass("Enter a number from 1 to 8");
                    pass("Do whole task again xD");
                }
                break;
                case 'n':
                pass("Enter the stock ID number");
                int s2 = Integer.parseInt(br.readLine());
                if(s2>0&&s2<=8)
                {
                    double t = randomn.nextDouble() *(stocks[s2]/10.0);
                    int add = randomn.nextInt(2);
                    if(add==0)stocks[s2]=stocks[s2]+t;
                    else
                    stocks[s2]=stocks[s2]-t;
                    pass("Current price: "+stocks[s2]);
                    pass("You currently have: "+mystocks[s2]+ " of these stocks");
                    pass("You have " +cash+" cash");
                    pass("Enter the number of stocks you wish to buy");
                    int nsbuy = Integer.parseInt(br.readLine());
                    if(nsbuy<0)pass("Idiot ! , add atleast 1 if you wish to buy");
                    else if(nsbuy==0);
                    else
                    {
                        double checkprice = nsbuy*stocks[s2];
                        if(checkprice > cash)
                        {
                            pass("You don't have enough cash in hand !");
                        }
                        else
                        {
                            cash = cash - checkprice;
                            mystocks[s2] = mystocks[s2] + nsbuy;
                            pass("Now you have "+mystocks[s2]+ " of stock ID " +s2);
                            pass("You are left with "+cash);
                        }
                    }
            }   
            else
            {
                pass("Invalid Input!");
                pass("Enter a number from 1 to 8");
            }
            break;
            case 'a':
            pass("You have currentln " +cash+" cash");
            break;
            case 'x':
            booleancheck = false ; 
            pass("You leave with "+cash+" cash");
            pass("Bye, Hope to see you again .");
            break;
            case 'o':
            commandlist();
            break;
            case 'y':
            for(int xyz = 1; xyz<mystocks.length - 1;xyz++)
            {
                pass("You have "+mystocks[xyz]+ " of stock ID "+xyz);
            }
            break;
            case 'e':
            pass("Enter the stock ID number");
            int s3 = Integer.parseInt(br.readLine());
            if(s3>0&&s3<=8)
            {
                double t = randomn.nextDouble() *(stocks[s3]/10.0);
                int add = randomn.nextInt(2);
                if(add==0)stocks[s3]=stocks[s3]+t;
                else
                stocks[s3]=stocks[s3]-t;
                pass("Current price: "+stocks[s3]);
                pass("You currently have: "+mystocks[s3]+ " of these stocks");
                pass("You have " +cash+" cash");
                pass("Enter the number of stocks you wish to sell");
                int nssell = Integer.parseInt(br.readLine());
                if(nssell<0)pass("Enter atleast 1 if you wish to sell(in case you have)");
                else if(nssell==0);
                else
                {
                    double nscheckprice = stocks[s3]*nssell;
                    if(nssell>mystocks[s3])pass("You don't jave that many stocks !");
                    else
                    mystocks[s3] = mystocks[s3] - nssell;
                    cash = cash + nscheckprice;
                    pass("You have successfully sold your " +nssell+" stocks for "+nscheckprice);
                    pass("Now , you have , "+ mystocks[s3]+ " of stock ID "+s3);
                    pass("You are left with " +cash + " cash");
                }
            }
            else
            {
                pass("Invalid Input!");
                pass("Enter a number from 1 to 8");
            }
            break;
            default:
            pass("Invalid Input!");
            break;                
        }
    }
}
private static void commandlist()
{
    pas("Your command list : ");
    pas("checkstock - check the current and earlier value of a stock");
    pas("invest - buy shares ");
    pas("sell - sell ur share ");
    pas("exit - leave the game");
    pas("my - show ur all current stock");
    pas("cash - show ur current cash in hand");
    pas("There are 7 stock with id 1 , 2 , 3 , and so on till 7")
    pas("Remember ! It is case sensitive");
}
private static void pass(String source)
{
    System.out.println(source);
}
private static void pas(String source)
{
    System.err.println(source);
}
}

我希望有一个这个代码的小程序,但我对applet了解不多。我希望使用一些按钮而不是打字。我知道代码添加按钮但不知道如何使用它们。需要帮助,所有回复都将不胜感激。

是的,也告诉我这个游戏怎么样(给这个游戏打分),但速度好像是新手一样。 现在,我没有充分询问如何将其转换为applet,而是希望以小块形式提出。首先,如何使用按钮告诉我的电脑如何继续?

这是我的代码:

import java.awt.*;

public class Buttonsuse extends java.applet.Applet
{
    Button invest , sell , check ;
    public void init()
    {
        setBackground (Color.white);
        setLayout(new FlowLayout (FlowLayout.CENTER,10,10));
        invest = new Button("Invest");
        sell = new Button("Sell");
        check = new Button("Check");
        add(invest);
        add(sell);
        add(check);
      }
}

现在,我怎么知道并告诉电脑按下按钮,就像用户按下投资按钮一样,它会打印出“你想投资”。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

您当前的程序只是一个大型的静态主方法,只有一些小的支持静态方法。由于此代码非常线性,因为大多数控制台程序都是如此,并且由于它没有符合OOP的类,因此无法直接转换或重用任何此代码以在事件驱动的GUI中使用。我建议:

  • 使用实例字段,构造函数和非静态方法将程序背后的逻辑或大脑提取到真正的OOP类中。
  • 只有在执行此操作后,您才应考虑创建一个GUI来显示逻辑。
  • 阅读一本关于OOP和Java的好书,例如“Thinking in Java”,因为这将帮助您以OOP方式思考和编码。

要考虑的非GUI类:

  • 具有String name,double(或BigDecimal)值,String缩写名称
  • 的Stock类
  • 持有名称字符串的玩家,持有股票的HashMap<Stock, Integer>和股票数量。
  • 玩家可以买卖股票的市场。

最重要的是,没有一种简单的方法可以将此代码“转换”为GUI,相反,您应该专注于学习Java基础知识,并在学习时试验您的代码(这就是您的学习方式)。