百钱买百鸡问题--java 实现
>> 运行结果:
点击"解答"按钮后:
点击“退出”按钮即可退出!
>> 原代码:(以下程序在eclipse里调试通过)
- package com.huqianhao.homework;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- import javax.swing.border.Border;
- public class ChickenProgram extends JFrame implements ActionListener {
- private static final long serialVersionUID = 1L;
- private static final int PRICE_PER_COCK = 5; // 公鸡5元钱一只
- private static final int PRICE_PER_HEN = 3; // 母鸡3元钱一只
- private static final int PRICE_THREE_CHICKENS = 1; // 小鸡1元钱三只
- private static final double TOTAL_MONEY = 100;
- private static final int NO_OF_CHOOKS = 100; // 总的鸡数量
- JPanel panel1, panel2;
- JLabel resultLabel;
- public ChickenProgram() {
- Container contentPane = getContentPane();
- contentPane.setLayout(new BorderLayout());
- panel1=new JPanel();
- panel1.setLayout(new GridLayout(1,3));
- panel2=new JPanel();
- panel2.setLayout(new BorderLayout());
- JLabel cockLabel, henLabel, chickenLabel;
- cockLabel = new JLabel("每只公鸡:" + PRICE_PER_COCK + "元");
- henLabel = new JLabel("每只母鸡:" + PRICE_PER_HEN + "元");
- chickenLabel = new JLabel("小鸡:" + PRICE_THREE_CHICKENS + "元钱三只");
- JButton calculateButton = new JButton("解答");
- calculateButton.addActionListener(this);
- JPanel smallPanel=new JPanel();
- smallPanel.add(calculateButton);
- JButton exitButton=new JButton("退出");
- smallPanel.add(exitButton);
- exitButton.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- System.exit(0);
- }
- });
- String question = "问题:"+TOTAL_MONEY + "元钱买" + NO_OF_CHOOKS
- + "只鸡,问公鸡、母鸡、小鸡各买多少只?";
- JLabel questionLabel = new JLabel(question);
- Border border1=BorderFactory.createTitledBorder("鸡的价格");
- Border border2=BorderFactory.createTitledBorder(question);
- resultLabel = new JLabel("");
- panel1.add(cockLabel);
- panel1.add(henLabel);
- panel1.add(chickenLabel);
- panel1.setBorder(border1);
- panel2.setBorder(border2);
- // panel2.add(questionLabel);
- panel2.add(smallPanel,BorderLayout.SOUTH);
- panel2.add(resultLabel);
- contentPane.add(panel1,BorderLayout.NORTH);
- contentPane.add(panel2,BorderLayout.CENTER);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public static void main(String args[]) {
- ChickenProgram frame = new ChickenProgram();
- frame.setTitle("百钱买百鸡问题");
- frame.setVisible(true);
- frame.setSize(500, 300);
- }
- public void actionPerformed(ActionEvent e) {
- String resultString = "<html>以下是几种可行方案:<ul>";
- int noOfcocks, noOfHens, noOfchickens;
- for (noOfcocks = 0; noOfcocks <= TOTAL_MONEY / PRICE_PER_COCK; noOfcocks++) {
- for (noOfHens = 0; noOfHens <= TOTAL_MONEY / PRICE_PER_HEN; noOfHens++) {
- noOfchickens = NO_OF_CHOOKS - noOfcocks - noOfHens;
- if (PRICE_PER_COCK * noOfcocks + PRICE_PER_HEN * noOfHens
- + noOfchickens * (PRICE_THREE_CHICKENS / 3.0) == TOTAL_MONEY){
- resultString += ("<li>公鸡-->" + noOfcocks + " 母鸡-->" + noOfHens
- + " 小鸡-->" + noOfchickens+"</li>");
- }
- resultLabel.setText(resultString+"</ul></html>");
- }
- }
- }
- }
转载于:https://blog.51cto.com/huqianhao/952930