簡體   English   中英

有人可以解釋一下這段代碼嗎? 關於用戶輸入、掃描儀等

[英]Can someone explain me this code? About user input, scanner etc

有人可以向我解釋這段代碼是如何工作的嗎?

它允許用戶輸入直到1000的數字,然后將原始輸入的數字(偶數和奇數)打印在一個單獨的數組中。 但是我只是不明白當它輸出偶數和奇數而不是偶數和奇數的數量時有gem++gem1++的部分。

在放了這個之后

double even[] = new double[gem];
double odd[] = new double [gem1]; 

為什么需要再次重復gem=0gem1=0 如果我問了太多問題,我很抱歉,我只是很困惑,我上周剛剛學習了 java。

public class wutt {
    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        System.out.print("Enter no. of elements you want in array : ");
        int n = s.nextInt();

        if (1 <= n && n <= 1000) {

            double a[] = new double[n];
            int gem = 0, gem1 = 0;

            System.out.println("Enter all the elements : ");
            for (int i = 0; i < n; i++) {
                a[i] = s.nextInt();
                if (a[i] % 2 == 0)
                    gem++;
                else
                    gem1++;
            }

            double even[] = new double[gem];
            double odd[] = new double[gem1];

            gem = 0;
            gem1 = 0;

            for (int i = 0; i < a.length; i++) {
                if (a[i] % 2 == 0) {
                    even[gem] = a[i];
                    gem++;
                } else {
                    odd[gem1] = a[i];
                    gem1++;
                }
            }

            System.out.println("Original: " + Arrays.toString(a));
            System.out.println("Odd: " + Arrays.toString(odd));
            System.out.println("Even: " + Arrays.toString(even));

        } else
            System.out.print("Invalid input");
    }
}

如果您希望程序在用戶輸入大於 1000 或小於 0 的數字后停止,您需要在if條件中添加break語句。

if (size < 0 || size > 1000) {
    System.out.println("Size must be between 0 and 1000");
    break;
}

double even[] = new double[gem]; double odd[] = new double [gem1]; double even[] = new double[gem]; double odd[] = new double [gem1]; 正在嘗試獲取發生的奇數和偶數,並將所有輸入的元素放入數組a中。 畢竟,現在我們得到的是一個稱為a的數字數組,其中包含所有輸入的元素。 還有兩個數字 gem 和 gem1,其中包含發生奇數的次數和發生偶數的次數。 所以我們得到 gem(numberOfEvens), gem1(numberOfOdds) 並列出a

接下來,我們需要將 a 中的所有賠率放入a名為 odd[] 且大小為 gem1 的新數組中,並將a中的所有偶數放入一個名為 even[] 且大小為 gem 的新數組中。 至此,變量gem1和gem的職責就完成了。 他們變得無用。

現在我們需要通過列表 go 並挑選奇數和偶數並按順序將它們一個一個放入數組中。 這就是為什么我們需要兩個初始化為 0 的新變量。 在這種情況下,因為 gem 和 gem1 是無用的區域,它們被重新分配以幫助操作樹 arrays aodd[]even[]

所以用戶在數組中輸入他/她想要的元素數量( n

double a[] = new double[n];   // a is the array that is initialised to accommodate n elements
int gem = 0, gem1 = 0;        // gem is the counter for "even" numbers and "gem1" the counter for odd numbers, and like every good counter, they start at 0

System.out.println("Enter all the elements : ");
for (int i = 0; i < n; i++) {     // so we ask the user to input n elements
    a[i] = s.nextInt();           // here we read every input and put it in the a array
    if (a[i] % 2 == 0)            // if the new number is even
        gem++;                    // we increase the even counter "gem"
    else                          // otherwise, when it is an odd number
        gem1++;                   // we increase the odd counter
}

double even[] = new double[gem];  // now we create a new array where we want to hold all the even numbers, we do that by telling it how many even numbers we have counted before (gem)
double odd[] = new double[gem1];  // and a new array for all odd numbers (gem1 was our counter)

gem = 0;                          // now we reinitialise the counters, because we want to start from the beginning
gem1 = 0;

for (int i = 0; i < a.length; i++) { // in order to copy all numbers from the a array into the two other arrays for even and odd numbers, we iterate over the whole length of the a array. i is the index for the "a" array
    if (a[i] % 2 == 0) {             // ever even number we encounter
        even[gem] = a[i];            // we put in the even array
        gem++;                       // while gem, the "even numbers counter" is our index for the "even" array
    } else {
        odd[gem1] = a[i];            // odd numbers are for the odd array
        gem1++;                      // while the former "odd numbers counter" now serves as our "odd" array index
    }
}

差不多就是這樣。 首先,用戶在一個數組中輸入所有數字,然后簡單地計算輸入了多少奇數和多少偶數,

然后創建了兩個新的 arrays,一個用於偶數,一個用於奇數,由於我們計算了它們,我們知道這兩個新的 arrays 必須有多大。

最后,所有數字再次迭代並放入相應的數組中。 最后你有 3 個數組,一個包含所有數字,一個包含偶數,一個僅包含奇數

編輯

在不改變該方法的性質的情況下,您可以進行一些小的更改:

double allNumbers[] = new double[n];  // "allNumbers" is way more specific than "a"
int oddCounter = 0;                   // "oddCounter" instead of "gem"
int evenCounter = 0;                  // numbers in variables like "gem1" is really bad practice, because numbers don't say anything about the nature of the variable

System.out.println("Enter all the elements : ");

for (int i = 0; i < n; i++) {
    allNumbers[i] = s.nextInt();
    if (allNumbers[i] % 2 == 0) {
        evenCounter++;
    } else {
        oddCounter++;
    }
}
// until here nothing changes but the names

double even[] = new double[evenCounter];
double odd[] = new double[oddCounter];

int oddIndex = 0;                   // and here we create new variables, instead of reusing old ones
int evenIndex = 0;                  // there is absolutely no performance gain in reusing primitives like this - it's just confusing
for (int i = 0; i < allNumbers.length; i++) {
    if (allNumbers[i] % 2 == 0) {
        even[evenIndex++] = allNumbers[i];   // the "++" can be done directly in the first expression. that's just to make it shorter.
    } else {
        odd[oddIndex++] = allNumbers[i];     // it is not more performant nor easier to read - just shorter
    }
}

編輯(再次)

這就是 arrays 的樣子,比如說當你輸入 4 個數字時:

gem = 0
gem1 = 0
n = 4               // user said 4 
a = [ , , , ]       // array a is empty but holds the space for 4 numbers

a = [1, , , ]       // user enters 1
     ^
    i=0

gem1 = 1            // 1 is an odd number -> gem1++

a = [1,4, , ]       // user entered "4"
       ^
      i=1
gem = 1             // 4 is an even number -> gem++

a = [1,4,2, ]       // user entered "2"
         ^
        i=2
gem = 2             // 24 is an even number -> gem++

a = [1,4,2,7]       // user entered "7"
           ^
          i=3
gem1 = 2             // 7 is an odd number -> gem1++


then we fill the other arrays

even = [ , ]          // gem is 2, so we have 2 even numbers
odd  = [ , ]          // gem1 is 2, so we have 2 odd numbers

a = [1,4,2,7]
     ^ 
    i=0

odd[1, ]    // for i=0, a[i] is 1, which is an odd number

a = [1,4,2,7]
       ^ 
      i=1

even = [4, ]    // for i=1, a[i] is 4, which is an even number

a = [1,4,2,7]
         ^ 
        i=2

even = [4,2]    // for i=2, a[i] is 2, which is an even number

a = [1,4,2,7]
           ^ 
          i=3

odd = [1,7]    // for i=3, a[i] is 7, which is an odd number

and in the end you have

a    = [1,4,2,7]
even = [4,2]
odd  = [1,7]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM