温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++用户输入、判断语句和switch怎么使用

发布时间:2022-04-15 10:38:19 来源:亿速云 阅读:202 作者:iii 栏目:编程语言

本文小编为大家详细介绍“C++用户输入、判断语句和switch怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++用户输入、判断语句和switch怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

用户输入

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 }
  • cout发音为“see-out”。用于输出,并使用插入运算符(<<)

  • cin发音为“ see-in”。用于输入,并使用提取运算符(>>)

最近,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&sup2;)

#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";     } }

switch

#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;     } }
  • 该switch关键字发起声明,其次是(),它包含了各种情况下将比较值。在示例中,switch  语句的值或表达式为grade。在此表达一个限制是它必须评估为整型(int,char,short,long,long long,或enum)。

  • 在块内部{},有多种情况。

  • case关键字检查是否表达后它附带指定值相匹配。第一种情况后的值为9。如果 的值grade等于9,:则将运行后面的代码。

  • 该break关键字告诉计算机退出块,不再执行任何代码或检查代码块内的任何其他情况。

  • 在每个 switch  语句的末尾,都有一个default语句。如果所有情况都不是true,则default语句中的代码将运行。它本质上是else一部分。

读到这里,这篇“C++用户输入、判断语句和switch怎么使用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI