Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

java - Calculations have me stumped

I'm working on a calculation code that needs to calculate a number between the first input to the final number(second input). When I do it creates this:

What is the first number?
45
What number do you wish to go to?
50
 Sum of numbers between 46 and 50 are: 46
Mean is: -1
Median is: 24
Standard Deviation is: NaN
Variance is: 2209
 Sum of numbers between 47 and 50 are: 93
Mean is: 0
Median is: 24
Standard Deviation is: 0.0
Variance is: 10858
 Sum of numbers between 48 and 50 are: 141
 Mean is: 1
 Median is: 24
 Standard Deviation is: 1.0
Variance is: 30458
 Sum of numbers between 49 and 50 are: 190
 Mean is: 2
Median is: 24
Standard Deviation is: 1.4142135623730951
Variance is: 65802
 Sum of numbers between 50 and 50 are: 240
Mean is: 3
Median is: 24
Standard Deviation is: 1.7320508075688772
Variance is: 121971

Which obviously is not what I want since I want them all together. My code is:

import java.util.Scanner;
public class Statistics {
    public static void main(String[] args) {
        int number;
        int total;
        int sum = 0;
        int mean;
        int median;
        double standardDeviation;
        int variance=0;
        Scanner input = new Scanner(System.in);
        System.out.println("What is the first number?");
        number = input.nextInt();
        
        System.out.println("What number do you wish to go to?");
        total = input.nextInt();

    
        
       while(total > number){
            number++;
          
          sum = sum + (number);
          
          mean= sum/total-1;
          if(total%2 == 1){
              median=(total+1)/2-1;
          }
          else{
              median=(total/2-1+total/2)/2;
          }
          variance += Math.pow((sum-mean),2);
          standardDeviation= Math.sqrt(mean);
          
           System.out.println(" Sum of numbers between " + number + " and "+ total + " are: " + sum); 
            System.out.println("Mean is: "+ mean);
            System.out.println("Median is: " + median);
            System.out.println("Standard Deviation is: " + standardDeviation);
            System.out.println("Variance is: " + variance);
        }
           
        }
        
            
            
}

I am completely stuck and have been trying for two hours on this one problem. Any advice?

question from:https://stackoverflow.com/questions/65913613/calculations-have-me-stumped

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, think about the question itself. What does 'between' truly mean in your case? If I were to choose 5 and 10 as numbers, would 5 and/or 10 be included, or not?

Second, the mathematical question. How to calculate it? There are multiple ways, and they are not equally efficient. calculating the sum of all numbers between two numbers with a loop is inefficient for a large difference.

But as I am sure, your teacher gave you a hint, which is that the sum of all numbers from 1 to n is n * (n + 1) / 2. However this function will give you the sum of all positive numbers up to n, but you only want the ones starting from another. I will leave it up to you to figure out this (easy) solution yourself, as that is more rewarding than being spoonfed the answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...