[英]Multiple if else statements using array
我想我可能已经忙得不可开交了。 最初的想法:使用数组增加已输入的值。 工作原理:(在public static void main(String[] args)
)
我认为如果使用2d数组本来可以做得更好,但是我真的不能再说了。
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 = ...;
将a
和b
赋予相同的值(表达式...
的求值结果)。 因此,如果希望它们引用单独的分配,则不能将两个引用变量分配给同一new
表达式。 因此,我不得不将minList
和incList
的分配分成两个单独的语句,每个语句都有自己的new
调用。 tierNo-1
; 它应该是tierNo
。 条件中的比较运算符是<
,因此, i
自然会从0
迭代到tierNo-1
,而无需从循环最大值中减去1。 演示:
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.