简体   繁体   中英

String not printing out after conclusion of loop

So after browsing the already asked questions on here and other sites I figured I would give this a go.

I am working on an exercise that wants you to set up a program in Java that takes an input of 10 numbers and computes the average(mean) and the standard deviation and outputs them.

My problem is that when I run the program, and then enter my desired 10 values and hit enter, nothing happens. For some reason the System printouts that occur after the loop are not being executed. The println inside the loop is not necessary, but is just to show that the values are being calculated correctly as the loop runs, and they are.

I am aiming to have the current value that myValues is assigned to to be added to sum1, and the square of myValues to be added to the sqrdSum. sqrdSum is just a variable I made to sum the squares of the values entered so that the calculation of standard deviation later in the program will be cleaner.

As expected, I am not looking for this to be done for me, just some advice on how to adjust my code such that the printlns at that occur after the loop will execute. I am expecting it to be something to do with my logic, but can not figure it out. Hopefully it is something simple I have managed to miss.

Thanks.

So far I have set it up using a for loop:

    int n = 10;
    double sum1 = 0.0;
    double sqrdSum = 0.0;
    double mean1 = sum1 / n;
    double std1 = Math.pow((sqrdSum - (Math.pow(sum1, 2) / n)) / (n-1), 0.5);

    Scanner input1 = new Scanner(System.in);
    System.out.println("Enter 10 numbers: ");
    double myValues = input1.nextDouble();

    for (int count = 0; count <= n; count++) {
        sum1 += myValues;
        sqrdSum += Math.pow(myValues, 2);
        System.out.println(sum1 + " " + sqrdSum); //this is to test to see if the loop is calculating correctly.
        myValues = input1.nextDouble(); 
    }

    System.out.print("The mean of your values is: " + mean1);
    System.out.print("The standard deviation of your values is: " + std1);


    //Test values: 1 2 3 4.5 5.6 6 7 8 9 10
    //Should give a mean of 5.61
    //std of 2.99794

The problem is, that input1.nextDouble() blocks until the next number is entered. You are entering 10 numbers, but you expect 11 inputs, since you have this line

double myValues = input1.nextDouble();

which executes once and

myValues = input1.nextDouble(); 

inside the loop which executes 11 times. Just move it at the beginning of the loop:

Scanner input1 = new Scanner(System.in);
System.out.println("Enter 10 numbers: ");
double myValues = 0;

for (int count = 0; count < n; count++) {
    double myValues = input1.nextDouble(); 
    sum1 += myValues;
    sqrdSum += Math.pow(myValues, 2);
    System.out.println(sum1 + " " + sqrdSum); //this is to test to see if the loop is calculating correctly.
}

As Brian noted, you also have an off-by-one error. You start at 0 but count to 10, that makes 11 loop cycles. Change <= to <

Just change count <= n to count < n in your cycle. You're accidentaly expecting one too many values.

While the answers given did solve your original problem there is also another problem with your code. You won't be getting the correct mean because of how you initiate it and then don't set it to any values after your for loop.

int n = 10;
double sum1 = 0.0;
double sqrdSum = 0.0;
double mean1 = sum1 / n;
double std1 = Math.pow((sqrdSum - (Math.pow(sum1, 2) / n)) / (n-1), 0.5);

The lines above should read,

int n = 10;
double sum1 = 0.0;
double sqrdSum = 0.0;
double mean1 = 0.0;
double std1 = 0.0;

and then after your for loop you should calculate mean1 and std1 like the code below.

int n = 10;
double sum1 = 0.0;
double sqrdSum = 0.0;
double mean1 = 0.0;
double std1 = 0.0;

Scanner input1 = new Scanner(System.in);
System.out.println("Enter 10 numbers: ");
double myValues = 0.0;

for (int count = 0; count < n; count++) {
    myValues = input1.nextDouble(); 
    sum1 += myValues;
    sqrdSum += Math.pow(myValues, 2);
    System.out.println(sum1 + " " + sqrdSum); //this is to test to see if the loop is calculating correctly.

}

mean1 = sum1 / n;
std1 = Math.pow((sqrdSum - (Math.pow(sum1, 2) / n)) / (n-1), 0.5);

System.out.print("The mean of your values is: " + mean1);
System.out.print("The standard deviation of your values is: " + std1);


//Test values: 1 2 3 4.5 5.6 6 7 8 9 10
//Should give a mean of 5.61
//std of 2.99794

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