简体   繁体   中英

Java for-loop is not executed

The first for-loop you see does not execute and I'm not sure why. It is completely ignored, I tried it in a separate method and I tried it in the main method but something seems to be ignoring but I'm not sure how to get it to run, it simply goes to the next method run in the main method.

package math;
import java.util.Scanner;


public class mathAverageValue {

    static int numOfVals;
    static double total;
    static double average;
    static double[] arr = new double[numOfVals];
    static String boole;

    public static void input() {
        Scanner s = new Scanner(System.in);
        System.out.println("How many values will be averaged ? : ");
        numOfVals = s.nextInt();
        for(int i=0; i<arr.length; i++){
                System.out.print("Enter Element No."+(i+1)+": ");
                arr[i] = s.nextDouble();
        }
    }


    public static void process() {
        for (int i=0; i < arr.length; i++) {
            total = total + arr[i];
        }

        average = total / arr.length;   
    }

    public static void output() {

        System.out.println("Your average is : " + average);

        System.out.println("Would you like to average again? Y or N : ");
        Scanner i = new Scanner(System.in);
        boole = i.next();

        if ("Y".equals(boole)) {
            input();
            output();
        }
    }

    public static void main(String[] args) {
        input();
        output();
    }

}

Assign some value to static int numOfVals . Java by default assign 0 to it. Hence your for loop will never run. Also modify your array declaration like below:-

 static double arr = new double[numOfVals];

It is ignored because it is a zero length array:

static int numOfVals;  // This implicitly equals 0.
static double total;
static double average;
static double[] arr = new double[numOfVals]; // so this has no elements.

hence

    for(int i=0; i<arr.length; i++){   //arr.length is 0
            System.out.print("Enter Element No."+(i+1)+": ");
            arr[i] = s.nextDouble();
    }

doesn't iterate

According to java primitive data types initialization, all types have a default value. In your case, static int numOfVals will be assigned with 0. This is the reason why the for loop is ignored. see https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

The problem is that you have assigned a value to numOfVals and then created the array in the wrong order.

public static void input() {
    Scanner s = new Scanner(System.in);
    System.out.println("How many values will be averaged ? : ");
    numOfVals = s.nextInt();
    arr = new double[numOfVals];       // <-- PUT THIS HERE
    for(int i=0; i<arr.length; i++){
            System.out.print("Enter Element No."+(i+1)+": ");
            arr[i] = s.nextDouble();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM