如何输出蝙蝠侠和蝙蝠洞 [英] How to output batman and batcave

查看:97
本文介绍了如何输出蝙蝠侠和蝙蝠洞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何输出



2蝙蝠

3男人

4蝙蝠

5洞穴

6蝙蝠侠

8蝙蝠

9男人

10 BatCave



**没有1和7

**我需要输出

6和10



**我只有



2蝙蝠

3男人

4蝙蝠

5洞穴

6蝙蝠

男人

8蝙蝠

9 Man

10蝙蝠

洞穴





how to output

2 Bat
3 Man
4 Bat
5 Cave
6 BatMan
8 Bat
9 Man
10 BatCave

**no 1 and 7
**i need the output of
6 and 10

**i only got

2 Bat
3 Man
4 Bat
5 Cave
6 Bat
Man
8 Bat
9 Man
10 Bat
Cave


#include <iostream>
using namespace std;
int main()
{
	
	for (int n=1; n<=10; n++)
	{
		if (n == 1 || n == 7)continue;
		{
			cout<<n <<" ";

			if (n % 2 == 0)
			{
				cout<<"Bat" <<endl;
			}
			if (n % 3 == 0)
			{
				cout<<"Man" <<endl;
			}
			if (n % 5 == 0)
			{
				cout<<"Cave" <<endl;
			}
		}
	}
	system("pause");
	return 0;
}





我的尝试:



请参阅上面的代码。



What I have tried:

See the code above.

推荐答案

看起来你想创建一个字符串来存储所有的组合,然后才能打到终点线?



(我做过C ++已经有一段时间了,所以你必须查看代码)



Seems like you want to create a string to store all the combinations, before you hit endline?

(Been a while since I did C++ so you have to check the code)

string myOutput = "";

if (n % 2 == 0)
{
    myOutput+="Bat";
}
if (n % 3 == 0)
{
    myOutput+="Man";
}
if (n % 5 == 0)
{
    myOutput+="Cave";
}

cout<<myOutput <<endl;


将您的代码更改为:

Change your code to:
for (int n=1; n<=10; n++)
{
    if (n == 1 || n == 7)
        continue;
    cout<<n <<" ";
    
    if (n % 2 == 0)
    {
    	cout<<"Bat";
    }
    if (n % 3 == 0)
    {
    	cout<<"Man";
    }
    if (n % 5 == 0)
    {
    	cout<<"Cave";
    }
    cout << endl;
}


在for循环中使用switch命令:



use a switch command inside your for loop:

switch( i )
{
  case 0:
  case 10:
    break;

  case 1:
    break;

// all unneeded cases
  default:
     break;
}



玩,玩得开心,学习编码......


Play around, have fun and learn coding...


这篇关于如何输出蝙蝠侠和蝙蝠洞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆