我有一个问题,以负数停止进程 [英] I have an issue with stopping the process at a negative number

查看:72
本文介绍了我有一个问题,以负数停止进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是,当我输入一个负数时,它会显示牛顿,我不希望它出现负数时。



这里是指示:

The issue I have is when I input a negative number it's show newtons and I don't want it to appear when a negative number is present.

Here is the instructions:

Mass and Weight 
Scientists measure an object’s mass in kilograms and its weight in newtons. If you know
the amount of mass that an object has, you can calculate its weight, in newtons, with
the following formula:  Weight = mass * 9.8  Write a program that asks the user to enter an object’s mass, and then calculates and
displays its weight. If the object weighs more than 1,000 newtons, display a message
indicating that it is too heavy. If the object weighs less than 10 newtons, display a mes-
sage indicating that the object is too light. The program will continue until a negative number is entered for the mass.





这是我的导师所拥有的

[ ^ ]



我尝试过:





This is how my instructor has it
[^]

What I have tried:

#include <iostream>
using namespace std;

int main()
{
double mass, weight;
do
{
   cout << "Enter the object's mass (in kilograms).  A negative mass stops the program.";
cin>>mass;

weight = (mass*9.8);
 cout << "The object weighs " << weight
        << " Newtons.\n";

if(weight>1000){

    cout << "The object is too heavy!\n";
}


else if(weight<10){

      cout << "The object is too light!\n";
}



}while(weight>=0);

   return 0;
}

推荐答案

学会正确缩进代码,显示其结构,有助于阅读和理解。

Learn to indent properly your code, it show its structure and it helps reading and understanding.
#include <iostream>
using namespace std;

int main()
{
  double mass, weight;
  do
  {
    cout << "Enter the object's mass (in kilograms).  A negative mass stops the program.";
    cin>>mass;

    weight = (mass*9.8);
    cout << "The object weighs " << weight
    << " Newtons.\n";

    if(weight>1000){
      cout << "The object is too heavy!\n";
    }
    else if(weight<10){
      cout << "The object is too light!\n";
    }
  }while(weight>=0);
  return 0;
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]


Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

引用:

我遇到的问题是,当我输入一个负数时,它显示牛顿,我不希望它出现负数时。

The issue I have is when I input a negative number it's show newtons and I don't want it to appear when a negative number is present.



简单!你在代码中的错误位置检查是否为负。



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

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

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



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



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

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

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Simple! You check for negative at wrong place in code.

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[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
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.


这篇关于我有一个问题,以负数停止进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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