为什么系统不打印我的平均值? [英] Why wont the system print out my average?

查看:134
本文介绍了为什么系统不打印我的平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的编码课中,我们学习打印出数组列表中一组数字的平均值。我知道如何打印单个阵列的平均值但我打印出双阵列的平均值时遇到问题。我的问题的上下文是询问用户数组的高度和宽度,然后询问最小和最大数量。它会随机找到数字然后将它们打印出来,然而很多时候会给出高度和宽度。现在我必须计算并显示整个阵列的降雨量。整个代码工作,除非我尝试这样做。谁能帮我。我有一个问题,我知道如何打印出一组特定数字的平均值,但我似乎无法弄清楚如何在我的作业中做到这一点。我应该在for循环中使用.size()方法来计算平均值吗?谢谢



我尝试过:



来自有效的问题:

 import java.util.ArrayList; 
import java.util.Scanner;

public class ArrayListAverage {
public static void main(String [] args){
final int NUM_ELEMENTS = 8;
Scanner scnr = new Scanner(System.in);
ArrayList< Double> userNums = new ArrayList< Double>(); //用户号码
Double sumVal = 0.0;
双倍averageVal = 0.0; //计算平均值
int i = 0; //循环索引

System.out.println(输入+ NUM_ELEMENTS +数字......);
for(i = 0; i< NUM_ELEMENTS; ++ i){
System.out.print(Number+(i + 1)+:);
userNums.add(new Double(scnr.nextDouble()));
}

//确定平均值
sumVal = 0.0;
for(i = 0; i< NUM_ELEMENTS; ++ i){
sumVal = sumVal + userNums.get(i); //计算所有数字的总和
}

averageVal = sumVal / NUM_ELEMENTS; //计算平均值

System.out.println(Average:+ averageVal);

返回;
}
}





代码不起作用:

< br $> b $ b

 import java.util.Scanner; 
import java.util.Random;
public class App {

public static void main(String [] args){
Scanner scan = new Scanner(System.in);

int userHeight;
int userWidth;
int userMin;
int userMax;


System.out.println(请输入数组的高度和宽度。);
System.out.print(Height:);
userHeight = scan.nextInt();
System.out.print(Width:);
userWidth = scan.nextInt();

System.out.println(请输入最小和最大降雨量值。);
System.out.print(最小降雨量:);
userMin = scan.nextInt();
System.out.print(最大降雨量:);
userMax = scan.nextInt();

int [] [] rainMap = new int [userHeight] [userWidth];

for(int i = 0; i< userHeight; i ++)
{
for(int j = 0; j< userWidth; j ++)
{
Random rand = new Random();
int randomNum = rand.nextInt((userMax - userMin)+ 1)+ userMin;
rainMap [i] [j] = randomNum;
}
}

for(int i = 0; i< userHeight; i ++)
{
for(int j = 0; j< ; userWidth; j ++)
{
System.out.print(rainMap [i] [j]);
}
System.out.println();
}

//找到平均值
double total = 0;
double sumValI = 0;
// double sumValJ = 0;
双倍平均值= 0.0;
for(int i = 0; i< userHeight; i ++)
{
for(int j = 0; j< userWidth; j ++)
{
sumValI = sumValI + rainMap.get((i)(j));
}
}
total = userHeight * userWidth;
average = sumValI / total;
System.out.println(平均降雨量:+平均值);
}

}

解决方案

引用:

我有一个问题,我知道如何打印出一组特定数字的平均值,但我似乎无法弄清楚如何在我的作业中这样做。





有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有按预期执行时,您接近于错误。


您无法以编码方式访问数组值 - 编译器错误消息应该给您一个线索。

 sumValI = sumValI + rainMap.get((i)(j)); 



请参阅数组(Java平台SE 7) [ ^ ]。





你只需要以与打印值的循环相同的方式执行此操作:

 sumValI = sumValI + rainMap [i] [j]; 



顺便说一下,为什么不在打印每个项目时进行此求和,而不是单独循环?


In my coding class we learning to print out the average of a set of numbers from an array list. I know how to print the average of a single array but I am having trouble with printing out the average of a double array. The context of my problem is to ask the user for height and width of the array, then ask for a minimum and maximum number. Where it will randomly find numbers and print them out however many times the height and width was given. Now I have to calculate and display the rainfall for the entire array. The whole code works except when I try to do this. Can anyone help me. I have a problem showing I know how to print out the average of a specific set of numbers, but I can't seem to figure out how to do it in my homework. Should I be using the .size() method in my for loops to figure out the average? Thanks

What I have tried:

From the problem that works:

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListAverage {
   public static void main (String [] args) {
      final int NUM_ELEMENTS = 8;
      Scanner scnr = new Scanner(System.in);
      ArrayList<Double> userNums = new ArrayList<Double>(); // User numbers
      Double sumVal = 0.0;
      Double averageVal = 0.0; // Computed average
      int i = 0; // Loop index

      System.out.println("Enter " + NUM_ELEMENTS + " numbers...");
      for (i = 0; i < NUM_ELEMENTS; ++i) {
         System.out.print("Number " + (i + 1) + ": ");
         userNums.add(new Double(scnr.nextDouble()));
      }

      // Determine average value
      sumVal = 0.0;
      for (i = 0; i < NUM_ELEMENTS; ++i) {
         sumVal = sumVal + userNums.get(i); // Calculate sum of all numbers
      }

      averageVal = sumVal / NUM_ELEMENTS; // Calculate average 
      
      System.out.println("Average: " + averageVal);

      return;
   }
}



The code that doesn't work:


import java.util.Scanner;
import java.util.Random;
public class App {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int userHeight;
        int userWidth;
        int userMin;
        int userMax;
        
        
        System.out.println("Please enter in a height and width of array.");
        System.out.print("Height: ");
        userHeight = scan.nextInt();
        System.out.print("Width: ");
        userWidth = scan.nextInt();
        
        System.out.println("Please enter in the minimum and maximum rainfall value.");
        System.out.print("Minimum rainfall: ");
        userMin = scan.nextInt();
        System.out.print("Maximum rainfall: ");
        userMax = scan.nextInt();
        
        int [][] rainMap = new int [userHeight][userWidth];
        
        for (int i = 0; i < userHeight; i++)
        {
            for (int j = 0; j < userWidth; j++)
            {
                Random rand = new Random();
                int randomNum = rand.nextInt((userMax - userMin) + 1) + userMin;
                rainMap[i][j] = randomNum;
            }
        }
        
        for (int i = 0; i < userHeight; i++)
        {
            for (int j = 0; j < userWidth; j++)
            {
                System.out.print(rainMap[i][j]);
            }
            System.out.println("");
        }
       
        //finding average
        double total = 0;
        double sumValI = 0;
        //double sumValJ = 0;
        double average = 0.0;
       for (int i = 0; i < userHeight; i++)
       {
           for (int j = 0; j < userWidth; j++)
           {
               sumValI = sumValI + rainMap.get((i)(j));
           }
       }
       total = userHeight * userWidth; 
        average = sumValI / total;
        System.out.println("Average rainfall: " + average);
    }
    
}

解决方案

Quote:

I have a problem showing I know how to print out the average of a specific set of numbers, but I can't seem to figure out how to do it in my homework.



There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


You cannot access the array values in the way you have coded - the compiler error message should have given you a clue.

sumValI = sumValI + rainMap.get((i)(j));


See Array (Java Platform SE 7 )[^].


You just need to do it the same way as in the loop that prints the values:

sumValI = sumValI + rainMap[i][j];


Incidentally, why not do this summing as you print each item, rather than in a separate loop?


这篇关于为什么系统不打印我的平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆