java for循环有什么问题

java for循环有什么问题,第1张

我写了一些代码,我对数组有问题:

public class array {
public static void main (String[] args) throws java.lang.Exception
{
    List<Float> sec = Arrays.asList(1f,1.2f,1.5f,2f,2.5f,3f,4f,5f,6f,8f,10f);
    List<Float> cond = Arrays.asList(6f,10f,16f,20f,25f,32f,40f,63f,100f,160f,250f);

    Scanner scan = new Scanner(System.in);
    System.out.print("Power: ");
    float pow = scan.nextFloat();
    System.out.print("Voltage: ");
    float volt = scan.nextFloat();
    float res = 0;

    float number = pow/volt;
    for (int i = 0; i < sec.size(); i  ) {
        if (number >= sec.get(i) && number < sec.size()-1){
            res = sec.get(i 1);
        }else if(number<sec.get(0) || number==0){
            res = sec.get(0);
        }else if (number>sec.size()-1){
            res = sec.size() - 1;
        }
    }
    float res1 = res*1.5f;
    float res_con = 0;
    for (int a = 0; a < cond.size(); a  ) {
        if (res1 > cond.get(a) && res1 < cond.size()-1){
            res_con = cond.get(a 1);
        }
        if(res1<cond.get(0)){
            res_con = cond.get(0);
        }
        if (res1>cond.size()-1){
            res_con = cond.size() - 1;
        }
    }
    System.out.println("Section: " res   " sq.mm");
    System.out.println("Circuit breaker: " res_con " A");
}

}

在第二次计算中,我总是10.0 A.这是不对的。 我需要有类似的东西 - 例如,如果icnputed数字等于2,结果将是更大的值(秒数组为2.5)。然后我将这个值乘以1.5。接下来,我应该将结果与第二个数组(cond)进行比较并输出一个更大的值。 我的大脑已经爆炸了。))谢谢

最佳答案:

1 个答案:

答案 0 :(得分:0)

我可能想要改变

 if (number >= sec.get(i) && number < sec.size()-1){
        res = sec.get(i 1);

 if (res1 > cond.get(a) && res1 < cond.size()-1){
        res_con = cond.get(a 1);

通过

 if (number >= sec.get(i) && i < sec.size()-1){   //replace number by i since you use i 1
        res = sec.get(i 1);

  if (res1 > cond.get(a) && a < cond.size()-1){   //for the same reasons
        res_con = cond.get(a 1);
  }
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复