c++循环、continue、排序、goto以及猜数字小游戏

简介: c++循环、continue、排序、goto以及猜数字小游戏

打印*


#include<iostream>
using namespace std;
#include<string.h>
int main(){
  for(int i=1;i<=9;i++){
  for(int j=1;j<i;j++){
    cout<<j<<"*"<<i<<"="<<j*i<<" ";
  }
  cout<<endl;
  }
  system("pause");
}

continue用法


#include<iostream>
using namespace std;
#include<string.h>
int main(){
  for(int i=0;i<=100;i++){
  if(i%2==0){
    continue;
  }
  cout<<i<<endl;
  }
  system("pause");
}

排序


#include<iostream>
using namespace std;
int main(){
  int arr[9]={4,2,8,0,5,7,1,3,9};
  cout<<"排序前:"<<endl;
  for (int i=0;i<9;i++){
  cout<<arr[i]<<" ";
  }
  cout<<endl;
  for(int i=0;i<9-1;i++){
  for(int j=0;j<9-i-1;j++){
    if(arr[j]>arr[j+1]){
    int temp=arr[j];
    arr[j]=arr[j+1];
    arr[j+1]=temp;
    }
  }
  }
  for (int i=0;i<9;i++){
    cout<<arr[i]<<" ";
  }
  system("pause");
}


猜数字


#include<iostream>
using namespace std;
#include<ctime>
int main(){
srand((unsigned int)time(NULL));
int num=rand()%100+1;
int val=0;
while(1){
  cin>>val;
  if(val>num)cout<<"猜测过大"<<endl;
  else if(val<num)cout<<"猜测过小"<<endl;
  else{
  cout<<"恭喜您猜对了"<<endl;
  break; 
  } 
}
  system("pause");  
}


goto用法


#include<iostream>
using namespace std;
int main(){
  cout<<"1、xxxx"<<endl;
  cout<<"2、xxxx"<<endl;
  goto FLAG;
  cout<<"3、xxxx"<<endl;
  cout<<"4、xxxx"<<endl;
  FLAG:
  cout<<"5、xxxx"<<endl;
  system("pause");
}
相关文章
|
2月前
|
存储 安全 编译器
【C++入门 四】学习C++内联函数 | auto关键字 | 基于范围的for循环(C++11) | 指针空值nullptr(C++11)
【C++入门 四】学习C++内联函数 | auto关键字 | 基于范围的for循环(C++11) | 指针空值nullptr(C++11)
|
3月前
|
算法 程序员 编译器
C++的四类循环分享
C++的四类循环:Entry or Exit controlled, Ranged-based or For_each
|
3月前
|
C++ 容器
C++之deque容器(构造、赋值、大小、插入与删除、存取、排序)
C++之deque容器(构造、赋值、大小、插入与删除、存取、排序)
|
3月前
|
C++
C++一分钟之-循环结构:for与while循环
【6月更文挑战第18天】在C++中,`for`循环适合已知迭代次数,如数组遍历;`while`循环适用于条件驱动的未知次数循环。`for`以其初始化、条件和递增三部分结构简洁处理重复任务,而`while`则在需要先检查条件时更为灵活。常见错误包括无限循环和逻辑错误,解决办法是确保条件更新和正确判断。了解两者应用场景及陷阱,能提升代码效率和可读性。
44 6
|
3月前
|
C语言 C++ 容器
c++primer plus 6 读书笔记 第五章 循环和关系表达式
c++primer plus 6 读书笔记 第五章 循环和关系表达式
|
3月前
|
程序员 编译器 C++
探索C++语言宝库:解锁基础知识与实用技能(类型变量+条件循环+函数模块+OOP+异常处理)
探索C++语言宝库:解锁基础知识与实用技能(类型变量+条件循环+函数模块+OOP+异常处理)
32 0
|
3月前
|
算法 搜索推荐 C++
C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)
C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)
|
4月前
|
算法 C++
c++循环
c++循环
29 0
|
13天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
13天前
|
C++
C++(十六)类之间转化
在C++中,类之间的转换可以通过转换构造函数和操作符函数实现。转换构造函数是一种单参数构造函数,用于将其他类型转换为本类类型。为了防止不必要的隐式转换,可以使用`explicit`关键字来禁止这种自动转换。此外,还可以通过定义`operator`函数来进行类型转换,该函数无参数且无返回值。下面展示了如何使用这两种方式实现自定义类型的相互转换,并通过示例代码说明了`explicit`关键字的作用。