百钱买百鸡问题--java 实现

>> 运行结果

点击"解答"按钮后:
 

点击“退出”按钮即可退出!

 

>> 原代码:(以下程序在eclipse里调试通过)

 
  
  1. package com.huqianhao.homework;  
  2.  
  3. import java.awt.*;  
  4. import java.awt.event.ActionEvent;  
  5. import java.awt.event.ActionListener;  
  6. import javax.swing.*;  
  7. import javax.swing.border.Border;  
  8.  
  9. public class ChickenProgram extends JFrame implements ActionListener {  
  10.  
  11. private static final long serialVersionUID = 1L;  
  12. private static final int PRICE_PER_COCK = 5// 公鸡5元钱一只  
  13. private static final int PRICE_PER_HEN = 3// 母鸡3元钱一只  
  14. private static final int PRICE_THREE_CHICKENS = 1// 小鸡1元钱三只  
  15. private static final double TOTAL_MONEY = 100;  
  16. private static final int NO_OF_CHOOKS = 100// 总的鸡数量  
  17. JPanel panel1, panel2;  
  18. JLabel resultLabel;  
  19.  
  20. public ChickenProgram() {  
  21. Container contentPane = getContentPane();  
  22. contentPane.setLayout(new BorderLayout());  
  23. panel1=new JPanel();  
  24. panel1.setLayout(new GridLayout(1,3));  
  25. panel2=new JPanel();  
  26. panel2.setLayout(new BorderLayout());  
  27.  
  28. JLabel cockLabel, henLabel, chickenLabel;  
  29. cockLabel = new JLabel("每只公鸡:" + PRICE_PER_COCK + "元");  
  30. henLabel = new JLabel("每只母鸡:" + PRICE_PER_HEN + "元");  
  31. chickenLabel = new JLabel("小鸡:" + PRICE_THREE_CHICKENS + "元钱三只");  
  32.  
  33. JButton calculateButton = new JButton("解答");  
  34. calculateButton.addActionListener(this);  
  35. JPanel smallPanel=new JPanel();  
  36. smallPanel.add(calculateButton);  
  37. JButton exitButton=new JButton("退出");  
  38. smallPanel.add(exitButton);  
  39. exitButton.addActionListener(new ActionListener(){  
  40. public void actionPerformed(ActionEvent e){  
  41. System.exit(0);  
  42. }  
  43. });  
  44.  
  45. String question = "问题:"+TOTAL_MONEY + "元钱买" + NO_OF_CHOOKS  
  46. "只鸡,问公鸡、母鸡、小鸡各买多少只?";  
  47. JLabel questionLabel = new JLabel(question);  
  48. Border border1=BorderFactory.createTitledBorder("鸡的价格");  
  49. Border border2=BorderFactory.createTitledBorder(question);  
  50. resultLabel = new JLabel("");  
  51.  
  52. panel1.add(cockLabel);  
  53. panel1.add(henLabel);  
  54. panel1.add(chickenLabel);  
  55. panel1.setBorder(border1);  
  56.  
  57. panel2.setBorder(border2);  
  58. // panel2.add(questionLabel);  
  59. panel2.add(smallPanel,BorderLayout.SOUTH);  
  60. panel2.add(resultLabel);  
  61. contentPane.add(panel1,BorderLayout.NORTH);  
  62. contentPane.add(panel2,BorderLayout.CENTER);  
  63. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  64. }  
  65.  
  66. public static void main(String args[]) {  
  67. ChickenProgram frame = new ChickenProgram();  
  68. frame.setTitle("百钱买百鸡问题");  
  69. frame.setVisible(true);  
  70. frame.setSize(500300);  
  71. }  
  72.  
  73. public void actionPerformed(ActionEvent e) {  
  74.  
  75. String resultString = "<html>以下是几种可行方案:<ul>";  
  76. int noOfcocks, noOfHens, noOfchickens;  
  77.  
  78. for (noOfcocks = 0; noOfcocks <= TOTAL_MONEY / PRICE_PER_COCK; noOfcocks++) {  
  79. for (noOfHens = 0; noOfHens <= TOTAL_MONEY / PRICE_PER_HEN; noOfHens++) {  
  80. noOfchickens = NO_OF_CHOOKS - noOfcocks - noOfHens;  
  81. if (PRICE_PER_COCK * noOfcocks + PRICE_PER_HEN * noOfHens  
  82. + noOfchickens * (PRICE_THREE_CHICKENS / 3.0) == TOTAL_MONEY){  
  83. resultString += ("<li>公鸡-->" + noOfcocks + " 母鸡-->" + noOfHens  
  84. " 小鸡-->" + noOfchickens+"</li>");  
  85. }  
  86. resultLabel.setText(resultString+"</ul></html>");  
  87. }  
  88. }  
  89.  
  90. }  
  91. }