cout用于输出(打印)值的详解 。现在将使用cin来获取用户输入。户输
cin是入判预定义的变量,可使用提取操作符(>>)从键盘读取数据。断语
在下面的句和示例中,用户可以输入一个数字,详解该数字存储在变量中x。户输然后我们输出的入判值x:
#include <iostream> using namespace std; int main(){ int x = 0; cout << "Type a number: "; // Type a number and press enter cin >> x; // Get user input from the keyboard cout << "Your number is: " << x; // Display the input value }
最近,户输Kelvin 开始在他的入判网站上发布他的天气预报,但是断语,有一个问题:他的句和所有预测都以华氏度来描述温度。
让我们将温度从华氏 F转换为摄氏 C
公式如下:
计算身体质量指数。身体质量指数(BMI)是常用的健康和营养专家估计在人群人体脂肪。
它的计算方法是服务器托管将个人的体重(公斤)除以身高(米)的平方(m²)
#include <iostream> int main() { double height, weight, bmi; // Ask user for their height,weight std::cout << "Type in your height (m): "; std::cin >> height; std::cout << "Type in your weight (kg): "; std::cin >> weight; // Now ask the user for their weight and calculate BMI bmi = weight / (height * height); std::cout << "Your BMI is " << bmi << "\n"; }
一个if语句用于测试真理的表达和执行基于它的一些代码。这是该if语句的一种简单形式:
#include <iostream> int main() { int x = 20; int y = 18; if (x > y) { std::cout << "x is greater than y"; } else { std::cout << "y is greater than x"; } }在化学中,pH是用于指定水溶液的酸度或碱度的标度。
写一个if,else if,else语句:
如果ph大于 7,则输出“Basic”。 如果ph小于7,则输出“ Acidic”。 如果两者都不是,则输出“中性”。 #include <iostream> int main() { double ph = 4.6; if (ph > 7) { std::cout << "Basic\n"; } else if (ph < 7) { std::cout << "Acidic\n"; } else { std::cout << "Neutral\n"; } }