簡體   English   中英

使用數組的多個if else語句

[英]Multiple if else statements using array

我想我可能已經忙得不可開交了。 最初的想法:使用數組增加已輸入的值。 工作原理:(在public static void main(String[] args)

  1. 聲明兩個數組的大小,兩個大小都相同。
  2. 較小的數組用於說明到達下一層的最小值。
  3. 較大的數組用於在層中添加特定值。
  4. 輸入一個數字。
  5. 數量是根據最小數量和增值計算的。

我認為如果使用2d數組本來可以做得更好,但是我真的不能再說了。

它應該如何工作:(3層)

 Minimum no. | Increase by this if belong to this tier
      0      |      2
      10     |      5
      20     |      10

如果輸入4,我應該得到6。
如果輸入13,我應該得到18。依此類推。

import java.util.Scanner;

public class ValueIncrease {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int tierNo;
        double value;
        double[] Req, Increase;
        System.out.printf("\nHow many tiers are there?");
        tierNo = s.nextInt();
        Req = Increase = new double[tierNo];
        System.out.printf("\nEnter the minimum amounts to reach the next tiers.");
        System.out.printf("\n(Remember to seperate by commas.)");
        s.nextLine();
        String requirement = s.nextLine();
        String req[] = requirement.split(",");
        System.out.printf("\nEnter the increase for each tier.");
        System.out.printf("\n(Seperate by commas.)");
        String ValInc = s.nextLine();
        String ValueIncrease[] = ValInc.split(",");
        for (int i = 0; i < (tierNo - 1); i++) {
            try {
                Req[i] = Double.parseDouble(req[i]);
                Increase[i] = Double.parseDouble(ValueIncrease[i]);
            } catch (NumberFormatException nfe) {
            }
        }
        System.out.printf("\nEnter value: ");
        value = s.nextDouble();
        //calculate value
        int l = Req.length;
        for (int a = 0; a < (l - 1); a++) {
            if (value >= Req[l - a]) {
                value = value + Increase[l - a];
            } else {
            }
        }
    }
}

這是固定的代碼,其中的注釋描述了我進行的所有非格式更改:

import java.util.Scanner;

public class ValueIncrease {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        int tierNo;
        double value;
        double[] minList; // changed var naming convention
        double[] incList;

        System.out.printf("\nHow many tiers are there?");
        tierNo = s.nextInt();
        // fixed allocation
        minList = new double[tierNo];
        incList = new double[tierNo];

        System.out.printf("\nEnter the minimum amounts to reach the next tiers.");
        System.out.printf("\n(Remember to seperate by commas.)");
        s.nextLine();
        String minStr = s.nextLine();
        String minStrList[] = minStr.split(",");
        System.out.printf("\nEnter the increase for each tier.");
        System.out.printf("\n(Seperate by commas.)");
        String incStr = s.nextLine();
        String incStrList[] = incStr.split(",");

        for (int i = 0; i < tierNo; i++) { // fixed loop max
            try {
                minList[i] = Double.parseDouble(minStrList[i]);
                incList[i] = Double.parseDouble(incStrList[i]);
            } catch (NumberFormatException nfe) {}
        } // end for

        while (true) { // added while loop for more efficient testing

            System.out.printf("\nEnter value (negative to exit): ");
            value = s.nextDouble();
            if (value < 0.0) break;

            // calculate value
            for (int i = tierNo-1; i >= 0; i--) { // changed loop direction
                if (value >= minList[i]) {
                    value = value + incList[i];
                    break; // added break
                } // end if
            } // end for

            System.out.printf("Result: %f", value ); // added print statement

        } // end while

    } // end main()

} // end class ValueIncrease

摘要:

  • 改進了變量命名約定,使其更加一致和更具描述性。
  • 語法a = b = ...; ab賦予相同的值(表達式...的求值結果)。 因此,如果希望它們引用單獨的分配,則不能將兩個引用變量分配給同一new表達式。 因此,我不得不將minListincList的分配分成兩個單獨的語句,每個語句都有自己的new調用。
  • 不知道為什么for循環的最大循環數是tierNo-1 ; 它應該是tierNo 條件中的比較運算符是< ,因此, i自然會從0迭代到tierNo-1 ,而無需從循環最大值中減去1。
  • 添加了while循環和print語句,可以更輕松地針對相同的層定義測試多個輸入值。
  • 將計算for循環更改為向下迭代,並在找到合適的層后中斷。

演示:

bash> ls;
ValueIncrease.java

bash> javac ValueIncrease.java;

bash> ls
ValueIncrease.class*  ValueIncrease.java;

bash> CLASSPATH=. java ValueIncrease;

How many tiers are there?3

Enter the minimum amounts to reach the next tiers.
(Remember to seperate by commas.)0,10,20

Enter the increase for each tier.
(Seperate by commas.)2,5,10

Enter value (negative to exit): 0
Result: 2.000000
Enter value (negative to exit): 1
Result: 3.000000
Enter value (negative to exit): 2
Result: 4.000000
Enter value (negative to exit): 8
Result: 10.000000
Enter value (negative to exit): 9
Result: 11.000000
Enter value (negative to exit): 10
Result: 15.000000
Enter value (negative to exit): 11
Result: 16.000000
Enter value (negative to exit): 12
Result: 17.000000
Enter value (negative to exit): 19
Result: 24.000000
Enter value (negative to exit): 20
Result: 30.000000
Enter value (negative to exit): 21
Result: 31.000000
Enter value (negative to exit): 22
Result: 32.000000
Enter value (negative to exit): 100
Result: 110.000000
Enter value (negative to exit): 3248957
Result: 3248967.000000
Enter value (negative to exit): -3

暫無
暫無

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

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