这些随机0是什么,为什么我的意思不起作用? [英] What are these random 0's, and why is my mean not working?

查看:79
本文介绍了这些随机0是什么,为什么我的意思不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre>scan = new Scanner(System.in);
        try {
            System.out.print("Please enter the filename for the integer values: ");
            File file = new File(scan.nextLine());
            scan = new Scanner(file);
            System.out.println("Integer Array Contents: ");
            String del = "";
            int totalOdd = 0;
            int lastZero = -1;
            int minimum = Integer.MAX_VALUE;
            int maximum = Integer.MIN_VALUE;
            int sum = 0;
            double mean;
            int totalIndex = 0;
            int[] oddNumbers = new int[15];
            int arrayIndex = 0;
            while (scan.hasNextLine()) {
                int num = Integer.parseInt(scan.next()); //parse the number into an integer.
                System.out.print(del + num);
                del = ", ";
                if (num%2!= 0) { //it's odd
                    oddNumbers[arrayIndex] = num; //you have to use the arrayIndex variable here
                    arrayIndex++;
                    totalOdd++; //technically we could combine these vars if we wanted
                }
                if (num == 0) {
                    lastZero= totalIndex;
                }
               
                if (num < minimum) {
                    minimum = num;
                }
               
                if (num > maximum) {
                    maximum = num;
                }
                sum += num;
                totalIndex++;
            }
           
            mean = sum/totalIndex;
            System.out.println();
            System.out.println("Total odd numbers: " + totalOdd);
            System.out.print("Odd numbers are: ");
            for (int i = 0; i < totalIndex; i++) {
                System.out.print(oddNumbers[i] + " ");
            }
           
            System.out.println();
            System.out.println("Index of last zero: " + lastZero);
            System.out.println("Minimum: " + minimum);  
            System.out.println("Maximum: " + maximum);
            System.out.println("Sum: " + sum);
            System.out.println("Element mean is: " + mean);
            scan.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}







我的输出:< br $>





My output:

Integer Array Contents: 
-3, 2, 0, 0, 1, -5
Total odd numbers: 3
Odd numbers are: -3 1 -5 0 0 0 
Index of last zero: 3
Minimum: -5
Maximum: 2
Sum: -5
Element mean is: 0.0





为什么这些3 0在奇数是:之下,我怎么摆脱他们只是显示其他数字。

为什么平均值不起作用?平均值应该是:-0.833



我尝试过:



我最初在格式化,最小和最大方面遇到了很多问题,但现在这些都是我要做的事情,无法弄清楚我做错了什么。



Why are those 3 0's under "Odd numbers are: " and how do I get rid of them to just show the other numbers.
Why is the mean not working? The mean is supposed to be: -0.833

What I have tried:

I originally had many problems with formatting, min and max, but now these are what I'm down to and can't figure out what I'm doing wrong.

推荐答案

当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器看看是什么你的代码正在做。只需设置断点并查看代码执行情况,调试器允许您逐行执行第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 [ ^ ]

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

调试器中没有魔法,它没有找到错误,它只是帮助你至。当代码没有达到预期效果时,你就接近了一个bug。
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, it is an incredible learning tool.

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.


这很简单: totalIndex 计算每一行你看了, totalOdd 计算你找到的奇数的数量。

然后用错误的数字打印奇数:

It's pretty simple: totalIndex counts each line you read, totalOdd counts the number of odd numbers you find.
And then you print the odd numbers using the wrong number:
System.out.print("Odd numbers are: ");
for (int i = 0; i < totalIndex; i++) {
    System.out.print(oddNumbers[i] + " ");
}

totalIndex 更改为 totalOdd 以及您的虚假零会消失。

和你的意思?这是你的整数除法!

你的总和是-3 + 2 + 0 + 0 + 1 - 5 == -5,元素数是6.在整数除法中,-5 / 6是0.

将你的总和改为双,它应该有效。

Change totalIndex to totalOdd and your spurious zeros will vanish.
And your mean? That's integer division for you!
Your sum is -3 + 2 + 0 + 0 + 1 - 5 == -5, and the number of elements is 6. In integer division, -5 / 6 is 0.
Change your sum to a double and it should work.


这篇关于这些随机0是什么,为什么我的意思不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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