我想我已經互換了動態數組的第一個和最後一個數字,並且完全喪失瞭如何使用交換的數字打印數組。如何打印出已更改的動態數組
理想情況下,在程序工作的情況下,用戶應該輸入他們想要輸入的數字的數量,然後他們將分別鍵入每個數字。 然後它應該輸出(以及標準偏差,平均值和原始數組順序)該數組按順序排列,除了輸入的第一個數字和輸入的最後一個數字被切換爲。你將如何去打印切換號碼的新陣列?
這裏是我到目前爲止的代碼:
import java.util.Scanner;
import java.lang.Math;
public class Project_1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("How many numbers would you like to enter? ");
int N = scan.nextInt();
float sd, mean;
float Sum = 0;
float Square = 0;
float [] numbs = new float[N];
System.out.println("Enter your numbers below: ");
for (int i = 0; i < N; i++){
numbs[i] = scan.nextFloat();
Sum += numbs[i];
}
mean = Sum/N;
scan.close();
for (int j = 0; j < N; j++){
Square = (numbs[j] - mean) * (numbs[j] - mean);
}
sd = (float)Math.sqrt(Square/N);
System.out.println("The mean is: " + mean);
System.out.println("The standard deviation is: " + sd);
for (int k = 0; k < N; k++){
if (k == N-1){
System.out.print(numbs[k]);
}else{
System.out.print(numbs[k] + ", ");
}
}
float lastNumb = numbs[numbs.length-1];
numbs[numbs.length-1] = numbs[0];
numbs[0] = lastNumb;
}
}
如果你問如何打印數組中,有很多多餘的信息混淆了這個問題。 – shmosel
歡迎來到SO。正如@shmosel寫道:後[mcve] – c0der