[英]Java - print user input of 4 integers, and the lowest, highest and average number
[英]Gather an array from the user, print the smallest number, highest number, calculate average, and median
我在此程序中進展順利,但是隨着我的進步,我似乎犯了一些邏輯錯誤,這些錯誤很難找到且需要幫助。 我有一些方法可以從最小到最大對數組進行排序。 每當我在屏幕上打印最小的數字時,即使我沒有輸入任何零,該數字也始終顯示為0。 我可以得到正確的最高數字,除非用戶輸入的數字最大,然后打印第二高數字。 有時我得到正確的中位數輸出,但從未獲得正確的中位數輸出。 任何幫助是極大的贊賞! 我覺得自己已經接近正確的代碼,但是這些錯誤使我很難受。
public static Scanner kbd = new Scanner(System.in);
public static void main(String[] args) {
//change to 100 when done testing
final int MAXSIZE = 10;
int[] nums = new int [MAXSIZE];
int usedSize, indexOfNextSmallest = 0;
double median, average;
System.out.println("Please enter each number starting from least to greatest(a negative number will quit input): ");
usedSize = getNums(nums);
for (int index = 0; index < nums.length -1; index++) {
indexOfNextSmallest = getIndexOfSmallest(index, nums);
interchange(index, indexOfNextSmallest, nums);
}
median = medians(nums);
average = averages(nums);
System.out.println("The smallest number entered is " + nums[0] + ".");
System.out.println("The largest number entered is " + nums[nums.length-1] + ".");
System.out.println("The median is: " + median);
System.out.println("The average is: " + average);
}
public static int getIndexOfSmallest(int startIndex, int[] nums) {
int min = nums[startIndex];
int indexOfMin = startIndex;
for (int index = startIndex +1; index < nums.length; index++) {
if (nums[index] < min) {
min = nums[index];
indexOfMin = index;
}
}
return indexOfMin;
}
private static void interchange(int index, int indexOfNextSmallest, int[] nums) {
int temp = nums[index];
nums[index] = nums [indexOfNextSmallest];
nums[indexOfNextSmallest] = temp;
}
public static int getNums(int nums[]) {
int usedSize = 0, userValue = 0;
while(userValue >= 0 && usedSize < nums.length) {
nums[usedSize] = userValue;
userValue = kbd.nextInt();
usedSize++;
}
if(!(userValue >= 0)) {
--usedSize;
System.out.println(usedSize + " numbers entered.");
}
else if(!(usedSize < nums.length)) {
System.out.println("Maximum amount of inputs (" + nums.length + ") reached.");
}
return usedSize;
}
public static double medians(int nums[]) {
double median;
if (nums.length % 2 == 0)
median = ((double)nums[nums.length / 2] + (double)nums[nums.length / 2 - 1]) / 2;
else
median = (double)nums[nums.length / 2];
return median;
}
public static double averages(int nums[]) {
double average;
int sum = 0;
for (int index = 0; index < nums.length; index++){
sum = sum + nums[index];
}
average = ((double)sum / (double)nums.length);
return average;
}
}這是我輸入1、2、3、4、5,-7時得到的輸出(否定是停止用戶輸入(可能有問題嗎?))
Please enter each number starting from least to greatest(a negative number will quit input):
1 2 3 4 5 -7
5 numbers entered.
The smallest number entered is 0.
The largest number entered is 5.
The median is: 0.5
The average is: 1.5
我應該用正確的代碼得到的答案是1、5、3.0和3.0
再次感謝您的幫助。
整個過程有兩個地方:
第一個
final int MAXSIZE = 10;
int[] nums = new int [MAXSIZE];
這意味着即使程序停止接受負值之后的值; 數組的其余所有部分都填充為0。
若要解決此問題,可以選擇在int數組上使用ArrrayList
。
第二期
getnums的現有代碼是
public static int getNums(int nums[]) {
int usedSize = 0, userValue = 0;
while(userValue >= 0 && usedSize < nums.length) {
nums[usedSize] = userValue;
userValue = kbd.nextInt();
usedSize++;
}
if(!(userValue >= 0)) {
--usedSize;
System.out.println(usedSize + " numbers entered.");
}
else if(!(usedSize < nums.length)) {
System.out.println("Maximum amount of inputs (" + nums.length + ") reached.");
}
return usedSize;
}
在while循環中,語句
nums[usedSize] = userValue;
userValue = kbd.nextInt();
將確保num [0]處的值始終為零(因為userValue初始化為0),並且不會從用戶輸入中獲取該值。
相反,它應該是:
while(userValue >= 0 && usedSize < nums.length) {
userValue = kbd.nextInt();
nums[usedSize] = userValue;
usedSize++;
}
如果您照顧好這兩個問題; 那么其余的代碼應該可以正常工作。
這是我更新第二個問題的程序后運行該程序的結果:
輸入
Please enter each number starting from least to greatest(a negative number will quit input):
5
8
4
32
-5
產量
The smallest number entered is -5.
The largest number entered is 32.
The median is: 0.0
The average is: 4.4
這是正確的,因為計算了10個數字的平均值和中位數,其余數字(輸入負數之后)僅為0
根據評論更新
如果您只想拒絕負數,則可以將getnums
方法中的while循環更新為:
while(userValue >= 0 && usedSize < nums.length) {
userValue = kbd.nextInt();
if(userValue >= 0) {
nums[usedSize] = userValue;
usedSize++;
}
}
另外,此后的if循環不應減少usedSize
的值
if(!(userValue >= 0)) {
System.out.println(usedSize + " numbers entered.");
}
您的medians()
和averages()
方法看起來不錯。 我建議您擺脫getIndexOfSmallest()
和interchange()
方法。 您表面上僅需要這些方法,因為您正在嘗試排序。 但是我相信排序會改變數組。 使用以下方法找到最小值:
public int getMin(int[] nums) {
int min = nums[0];
for (int i=1; i < nums.length; ++i) {
if (nums[i] < min) {
min = nums[i];
}
}
return min;
}
我將其留作家庭作業,以供您編寫一種方法來查找最大值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.