If i use if statements there will be at least 100 of them. If i use switch/case it doesnāt seem as straight forward. How would i check to see if this number or that number?
switch(inputA){
case 1:
switch(inputB){
case 1:
//code to process
break;
case 2:
//code to process
break;
...
case 2:
switch(inputB){
case 1:
//code to process
break;
case 2:
//code to process
break;
...
case 3:
switch(inputB){
case 1:
//code to process
break;
case 2:
//code to process
break;
...
...
}
HTH,
PS⦠or, if the inputs are both 1-10, you could make an array, and use the inputs of each as the indexes. (ie, thisArray[inputA][inputB] - if A=3 and B=5, then the value of thisArray[3][5].
No. The ācode to processā is what you indicated in your original post. āIf inputA is 1 and inputB is 3 then 5ā The āthen 5ā would be the code that goes in //code to process.
I doubt whoever gave you this problem listed out all 100 possible combinations in a chart, so there must be some programmatic/mathematical principle behind it.
Given the two examples you gave, it would be Result = B+(B-A). But thatās only 2 examples out of 100.
This is a little program that will assess the input of a form.
If the water is 10 degrees and the air is 30 degrees then it will pull the result out of MySQL by ID.
There are ten for inputA and ten for inputB, so 100 possible combinations. I could write out 100 if/then statements but i know there has to be a better way. I just canāt get my head around the solution.
Then calculate the value you need to find your product recommendation. What would you query for in the product database to find the recommended products?
What weāre trying to get at is: consolidate the number of cases you have to deal with.
If there is absolutely no pattern to the value of the result and all of the values are different, then yes, youāll either have to do 100 if/else clauses, 100 case switches, or a 10x10 multidimensional array.