此代码应打印随机卡类型(但目前它们代表和数字),然后它打印另一种卡类型,但第二种卡类型将是不同的类型。任何人都可以看到为什么这段代码每次都不起作用吗?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct houses {
int house_number;
string house_name;
};
int main()
{
srand(time(NULL));
houses house[4];
house[0].house_number = 0;
house[1].house_number = 1;
house[2].house_number = 2;
house[3].house_number = 3;
house[0].house_name = "spades";
house[0].house_name = "hearts";
house[0].house_name = "diamonds";
house[0].house_name = "clubs";
int temp, tempo, tempor;
temp = house[(rand() % 4)].house_number;
cout << temp << "\n";
tempo = house[(rand() % temp)].house_number;
tempor = house[(rand() % (4 - temp)) + temp].house_number;
int x = (rand() % 2);
if (x == 0) {
cout << tempo;
}
else if (x == 1) {
cout << tempor;
}
}
为了帮助你理解,这部分中的代码:[(RAND()%温度)]试图产生0之间,然后打印在最后数的随机数的代码:[(RAND() %(4-temp))+ temp]尝试获取最后打印的数字和数组中最大数字之间的随机数。这意味着我现在有一个数字更小,一个数字更大但不相同。
我现在认识到,通过为x生成一个0到1之间的随机数,然后使用x来确定下一个数字是大于还是小于过去的数字,这意味着如果有3个小数字和1个大数字,一个大号码与下一个打印的号码有相同的机会,就像所有3个较小的号码一样,但是没关系,我可以再次分类。
请详细说明,更多的关于_doesn't WORK_部分。 –
1. [using namespace std is bad](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice)。 2.缩进代码。 3.也许'house ... name'应该有除0之外的其他索引号 –
你有没有可以用来运行它的代码块? –