java - 我正在尝试创建一个将显示有关一组整数的信息的类,但我无法调用我的类
问题描述
好的,我试图理解你的答案并更改了我的代码,但我仍然遇到类似的问题。我正在尝试将IntArray构造函数调用到我的IntMath构造函数中,因为我需要一些变量才能使我的数学工作。之后,我试图将两个构造函数都调用到我的 Main 类中。然而,它告诉我,我所有的变量“可能都没有被初始化。我对此很陌生,只是感到愚蠢和无助。我已经疯狂地使用谷歌了。提前感谢你的帮助。
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//int sum, even, odd, min, max, size;
//int [] intArray;
//double average;
//int even = 0, odd = 0, min = 0, max = 0, size = 0, sum = 0;
//double average;
System.out.print("Please enter a number for the array size (zero or greater): ");
int n=in.nextInt();
if (n<0){
System.out.println("ERROR: Number must be at least zero!!!");
}else {
IntArray mainArray = new IntArray(n);
}
//com.company.IntMath intMath = new com.company.IntMath(average, even, odd, min, max, size, sum);
IntMath intMath = new IntMath();
//intMath.getAverage();
intMath.getEven();
intMath.getOdd();
intMath.getMin();
// intMath.getMax();
intMath.getSize();
intMath.getSum();
System.out.println("Average: " + intMath.getAverage() ); //print the average of the elements
System.out.println("Even Count: " + intMath.getEven()); //prints the count of all even numbers
System.out.println("Odd Count: " + intMath.getOdd()); //prints the count of all odd numbers
System.out.println("Min: " + intMath.getMin()); //prints the min number
//System.out.println("Max: " + intMath.getMax()); //prints the max number
System.out.println("Size: " + intMath.getSize()); //prints the size of the array
System.out.println("Sum: " + intMath.getSum()); //prints the sum of all elements
}
}
package com.company;
import java.util.Arrays;
import java.util.Random;
public class IntArray {
private int n;
private int [] intArray;
private double average;
private int sum;
private int even;
private int odd;
private int max;
private int min;
private int size;
public IntArray(int n) {
this.n = n;
Random randArray = new Random();
int[] intArray = new int[n];
intArray[0] = 0; //why does this not make the element 0 a 0?????????
for (int i = 0; i < n; i++) {
intArray[i] = randArray.nextInt(50); //I was getting very big random numbers, so I set the max to 50
}
System.out.println("Set: " + Arrays.toString(intArray));
}
public double getAverage() {
average = 0;
double total = 0;
for (int i = 0; i < n; i++) {
total = total + intArray[i];
}
return average;
}
public int getSum() {
//find the sum of all elements
sum = 0;
for (int i = 0; i < n; i++) {
sum += intArray[i];
}
return sum;
}
public int getEven() {
even = 0;
for (int i = 0; i < n; i++) {
if (intArray[i] % 2 == 0) {
even++;
}
}
return even;
}
public int getOdd() {
odd = 0;
for (int i = 0; i < n; i++) {
if (intArray[i] % 2 != 0) {
odd++;
}
}
return odd;
}
public int getMax() {
max = intArray[0];
for (int i = 1; i < n; i++) {
if (intArray[i] > max) {
max = intArray[i];
}
}
return max;
}
public int getMin() {
min = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < intArray.length; j++) {
if (intArray[i] > intArray[j]) {
min = intArray[i];
intArray[i] = intArray[j];
intArray[j] = min;
}
}
}
return min;
}
public int getSize() {
//find the size of the array
size = n + 1;
return size;
}
}
package com.company;
public class IntMath {
private double average;
private int sum;
private int even;
private int odd;
private int max;
private int min;
private int size;
private int[] intArray;
private int n;
//com.company.IntArray mainArray = new com.company.IntArray(n); //call IntArray to get variables ''n'' and ''intArray''
/*public IntMath(double average, int sum, int even, int odd, int max, int min, int size, int[] intArray) {
this.average = average;
this.sum = sum;
this.even = even;
this.odd = odd;
this.max = max;
this.min = min;
this.size = size;
this.intArray = intArray;
com.company.IntArray mainArray = new com.company.IntArray(n); //call IntArray to get variables ''n'' and ''intArray''
// average = 0;
// double total = 0;
// for (int i = 0; i < n; i++) {
// total = total + intArray[i];
// }
//find the sum of all elements
//sum = 0;
//for (int i = 0; i < n; i++) {
// sum += intArray[i];
//}
//find the count of the even numbers
//even = 0;
//for (int i = 0; i < n; i++) {
// if (intArray[i] % 2 == 0) {
// even++;
// }
//}
//find the count of the odd numbers
//odd = 0;
//for (int i = 0; i < n; i++) {
// if (intArray[i] % 2 != 0) {
// odd++;
// }
//}
//find the biggest number
//max = intArray[0];
//for (int i = 1; i < n; i++) {
// if (intArray[i] > max) {
// max = intArray[i];
// }
//}
//find the smallest number
//min = 0;
//for (int i = 0; i < n; i++) {
// for (int j = i + 1; j < intArray.length; j++) {
// if (intArray[i] > intArray[j]) {
// min = intArray[i];
// intArray[i] = intArray[j];
// intArray[j] = min;
// }
// }
/
//find the size of the array
//size = n + 1;
*/
public double getAverage() {
average = 0;
double total = 0;
for (int i = 0; i < n; i++) {
total = total + intArray[i];
}
return average;
}
public int getSum() {
//find the sum of all elements
sum = 0;
for (int i = 0; i < n; i++) {
sum += intArray[i];
}
return sum;
}
public int getEven() {
even = 0;
for (int i = 0; i < n; i++) {
if (intArray[i] % 2 == 0) {
even++;
}
}
return even;
}
public int getOdd() {
odd = 0;
for (int i = 0; i < n; i++) {
if (intArray[i] % 2 != 0) {
odd++;
}
}
return odd;
}
public int getMax() {
max = intArray[0];
for (int i = 1; i < n; i++) {
if (intArray[i] > max) {
max = intArray[i];
}
}
return max;
}
public int getMin() {
min = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < intArray.length; j++) {
if (intArray[i] > intArray[j]) {
min = intArray[i];
intArray[i] = intArray[j];
intArray[j] = min;
}
}
}
return min;
}
public int getSize() {
//find the size of the array
size = n + 1;
return size;
}
}
解决方案
在 Main 类中,如果您初始化所有变量,则该类将编译而不会出错。
通过学习你的课程,我想你可能想做的是,
- 接受一个数字以创建该大小的数组并用一些随机数填充数组
- 接下来,您要使用已填充的数组执行各种操作,例如求平均值、求和等。
如果是这种情况,那么你将不得不做这样的事情......
- 实际上,您不需要 IntArray 类。
- 将 int[] intArray 声明为 IntMath 类中的类级别变量,并像现在一样在 IntArray 类的构造函数中填充该数组。
- 在 IntMath 类中为每个操作定义多个方法..average、sum 等,并在打印答案时在 Main 类中调用它们。
- 您必须更正 getAverage、getSize 和 getMin 方法中的代码。
推荐阅读
- javascript - WriteStream 不会写入所有数据
- php - 如何向 JSON Laravel 分页对象添加元素
- javascript - 将 onclicklistener 重新附加到按钮
- python-3.x - 如何根据具有多个 ID 的另一列从一列中选择特定值
- ggplot2 - ggplot中的线图平滑
- java - 应用 onItemClickListener 后,我的列表视图显示为 null
- arrays - 在 angular2+ 中使用嵌套数组的问题
- sql-server - 任何序列的字符串搜索
- arcore - Arcore 未能从目标图像中获取足够的关键点
- node.js - mongodb节点组集合