关于编程的问题,我想编一个程序,就是统计各个数字的个数,用到数组时有些问题,像各位大侠求助

2024-12-14 01:29:26
推荐回答(4个)
回答1:

or.java *
** write by banxi1988 2010-05-*;
** modification by banxi1988 2010-06-05 22:00*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class SimpleCalculator extends JFrame implements ActionListener {
JFrame jfr = new JFrame("A Simple Calculator");
JLabel title = new JLabel("Simple Calculator");
JLabel[] jlaArry = { new JLabel("操作数一"), new JLabel("操作数二"),
new JLabel("结果") };
JTextField[] jtfArray = { new JTextField(), new JTextField(),
new JTextField() };
JButton[] jbtArray = { new JButton("相加"), new JButton("相减"),
new JButton("相乘"), new JButton("相除"), new JButton("清零") };

public SimpleCalculator() {
jfr.setLayout(null);
title.setBounds(30, 10, 160, 30);
jfr.add(title);
for (int i = 0; i < jlaArry.length; i++) {
jlaArry[i].setBounds(20, 50 + i * 40, 90, 30);
jfr.add(jlaArry[i]);
jtfArray[i].setBounds(140, 50 + i * 40, 90, 30);
jfr.add(jtfArray[i]);
}/* for */

jbtArray[0].setBounds(30, 40 + (jlaArry.length + 1) * 40, 80, 30);
jbtArray[0].addActionListener(this);
jfr.add(jbtArray[0]);
jbtArray[1].setBounds(120, 40 + (jlaArry.length + 1) * 40, 80, 30);
jbtArray[1].addActionListener(this);
jfr.add(jbtArray[1]);
jbtArray[2].setBounds(210, 40 + (jlaArry.length + 1) * 40, 80, 30);
jbtArray[2].addActionListener(this);
jfr.add(jbtArray[2]);
jbtArray[3].setBounds(300, 40 + (jlaArry.length + 1) * 40, 80, 30);
jbtArray[3].addActionListener(this);
jfr.add(jbtArray[3]);
jbtArray[4].setBounds(70, 50 + (jlaArry.length + 2) * 40, 80, 30);
jbtArray[4].addActionListener(this);
jfr.add(jbtArray[4]);

jfr.setBounds(100, 100, 460, 330);
// jfr.setResizable(false);
jfr.setVisible(true);
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}/* SimpleCalculator no-arg Constructor */

public void actionPerformed(ActionEvent e) {
float n1 = 0; /* local variable should be initialized */
float n2 = 0;
boolean correct = true;/* judge if input is correct */
String message = "您的输入有误.或者输入不完整.\n 请检查后重试!";

if (e.getSource() == jbtArray[4]) {
for (int i = 0; i < jtfArray.length; i++) {
jtfArray[i].setText("");
}/* for */
}/* if clear button is pressed */
else {
try {
n1 = Float.parseFloat(jtfArray[0].getText());
n2 = Float.parseFloat(jtfArray[1].getText());

} catch (NumberFormatException nfe) {
correct = false;
JOptionPane.showMessageDialog(null, message, "出错啦!",
JOptionPane.WARNING_MESSAGE);
}/* catch */
if (correct) {

if (e.getSource() == jbtArray[0]) {
float result = n1 + n2;
jtfArray[2].setText(Float.toString(result));
}/* if Add button is pressed */
else if (e.getSource() == jbtArray[1]) {
float result = n1 - n2;
jtfArray[2].setText(Float.toString(result));
}/* if sub button is pressed */
else if (e.getSource() == jbtArray[2]) {
float result = n1 * n2;
jtfArray[2].setText(Float.toString(result));
}/* if sub button is pressed */
else if (e.getSource() == jbtArray[3]) {
float result = n1 / n2;
jtfArray[2].setText(Float.toString(result));
}/* if sub button is pressed */
}/* if input is correct */

}/* outer else not clear button is pressed */

}/* actionPerformed */

/* main method */
public static void main(String[] args) {

SimpleCalculator cal = new SimpleCalculator();

}/* main() */

}/* SimpleCalculator class */

回答2:

是统计用户输入的那一个或一组数的个数吗?是的话,这里给一个大体思想: #define maxsize 100 int count[maxsize],num[maxsize]; //定义计数器和要统计的数组 int a[要统计的数的个数]; //接着用嵌套循环,在这之前count数组全置0,num和a由用户输入 for(i=0;i<=要统计的数组元素的个数;i++){ for(j=0;j<=num数组元素的个数;j++){if a[i]=num[j] count[i]++;}}上面不是完整程序

回答3:

有不懂的请追问。
#include
#define MAX 15
#define NMAX 36

int main()
{
static int a[MAX] = { 1, 2, 3, 4, 5, 6, 8, 9, 4, 25, 35, 32, 12, 2, 5};
static int b[NMAX] = {0};
int i;
for(i = 0; i < MAX; i++)
{
b[a[i]]++;
}
for(i = 0; i < NMAX; i++)
{
if(b[i] > 0)
{
printf("%d\t:\t%d\n", i, b[i]);
}
}
return 0;
}

回答4:

你想干什么?什么是各个数字的个数?是想求一个未知数组的大小还是其他的