What is wrong in java for loop

82 views Asked by At

I write some code and i have a problem with arrays:

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");
}

}

In second calculation i have always 10.0 A.It isn't right. I need to have something like - if icnputed number equals to 2 for example, result will be the bigger value (2.5 on sec array). Then I multiply this value to 1,5. Next, I should compare the result with the second array (cond) and output a larger value. My brain is already exploding.)) Thanks

1

There are 1 answers

0
AudioBubble On

I might want to change

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

and

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

by

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

and

  if (res1 > cond.get(a) && a < cond.size()-1){   //for the same reasons
        res_con = cond.get(a+1);
  }