- import java.awt.FlowLayout;
- import java.awt.LayoutManager;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.text.NumberFormat;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.border.TitledBorder;
- public class Example12{
- /**1、英雄升级,从0级升到1级,概率100%。
- 从1级升到2级,有1/3的可能成功;1/3的可能停留原级;1/3的可能下降到0级;
- 从2级升到3级,有1/9的可能成功;4/9的可能停留原级;4/9的可能下降到1级。
- 每次升级要花费一个元宝,不管成功还是停留还是降级。
- 求英雄从0级升到3级平均花费的元宝数目。
- * @param args
- */
- JFrame login;
- JFrame test;
- private int gold;
- private static int level=1;
- private double rate;
- private JLabel goldLabel;
- private JLabel levelLabel;
- private JLabel rateMsg;
- private JButton levelUp;
- private JButton exit;
- private JTextArea result;
- private JLabel prompt;
- private Status status;
- public Example12(){
- loginInit();
- testInit();
- }
- public void testInit(){
- //信息面板
- JPanel msgPanel=new JPanel();
- //提示面板
- JPanel promptPanel=new JPanel();
- //接扭面板
- JPanel btnPanel=new JPanel();
- //升级结果
- // JPanel panel=new JPanel();
- test=new SwingFrame().getFrame("测试窗口",550,350,new FlowLayout());
- test.setVisible(false);
- //元宝数
- //初始化控件
- conInit();
- //设置面板提示信息
- msgPanel.setBorder(new TitledBorder("人物信息:"));
- promptPanel.setBorder(new TitledBorder("升级提醒:"));
- btnPanel.setBorder(new TitledBorder("操作"));
-
- msgPanel.add(goldLabel);
- msgPanel.add(levelLabel);
- msgPanel.add(rateMsg);
- promptPanel.add(result);
- promptPanel.add(prompt);
- btnPanel.add(levelUp);
- btnPanel.add(exit);
- //增加面板到窗体
- test.add(msgPanel);
- test.add(promptPanel);
- test.add(btnPanel);
- addAction();
- }
- public void conInit(){
- goldLabel=new JLabel();
- //等级数提示
- levelLabel=new JLabel("你目前的等级为:"+level);
- //成功机率
- rateMsg=new JLabel("目前提升的成功率为:"+getRate());
- //升级提示
- prompt=new JLabel();
- //结果
- result=new JTextArea(5,2);
- result.append(getMsg());
- result.setEditable(false);
- levelUp=new JButton("升级");
- exit=new JButton("结束");
- }
- public void addAction(){
- levelUp.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- if(gold>0){
- status=getStatus();
- goldLabel.setText("你目前的元宝数:"+--gold+"个");
- levelLabel.setText("你目前的等级为:"+status.getLevel());
- prompt.setText(getResult());
- rateMsg.setText("目前提升的成功率为:"+getRate());
- result.setText(getMsg());
- } else {
- goldLabel.setText("你目前的元宝数不足,不能继续提升!");
- }
-
- }
- });
- exit.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- }
- public void loginInit(){
- login=new SwingFrame().getFrame("登录窗口",250,120,new FlowLayout());
- JLabel label=new JLabel("请设置测试元宝数量");
- final JTextField txt=new JTextField(10);
- JButton testBtn=new JButton("测试");
- JButton exitBtn=new JButton("退出");
- testBtn.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- if(!txt.getText().equals(""))
- gold=Integer.parseInt(txt.getText());
- if(gold>0){
- goldLabel.setText("你目前的元宝数:"+gold+"个");
- login.setVisible(false);
- test.setVisible(true);
- }
- else JOptionPane.showMessageDialog(null,"输入的元宝数必须大于0,否则无法提升!");
- }
- });
- exitBtn.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- login.add(label);
- login.add(txt);
- login.add(testBtn);
- login.add(exitBtn);
- }
- public String getResult(){
- status=getStatus();
- System.out.println(status);
- switch(status){
- case LEVEL_UP:return "提升成功,等级 +1";
- case LEVEL_DOWN:return "提升失败,等级 -1";
- default:return "等级不变!";
- }
-
- }
- //根据成功率判定是否升级成功
- public Status getStatus(){
- int bfLevel=level;
- rate=1.0;
- for(int i=0;i<level;i++,rate/=3);//以/3率增加提升难度,写循环是为了更多等级
- double num=Math.random();
- if(num<rate) //最开始是1.0也就是100%,然后是1/3,1/9,1/27...所以这里难度会越来越高.与题意也相符
- return Status.LEVEL_UP;
- else if((num-rate)/2>0){//判断是否降级
- return Status.LEVEL_DOWN;
- }
- else return Status.LEVEL;
- }
- public String getRate(){
- NumberFormat f=NumberFormat.getInstance();
- for(int i=0;i<level;i++,rate/=3);
- f.setMaximumFractionDigits(2);
- double value=Double.parseDouble(f.format(rate));
- return (value*100)+"%";
- }
- public String getMsg(){
- String msg=null;
- switch(level){
- case 1:rate=1.0;msg="目前升级"+(rate*100)+"%成功率,无失败逞罚";break;
- case 2:rate=0.3;result.append("目前升级"+(rate*100)+"%成功率有1/3的可能成功,\n"+"1/3的可能停留原级;\n1/3的可能下降到0级");break;
- case 3:rate=0.1;msg="目前升级"+(rate*100)+"%有1/9的可能成功;\n4/9的可能停留原级;\n4/9的可能下降到1级";break;
- }
- return msg;
- }
-
- public static void main(String[] args) {
- new Example12();
- }
- enum Status{
- LEVEL_UP,LEVEL_DOWN,LEVEL;
- public int getLevel(){
- switch(this){
- case LEVEL_UP:level++;break;
- case LEVEL_DOWN:level--;break;
- default:break;
- }
- return level;
- }
- }
- }
- class SwingFrame{
- JFrame frame=new JFrame();
- SwingFrame(){
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setResizable(false);
- frame.setLocation(200,100);
- frame.setVisible(true);
- }
- public JFrame getFrame(String title,int width,int height,LayoutManager layout){
- frame.setTitle(title);
- frame.setSize(width,height);
- frame.setLayout(layout);
- return frame;
- }
- }
复制代码 功能全部实现了.因为GUI的问题代码写的太多了.布局出了问题.很难看.还有提示信息出了错.很麻烦,不改了,如果不实现GUI.那么里面的功能都实现了.
|