我的用户友好型随机数生成器"在C ++中
当我运行该程序时,它运行正常,但是对于程序的最后一部分,该程序旨在查找随机生成的特定数量的数字。当我输入生成的数字时,程序的唯一输出是数字" 16"。我希望输出会是这样的:
输入你想要骰子的数量6输入 你要掷骰子的数量10 3 1 6 1 4 4 6 2 2 1
你想知道一个数字是你的结果的次数吗? 卷
请输入您想要计入1的数字
数字1出现了16次
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int rand_0toN1(int dice_rolls);
int main() {
int dice_rolls;
int num_on_dice;
int x = 1;
int output;
int analysis_num;
int analysis_counter;
std::string analysis;
srand(time(NULL));
cout << "Enter how many numbers do you want to be on your dice" << endl;
cin >> num_on_dice;
while(num_on_dice < 2){
cout << "" << endl;
cout << "The amount of numbers on a dice cannot be less than 2" << endl;
cout << "Re-Enter:" << endl;
cin >> num_on_dice;
}
while(num_on_dice > 1000000){
cout << "" << endl;
cout << "The amount of numbers on a dice cannot exceed 1000000" << endl;
cout << "Re-Enter:" << endl;
cin >> num_on_dice;
}
cout << "Enter the number of dice you want to roll" << endl;
cin >> dice_rolls;
while(dice_rolls > 1000000000000){
cout << "" << endl;
cout << "For testing purposes the number of dice you can roll cannot exceed 100" << endl;
cout << "Re-Enter:" << endl;
cin >> dice_rolls;
}
while(dice_rolls < 1){
cout << "" << endl;
cout << "The number of dice you can roll cannot be less than 1" << endl;
cout << "Re-Enter:" << endl;
cin >> dice_rolls;
}
while(x <= dice_rolls){
output = rand_0toN1(num_on_dice) + 1;
cout << output << endl;
if(output == analysis_num){
analysis_counter++;
}
x++;
}
cout << "" << endl;
cout << "Do you want to now how many times a number was the outcome of your rolls <Type yes or no>" << endl;
cin >> analysis;
if(analysis == "no"){
cout << "Good-Bye, I hope I will see you again later" << endl;
}
if(analysis == "yes"){
cout << "Please enter the number you want to be counted for" << endl;
cin >> analysis_num;
while(analysis_num > num_on_dice){
cout << "The number that you want to be searched cannot exceed the number that you entered for how many <how many numbers do you want to be on your dice>" << endl;
cout << "Re-Enter:" << endl;
cin >> analysis_num;
}
cout << "The number " << analysis_num << " showed up " << analysis_counter << " Times" << endl;
}
return 0;
}
int rand_0toN1(int dice_rolls){
return rand() % dice_rolls;
}
答案 0 :(得分:0)
您的代码看起来不错,但您注意哪个号码?你到目前为止看过多少人?当你输入骰子滚动循环时,analysis_counter
和analysis_num
都没有被初始化,所以他们都有垃圾值!
您需要重新构建代码,以便从用户处获取analysis_num
并在计数循环之前将analysis_count
初始化为0.