在 AWT 中排序和显示数组时出现问题 (Java)
Issue at sorting and showing array in AWT (Java)
我最近在这个有用的网页上注册了。所以,我需要你的帮助。
我一直在使用 GUI,使用 AWT(使用非 Swing 组件)。所以。我必须使用带有随机整数的 POO(非结构化)填充一维数组,我必须从该数组中计算平均值;有了原始数组的平均值,我们必须 select(存储)大于平均值且小于平均值的值(我将它分别存储在两个数组中)并显示给 AWT windows,显示3 个组件:
- 一个列表,其值大于平均值。
- 一个列表,其值低于平均值。
- 标签平均。
这是代码.. (3 classes):
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
}
处理数据的Class...
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;
}
}
最后是框架 class.....
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
我不知道出了什么问题。 :(
有一点不对,就是您如何使用 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()
returns 一个新的 int[]
,您可以从中得到 i
第一个条目。
感谢@stvcisco 的回答。代码修改了 class 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
我最近在这个有用的网页上注册了。所以,我需要你的帮助。
我一直在使用 GUI,使用 AWT(使用非 Swing 组件)。所以。我必须使用带有随机整数的 POO(非结构化)填充一维数组,我必须从该数组中计算平均值;有了原始数组的平均值,我们必须 select(存储)大于平均值且小于平均值的值(我将它分别存储在两个数组中)并显示给 AWT windows,显示3 个组件:
- 一个列表,其值大于平均值。
- 一个列表,其值低于平均值。
- 标签平均。
这是代码.. (3 classes):
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
}
处理数据的Class...
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;
}
}
最后是框架 class.....
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
我不知道出了什么问题。 :(
有一点不对,就是您如何使用 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()
returns 一个新的 int[]
,您可以从中得到 i
第一个条目。
感谢@stvcisco 的回答。代码修改了 class 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