你为什么这段代码给了我总和和平均数?这是一个总和和平均的循环。 [英] Do you why this code gives me worng sum and average numbers? It a while loop for sum and average.

查看:59
本文介绍了你为什么这段代码给了我总和和平均数?这是一个总和和平均的循环。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void main(String args[]) 
       {

        Scanner scanner = new Scanner(System.in);

        double sum = 0;

        System.out.println("How many entries would you like input");
        int entry = scanner.nextInt();
        
        int counter = 1;
        while (counter <= entry;) 
        counter++;
       {
            System.out.println("Please input number " + counter);
            sum += scanner.nextDouble();
        }

        System.out.println("The total sum is: " + sum);
        double average = sum / entry;
        System.out.println("The total average is: " + average);

       }





我的尝试:



我试图从用户添加两个数字并显示总和和平均值,它运行并要求用户输入他们的数字但是它显示错误的总和和平均值?我用Do Loop尝试了它并得到了相同的结果并且不确定为什么?



What I have tried:

I have tried to add two numbers from user and show the sum and average, it runs and ask user to input their numbers but then it shows wrong sum and average? I tried it with Do Loop and got the same result and not sure why?

推荐答案

我更新了你的帖子以显示缩进等



查看部分

I've updated your post to show the indentation etc

Look at the section
while (counter <= entry;)
  counter++;
 {



尝试


Try

      while (counter <= entry) 
{
        counter++;

或类似的...我真的不能在VS中加载它并检查它,但你需要了解语句块是如何形成的以及何时形成用a终止一个语句;

基本上,你的循环没有像你想象的那样执行。用调试器逐步完成它,你会看到我的意思

or similar ... I really cannot be bothered to load it all up in VS and check it, but you need to understand how statement blocks are formed and when you are terminating a statement with a ;
Basically, your loop is not performing as you think it is. Step through it with the debugger and you will see what I mean


引用:

你为什么这样做代码给了我总和和平均数?这是一个总和和平均值的循环

Do you why this code gives me worng sum and average numbers? It a while loop for sum and average



你的循环错误,使用调试器来查看你的代码是什么,它是一个专门的学习工具。



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

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

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

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

调试器中没有魔法,它没有找到错误,它只是帮助你。如果代码没有达到预期的效果,则接近错误。


Your loop os wrong, use the debugger to see what is doing your code, it is a graet learning tool.

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.


当您尝试增加计数器以便请求下一个数字时,需要添加它在while循环中。如果你保持原样,它只会增加一次计数器并保持值为2。
When you are trying to increment counter so that the next number is asked for, it needs to be added inside the while loop. If you keep it where it is, it will increment counter only once and remain a value of 2.


这篇关于你为什么这段代码给了我总和和平均数?这是一个总和和平均的循环。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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