gpt4 book ai didi

java - 使用数组的多个 if else 语句

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:00 25 4
gpt4 key购买 nike

我想我可能搞砸了。最初的想法:使用数组来增加已经输入的值。它是如何工作的:(在public static void main(String[] args)内)

  1. 声明两个数组的大小,两者大小相同。
  2. 较小的数组用于说明到达下一层的最小值。
  3. 如果特定值位于层中,则使用较大的数组来添加该值。
  4. 已输入一个数字。
  5. 数量是根据最低金额和增值额计算的。

我认为如果我使用二维数组,我可以做得更好,但我真的不能再告诉了。

它应该如何工作:(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当然,不需要从循环最大值中减一。
  • 添加了 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

关于java - 使用数组的多个 if else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29309665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com