简体   繁体   English

编写一个完整的程序,从用户那里读取 3 个数字,并计算和显示仅正整数的乘积

[英]write a complete program that will read 3 NUMBers from user and calculates and displays the product only of the positive integers

This is what I've tried so far这是我迄今为止尝试过的

// Calculate the product of three integers 
#include <iostream> // allows program to perform input and output

using namespace std;

// function main begins program execution 
int main()
{
    int x; // first integer to multiply 
    int y; // second integer to multiply 
    int z; // third integer to multiply 
    int result; // the product of the three integers 
    cout << "Enter three integers: "; // prompt user for data 
    cin >> x >> y >> z; 
    result = x * y * z; 
}

what can i do to solve this question?我能做些什么来解决这个问题?

EDIT: If you do not need it to loop use a if instead of a while.编辑:如果您不需要它循环使用 if 而不是 while。

Looks like you need to study some conditional statements try:看起来你需要研究一些条件语句试试:

result = x*y*z;
while(result < 0){
   cout << "Enter three integers: "; // prompt user for data 
   cin >> x >> y >> z; 
}
cout << result << endl;

Should work fine...应该可以正常工作...

I would suggest using a condition to check the number at the input itself:我建议使用条件来检查输入本身的数字:

#include <iostream>

int main() {
    int inum1 = 0;
    std::cin >> inum1;
    if (inum1 < 0) { inum1 = 1; }
    int inum2 = 0;
    std::cin >> inum2;
    if (inum2 < 0) { inum2 = 1; }
    int inum3 = 0;
    std::cin >> inum3;
    if (inum3 < 0) { inum3 = 1; }
    std::cout << inum1*inum2*inum3 << "\n";
    return 0;
}

Notice that in my example I have used std::cout and std::cin instead of using namespace std请注意,在我的示例中,我使用了 std::cout 和 std::cin 而不是using namespace std

use of using namespace std is fine if you're a beginner or the program is small, but is discouraged for large programs:)如果您是初学者或程序很小,则使用using namespace std很好,但不鼓励大型程序使用:)

If you find typing std:: tedious, you can instead include the following statements:如果您发现键入std::很乏味,您可以改为包含以下语句:

using std::cin;
using std::cout;

Also, printf and scanf is usually preferred over cin and cout for fast I/O.此外,对于快速 I/O, printfscanf通常优于cincout

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++,计算正负数并计算数字的平均值)编写一个程序,读取未指定数量的整数 - C++ ,Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers 编写一个 C++ 程序来打印一个从 1 到用户输入的数字的数字。 只接受 1 到 100 之间的数字 - Write a C++ program to print a number from 1 to a user entered number. Only accept numbers from 1 to 100 编写一个程序,计算并输出前N个奇数斐波那契数,以逗号和空格分隔。 从标准输入中输入N - Write a program that calculates and outputs the first N odd Fibonacci numbers, separated by comma and space. N is entered from the standard input (显示金字塔)编写程序,提示用户输入1到15之间的整数并显示金字塔 - (Display pyramid) Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid C ++:如何编写程序以从文件读取整数? - C++: how can I write a program to read integers from a file? 如何仅将文件中的整数读取到也有字符串的 C++ 程序中? - How do I read only integers from a file into a c++ program that also has strings? C++ 初学者 - 如何仅对用户输入的正整数或负整数求和以及如何计算平均值 - Beginner to C++ - How to sum up only positive or only negative integers the user inputs and how to calculate the avg C ++程序只接受用户使用的整数 <curses.h> 在Linux上 - C++ program to accept only the integers from the user using <curses.h> on Linux 如何编写计算整数和的variadic模板类的通用版本 - How to write generic version of variadic template class that calculates sum of integers C++ 程序不断从用户那里读取整数,直到两个连续输入数字之和大于或等于 10 - C++ program to keep reading integers from the user until the sum of two consecutive input numbers is larger than or equal to 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM