我的目标是让它输出的最小最大avg和条形图。
`
package arraystuff1;
import java.util.Random;
public class problem1
{
public static void main(String[] args)
{
int min=0;
int max=0;
int avg=0;
//initialize array
Random randomNumbers = new Random();
final int Length = 30;
int[] array1 = new int[Length];
for ( int counter = 0; counter < array1.length; counter++ )
{
array1[ counter ] = 1+randomNumbers.nextInt( 100 );
}
//array tested and produces correct output
for ( int counter = 0; counter < array1.length; counter++ )//minimum
{
min = functionsp1.min(array1[counter],min);
if(counter == Length-1)
System.out.printf("The minimum value is: %d\n",min);
}
for ( int counter = 0; counter < array1.length; counter++ )//maximum
{
max = functionsp1.max(array1[counter],max);
if(counter == Length-1)
System.out.printf("The maximum value is: %d\n",max);
}
for ( int counter = 0; counter < array1.length; counter++ )//average
{
avg = functionsp1.avg(array1[counter],Length);
if(counter == Length-1)
System.out.printf("The average value is: %d\n",avg);
}
for ( int counter = 0; counter < array1.length; counter++ )//average
{
functionsp1.bar(array1[counter],Length);//print a line showing range of the bar graph use switch case to build frequencies for each 10th unit
functionsp1.freq(array1[counter],Length);//print stars representing number of random numbers that fit in this category
}
}
}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
`
package arraystuff1;
public class functionsp1
{
public static int min(int counter,int min)//input array index and length
{
if (counter < min)
{
counter = min;
}
return counter;
}
public static int max(int counter,int max)
{
if (counter > max)
{
counter = max;
}
return counter;
}
public static int avg(int current,int total)//input array length for total and index for current
{
int sum;
int fintotal;
for (int counter = 0; counter < total; counter++ )
{
sum = current + sum; // random number 0-99 add 1 to make it 1-100
if (counter == total)
fintotal = sum/total;
return fintotal;
}
System.out.printf("The average of the array is: %2d",fintotal);
//System.out.printf( "%s%8s\n", "Index", "Value" ); // column headings
return fintotal;
}
public static void bar(int index,int limit)//length and array variable
{
System.out.println("Grade Distribution: ");
for(int counter = 0; counter < limit;counter++)
{
if (counter ==10)
System.out.printf("%5d: ", 100);
else
System.out.printf("%02d-%02d: ",counter*10,counter*10+9);
}
}
public static void freq( int stars )
{
for(int counter=0;counter<stars ; counter ++)
System.out.print("*");
} // end main
}`
我已经验证了在1-100范围内用30个随机数初始化的数组.我的目标是让条形图和其他功能发挥作用,但这是我第一次使用数组,有什么建议吗?
发布于 2022-10-26 22:48:00
让您的Functionsp1类方法处理数组,例如:
//initialize array
java.util.Random randomNumbers = new java.util.Random();
final int length = 30;
int[] array1 = new int[length];
for (int counter = 0; counter < array1.length; counter++) {
array1[counter] = randomNumbers.nextInt(100) + 1;
}
//array tested and produces correct output
Functionsp1 fnctn = new Functionsp1();
int min = fnctn.min(array1);
int max = fnctn.max(array1);
int avg = fnctn.avg(array1);
System.out.printf("The minimum value in array1 is: %d%n", min);
System.out.printf("The maximum value in array1 is: %d\n", max);
System.out.printf("The average value in array1 is: %d\n", avg);
System.out.println("Horizontal Graph For Numerical Range: " + min + " To " + max);
fnctn.bar(array1);
public class Functionsp1 {
public int min(int[] array) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
return min;
}
public int max(int[] array) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
public int avg(int[] array) {
int avg = 0;
int sum = 0;
for (int i : array) {
sum += i;
}
avg = sum / array.length;
return avg;
}
public void bar(int[] array) {
System.out.println();
System.out.println("Grade Distribution:");
System.out.println("0 0 10 20 30 40 50"
+ " 60 70 80 90 100");
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < array.length; i++) {
int counter = 0;
for (int j = 0; j < array[i]; j++) {
if ((counter + 1) % 10 == 0) {
sb.append("+");
}
else {
sb.append("|");
}
counter++;
}
System.out.printf("%-3s%-103s(%-3s)%n", (i+1), sb.toString(), array[i]);
sb.setLength(0);
//System.out.printf("%-3s%-100s%n", (i + 1), String.join("", java.util.Collections.nCopies(array[i], "|")));
}
System.out.println();
}
public void freq(int stars) {
for (int counter = 0; counter < stars; counter++) {
System.out.print("*");
}
}
}我仍然不知道freq()方法应该完成什么任务。
正如上面的代码现在所显示的那样,如果运行将产生类似于控制台窗口中的以下内容:
The minimum value in array1 is: 1
The maximum value in array1 is: 99
The average value in array1 is: 55
Horizontal Graph For Numerical Range: 1 To 99
Grade Distribution:
0 0 10 20 30 40 50 60 70 80 90 100
1 |||||||||+|||| (14 )
2 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||||| (55 )
3 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+ (90 )
4 ||||| (5 )
5 |||||||||+ (10 )
6 |||||||||+|||||||||+|||||||| (28 )
7 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||| (94 )
8 |||||||||+|||||||||+||||||| (27 )
9 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+| (91 )
10 |||||||||+|||||||||+||||||||| (29 )
11 |||||||||+|||||||||+|||||||||+|||||||||+||||||||| (49 )
12 |||||||||+|||||||||+|||||||||+|||||||||+|| (42 )
13 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||||||| (97 )
14 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||| (94 )
15 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||||||||| (99 )
16 | (1 )
17 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||||||||| (79 )
18 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||||| (95 )
19 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+ (60 )
20 |||||||||+|||||||||+|||||||||+|||||||||+| (41 )
21 |||||||||+||| (13 )
22 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|| (72 )
23 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||| (73 )
24 |||||||||+|||||||||+||||||| (27 )
25 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+ (80 )
26 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|| (82 )
27 |||||||||+|||||||||+|||||||||+|||| (34 )
28 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||| (98 )
29 |||||||||+|||||||||+|||||||||+ (30 )
30 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||| (54 )https://stackoverflow.com/questions/74202688
复制相似问题