[英]Struggling to calculate average, min and max in java
我目前正努力計算編碼中的平均值,最小值和最大值。 我收到很多錯誤,不確定我做錯了什么。 對不起,如果我沒有提供足夠的信息,我會在需要時更新我的帖子。 此外,如果您能解釋為什么使用該代碼以便我能理解,我將不勝感激。 謝謝。
編輯 - 我想要它做的是當用戶輸入一組數字並完成輸入數字時,我希望它顯示用戶在直方圖后輸入的數字的平均值以及最大值和最小值。 我假設我必須使用我的count變量以及我的num變量,但仍然在努力實現它。
我收到的錯誤顯示在我編碼的下面的編碼中。
import java.util.Scanner;
public class Histogram1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] array = new int[5000];
int num = 0;
int count = 0;
int total = 1;
int max = 0;
int min = 0;
System.out.println ("Enter students marks in the range 0 to 100\n");
loop: for (count = 0; count <= total; count++)
{
System.out.println ("Enter a number:");
num = scan.nextInt();
if (num < 0 || num > 100)
{
break loop;
}
array[count] = num;
total = count+1;
}
System.out.println ("How many times a number between 0-100 occur.");
String[] asterisk = {"0- 29 | ", "30- 39 | ","40- 69 | ", "70- 100 | "}; //4 strings
for (count = 1; count <= total; count++)
{
num=array[count];
if (num >=0 && num <=29) asterisk [0] +="*";
else if (num>29 && num <=39) asterisk[1] +="*";
else if (num>39 && num <=69) asterisk[2] +="*";
else if (num>69 && num <=100) asterisk[3] +="*";
}
for (count =0;count < 4;count++)
System.out.println(asterisk[count]);
System.out.println("The total amount of students is " + total);
**int cannot be dereferenced** for (int i = 0; i < count.length; i++) {
**array required, but int found** num += count[i];
**array required, but int found** if (min > num[i]) {
**array required, but int found** min = num[i];
}
**array required, but int found** if (max < num[i]) {
**array required, but int found** max = num[i];
}
}
**int cannot be dereferenced** double average = (double) num / count.length;
System.out.printf(" min: " + min);
System.out.printf("%n max: " + max);
System.out.printf("%naverage: %.1f", average);
}
}
如果您有一個數組,通常首先通過將初始最小值設置為已知最大值“無窮大”或在這種情況下為100來計算最小值。下一步將迭代您的值集並檢查值是否低於當前值最小值,如果設置為最小值,則為該值。
int min = 100; for (int i = 0; i < total; i++) { if (array[i] < min) { min = array[i]; } }
為了達到最大值,反之,將初始最大值設置為0或負“無窮大”,並檢查數組中的值是否大於當前最大值。
對於平均值,將所有值相加並將結果除以數組中元素的總量。
int sum = 0; for (int i = 0; i < total; i++) { sum += array[i]; } double avg = (double) (sum) / total;
在java正無窮大中,整數類型表示為Integer.MAX_VALUE
和負無窮大: Integer.MIN_VALUE
。
假設我們有一個值數組:int [] values = {some values}
要計算平均值:
按要素數除以和
int sum = 0; for(int i : values) { sum += i; } double average = (double)(sum) / values.length;
找到最小值和最大值:
循環遍歷數組並將當前元素與min和max進行比較並適當地設置它們
int max = -2147483648; //set to min int value, -2^31 int min = 2147483647; //set to max int value, 2^31 - 1 for (int i : values) { if (i > max) max = i; if (i < min) min = i; }
我看到其他帖子已經回答了如何計算數組中的平均值,最小值和最大值,所以我只會用你的代碼解決錯誤。
您收到一個錯誤,指出int cannot be dereferenced
:
tmp.java:48: int cannot be dereferenced
for (int i = 0; i < count.length; i++) {
^
這源於您將count定義為int
而不是數組的事實。 實際上,所有的編譯錯誤都來自這里。 變量count
不會有長度,也不能訪問它的元素(它沒有)。 在代碼的最后部分(出現錯誤的地方),您將count,min和max混淆為數組,而它們實際上只是整數。 你的代碼很接近(有正確的想法),但我認為你可能會混淆一些語法。
因為你的數組長度總是5000,所以我不得不使用第二個for循環來計算最小數量。 否則,如果我要使用第一個for循環(現在已注釋),最小數字將始終為0(如果輸入少於5000個數字),因為總會有尾隨元素= 0.我建議使用相反, ArrayList ,但我的代碼使用您創建的相同數組。
我評論了我在代碼中更改/添加的所有內容:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] array = new int[5000];
int num = 0;
int count = 0;
int total = 0; // start this at 0, in case no inputs are valid
double sum = 0; // make this a double to get a decimal average
int max = 0; // start at the lowest possible number for max
int min = 100; // start at the highest possible number for min
double average = 0;
System.out.println ("Enter students marks in the range 0 to 100\n");
loop: for (count = 0; count <= total; count++)
{
System.out.println ("Enter a number:");
num = scan.nextInt();
if (num < 0 || num > 100 || total == 5000) // can't enter more than 5000 (array length restriction)
{
break loop;
}
array[count] = num;
sum += num; // keep track of the sum during input
total = count+1;
}
System.out.println ("How many times a number between 0-100 occur.");
String[] asterisk = {"0- 29 | ", "30- 39 | ","40- 69 | ", "70- 100 | "}; //4 strings
for (count = 1; count <= total; count++)
{
num=array[count];
if (num >=0 && num <=29) asterisk [0] +="*";
else if (num>29 && num <=39) asterisk[1] +="*";
else if (num>39 && num <=69) asterisk[2] +="*";
else if (num>69 && num <=100) asterisk[3] +="*";
}
for (count =0;count < 4;count++)
System.out.println(asterisk[count]);
System.out.println("The total amount of students is " + total);
// calculate the average
average = sum / total;
// calculate the min and max
// use this for loop if the length of the array is
// the amount of the inputs from the user
/*for (int i : array) // for every int in the array of inputted ints
{
if (i > max) max = i; // if this int in array is > the last recording max number, set the new max number to be this int
if (i < min) min = i; // if this int in array is < the last recording min number, set the new min number to be this int
}*/
for (int i = 0; i < total; i++) // for every inputted int ( < total so that you exclude the trailing elements that are = 0)
{
if (array[i] > max) max = array[i]; // if this int in array is > the last recording max number, set the new max number to be this int
if (array[i] < min) min = array[i]; // if this int in array is < the last recording min number, set the new min number to be this int
}
// my way of printing results
//System.out.println("\n Average: " + average);
//System.out.println("Max: " + max + "\n Min: " + min);
System.out.printf(" min: " + min);
System.out.printf("%n max: " + max);
System.out.printf("%naverage: %.1f", average);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.