编码新手,被while循环问题困扰 [英] New to coding, stumped by a while loop problem

查看:101
本文介绍了编码新手,被while循环问题困扰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这项任务要求我制作一个添加机器,应该不断要求一个int

并将此int添加到总数中。



我有两个问题..首先,我似乎无法将我的总数添加到'add'变量,如下所示



System.out.println (总计:+总计+加+);



其次,我不知道如何保持我的总数并不断添加到那个int ..



This assignment requires me to make an 'adding machine' that should continually ask for an int
and add this int to a total.

I have two issues.. Firstly, I can't seem to add my total to the 'add' variable as shown here

System.out.println("Total:" + total + add + "");

secondly, I don't know how to keep my total and continually add to that int..

while (count <= 100000)
{
    int add = keyboard.nextInt();
    System.out.println("Add:" + add + "");

    System.out.println("Total:"  + total  + add +  "");
    count ++;





整件事情如下......





The whole thing is below...

Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to Adding Machine! \n");
int total = 0;
System.out.println ("Total:" + total +"");

int count = 0;

while (count <= 100000)
{
    int add = keyboard.nextInt();
    System.out.println("Add:" + add + "");

    System.out.println("Total:"  + total  + add +  "");
    count ++;
    
    if (add == 0)
    {
        break;
    }    
} 

out.println("your count was "  + count + "");
out.println("your total was "  + total + "");





< b>我尝试了什么:



我已经尝试用'+'代替'*'看看会发生什么,并且工作正常..



What I have tried:

I've tried subbing out the '+' for a '*' to see what happens and that works fine..

推荐答案

您可以将新值添加到总计中以显示它,但是您不会将其添加到下一次的总计中。

试试这个:

You add your new value into the total to display it, but you don't add it to the total for next time.
Try this:
System.out.println("Add:" + add + "");
total = total + add;
System.out.println("Total:" + total + "");


更改此:

Change this:
System.out.println("Total:"  + total  + add +  "");



to:


to:

System.out.println("Total:"  + (total  + add) +  "");



您的代码也忘了添加到总计



有一个工具允许您查看代码正在执行的操作,其名称为调试程序。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

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

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第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 code also forgot to add into total.

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循环问题困扰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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