
「@Author:Runsen」
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
公式如下:
#include <iostream>
int main() {
double tempf;
double tempc;
// Ask the user
std::cout << "Enter the temperature in Fahrenheit: ";
std::cin >> tempf;
tempc = (tempf - 32) / 1.8;
std::cout << "The temp is " << tempc << " degrees Celsius.\n";
}

计算身体质量指数。身体质量指数(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语句:
#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";
}
}
#include <iostream>
int main()
{
int grade = 9;
switch (grade)
{
case 9:
std::cout << "Freshman\n";
break;
case 10:
std::cout << "Sophomore\n";
break;
case 11:
std::cout << "Junior\n";
break;
case 12:
std::cout << "Senior\n";
break;
default:
std::cout << "Invalid\n";
break;
}
}