簡體   English   中英

我正在嘗試創建一個對數字求平均的程序,但無法弄清楚如何將用戶輸入的數字加在一起

[英]I am trying to create a program that averages numbers, but cannot figure out how to add the user inputted numbers together

我是一名學習Java的AP計算機科學專業的學生。 在上這門課程之前,我學習過JavaScript,但是在弄清楚如何完成此代碼時遇到了麻煩。 我的老師正在休假,我們的分校不是程序員。 感謝您的幫助。 我的老師希望我們發表評論以解釋一切。 這是我的代碼:

package com.company;

//導入掃描器類和所需的任何其他可導入類

導入java.util。*;

公共班級主要{

public static void main(String[] args) {
    //Asks a question and lets the user input an answer, saved as the int Dividend
    System.out.println("How many numbers do you want to average?");
    Scanner dividend = new Scanner(System.in);
    int Dividend = dividend.nextInt();

    //this is a variable set in order to make the while statement work for any number
    int change = 0;

    //Asks for the numbers that should be averaged
    System.out.println("Enter the numbers to find their average: ");

    /*This is saying that while change is less than the amount of numbers being averaged,
      continue to provide an input for numbers*/
    while (change <= Dividend) {
        int Num = 0;
        int nums = 0;
        Scanner num = new Scanner(System.in);
        //I am trying to add the input to num so I can average them
        nums = num.nextInt();
        Num += nums;
        /*this is making sure that change is letting the code run through the exact amount of necessary times
          in order to input the correct amount of numbers*/
        change++;

        //once all of the numbers have been put in, average them
        if (change == Dividend) {
            System.out.println("Average: " + Num / Dividend);
        }
        System.out.println(Num);
    }


}

}

我修復了您的問題,因此它現在添加了所有數字,但仍不確定我添加了nums = nums + num.nextInt();那一半nums = nums + num.nextInt(); 因此它將總計所有數字

如果您想在自己的下方找到可用的參考,我也創建了解決方案

public static void main(String[] args){
    int change = 0;//Total Numbers It think
    int nums = 0;//Sum

    System.out.println("Enter the numbers to find their average: ");
    Scanner num = new Scanner(System.in);//Should be moved out of loop so its not initialized every time

    int Dividend=1;//I had to declare to run it
    while (change <= Dividend) {//not sure why your comparing these

        nums = nums + num.nextInt();//Total Sum
        change++;//Total nums

        if (change == Dividend) {
            System.out.println("Average: " + nums / Dividend);
        }
        System.out.println(nums);
    }
}

這是我會做的

public static void main(String[] args){
    int sum = 0;
    int countOfNumbers = 0;//Tthe same as your change im guessing

    System.out.println("Enter the numbers to find their average type stop to quit: ");//Added stop statement
    Scanner scanner = new Scanner(System.in);//Should be moved out of loop so its not initialized everytime

    while (scanner.hasNext()){//Ensures there is something to check
        String next = scanner.next(); //Pull next Value
        if(next.equalsIgnoreCase("Stop"))//This will prevent looping forever
            break;
        sum = sum + Integer.valueOf(next); //Get total sum
        countOfNumbers++; //Increment total numbers
        System.out.println("The Sum is:"+sum
                +"\nThe count of Numbers averaged is:"+countOfNumbers
                +"\nThe Average is:"+sum/countOfNumbers);//Print info 
    }
    System.out.println("Done Averaging Goodbye");
}

您可以使用ArrayList來存儲所有用戶輸入,然后在其間進行迭代以平均它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM