简体   繁体   中英

Multiple if else statements using array

I think I may have messed up big time. Original idea: Use arrays to increase values that have been inputted. How it works: (inside public static void main(String[] args) )

  1. declare the size of the 2 arrays, both same size.
  2. smaller array is used to state the minimum to reach the next tier.
  3. larger array is used to add a specific value if it is in a tier.
  4. a number is typed in.
  5. number is calculated based on minimum amount and value increase.

I think that I could have done better if I had used a 2d array, but I can't really tell anymore.

How it's supposed to work: (for 3 tiers)

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

If I enter 4, I should get 6.
If I enter 13, I should get 18. And so on.

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 {
            }
        }
    }
}

Here's fixed code, with comments describing all the non-formatting changes I made:

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

Summary:

  • Improved the variable naming convention to be more consistent and descriptive.
  • The syntax a = b = ...; assigns a and b to the same value (the result of evaluation of the expression ... ). Thus, you cannot assign two reference variables to the same new expression, if you want them to refer to separate allocations. Hence I had to separate the allocation of minList and incList into two separate statements, each with its own new call.
  • Not sure why your loop max on the parsing for-loop was tierNo-1 ; it should be tierNo . The comparison operator in the conditional is < , thus, i will iterate from 0 to tierNo-1 naturally, with no need to subtract one from the loop max.
  • Added a while-loop and print statement for easier testing of multiple input values against the same tier definition.
  • Changed the calculation for-loop to iterate downwards and break once a suitable tier has been found.

Demo:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM