【问题标题】:Issue at sorting and showing array in AWT (Java)在 AWT (Java) 中排序和显示数组的问题
【发布时间】:2015-07-09 14:45:20
【问题描述】:

我最近注册了这个有用的网页。所以,我需要你的帮助。

我使用 AWT(使用非 Swing 组件)使用 GUI。所以。我必须使用带有随机整数的 POO(非结构化)填充一维数组,我必须从该数组中计算平均值;有了平均值,从原始数组中,我们必须选择(存储)大于平均值和小于平均值的值(我将它分别存储在两个数组中)并显示到 AWT 窗口,显示 3 个组件:

  1. 值大于平均值的列表。
  2. 值小于平均值的列表。
  3. 平均标签。

这是代码..(3个类):

package arregloawt;

public class ArregloAWT {
    public static void main(String[] args) {  
    Ventana ventana = new Ventana(); //this run the Ventana constructor
    } //this is only the main
}

处理数据的类...

package arregloawt;

import java.util.Random;
public class Media{

    public int[] vec() { //this fills the array from the method
        int vect[];
        vect = new int [20];
        Random ran = new Random();
        for (int i = 0; i < 20; i++) {
            vect[i]= ran.nextInt(100)+1;
        }
        return vect;
    }

    public int CalcMedia(){ //This calculates the Average
        int suma=0;

        for (int i = 0; i < 20; i++) {
            suma=suma+vec()[i];
        }
        return suma/20;
    }

    public int  ContMayores(){// Not Necessary
        int contM=0;
        for (int i = 0; i < 20; i++) {
            if (vec()[i]>CalcMedia()) {
                contM++;
            }
        }
        return contM;
    }

    public int [] SelecMayores(){//This Calculates who value is greater and it store in a new array
        int Mayores [];
        Mayores = new int [20];
        int j=0;
        for (int i = 0; i < 20; i++) {
            if (vec()[i]>CalcMedia()) {
                Mayores[j]= vec()[i];
                j++;
            }
        }
        return Mayores;
    }

    public int  ContMenores(){
        int contm=0;
        for (int i = 0; i < 20; i++) {
            if (vec()[i]<CalcMedia()) {
                contm++;
            }
        }
        return contm;
    }

    public int [] SelecMenores(){
        int Menores [];
        Menores = new int [20];
        int j=0;
        for (int i = 0; i < 20; i++) {
            if (vec()[i]<CalcMedia()) {
                Menores[j]= vec()[i];
                j++;
            }
        }
        return Menores;
    }


}

最后是 Frame 类....

package arregloawt;

import java.awt.*;
import java.awt.event.*;

public class Ventana extends Frame{
    Label lblTitulo = new Label();
    Label lblMedia = new Label();
    List mayores = new List(20);
    List menores = new List(20);
    Panel panel1 = new Panel();
    Media m = new Media();
    public Ventana(){
        setTitle("Mayores y Menores que la Media");
        initComponentes();
        initEvents();
        setSize(400,400);
        setResizable(false);
        setVisible(true);
    }

    private void initComponentes() {
        panel1.setLayout(new FlowLayout());
        LlenarMayores();
        LlenarMenores();
        lblMedia.setText("La Media es: "+String.valueOf(m.CalcMedia()));
        panel1.add(lblMedia);
        add(panel1);
    }

    private void initEvents() {

    addWindowListener(new ParaTerminar());}

    class ParaTerminar extends WindowAdapter
    {
    public void windowClosing(WindowEvent e)
        {System.exit(0);}
    }
    public void LlenarMayores() {

        for (int i = 0; i < 20; i++) {
            if (m.SelecMayores()[i]!=0) 
                mayores.add(String.valueOf(m.SelecMayores()[i]));
        }
        panel1.add(mayores);
    }

    public void LlenarMenores(){
        for (int i = 0; i < 20; i++) {
            if (m.SelecMenores()[i]!=0) {
                menores.add(String.valueOf(m.SelecMenores()[i]));
            }   
        }
        panel1.add(menores);
    }
}

请帮忙。结果并不令人信服。结果……

Result

我不知道出了什么问题。 :(

【问题讨论】:

  • “我一直在使用 GUI,使用 AWT(带有非 Swing 组件)。” 为什么使用 AWT?请参阅 this answer 了解放弃 AWT 使用支持 Swing 的组件的许多充分理由。

标签: java arrays swing user-interface awt


【解决方案1】:

有一个问题是您如何使用vec() 方法。您应该调用一次并将其存储在变量中,然后将其作为数组访问。你正在做的是多次调用vec(),这意味着它每次都会重新计算不同的随机数。例如,不要这样做:

Mayores[j]= vec()[i];

你应该这样做:

// Get the array early and just once. 
int[] vect = vec();
// then later when needing a number from the array...
Mayores[j]= vect[i];

通过使用vec()[i],您每次都会生成一组不同的新整数,因为vec() 返回一个新的int[],您可以从中获得ith 条目。

【讨论】:

    【解决方案2】:

    感谢@stvcisco 的回答。代码修改了Media.java类,所以新的是....

    package arregloawt;
    
    import java.util.Random;
    public class Media{
        int Vect[] = vec();
        public int[] vec() { //this fills the array from the method
            int vect[];
            vect = new int [20];
            Random ran = new Random();
            for (int i = 0; i < 20; i++) {
                vect[i]= ran.nextInt(100)+1;
            }
            return vect;
        }
    
        public int CalcMedia(){ //This calculates the Average
            int suma=0;
    
            for (int i = 0; i < 20; i++) {
                suma=suma+Vect[i];
            }
            return suma/20;
        }
    

    对于需要帮助的人。这个问题得到了回答。 The Screenshot

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 2020-11-15
      • 2016-10-14
      • 1970-01-01
      • 2019-08-20
      • 2015-07-03
      相关资源
      最近更新 更多