- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我几乎完成了这个基本的骰子程序,但无法找出我对 srand 做错了什么。它应该为用户随机掷 2 个骰子,他们可以选择保留他们掷出的 (K) 或再次掷出 (R)。我希望程序每次在询问用户是否要再次掷骰子时输入“R”时为掷骰子输出新的“随机”数字,但相同的数字会继续显示。
程序的其余部分似乎运行良好——用户与计算机对战。当用户在要求保留或重新滚动时输入“R”时,如何为 roll1 和 roll2 生成新数字?
#include <stdlib.h> /// need this for srand() -- for random numbers
#include <time.h> /// need this for time() -- time
#include <iostream> /// need this for cout<< and cin>>
using namespace std; /// need this for cout<< and cin>>
int main()
{
int iseed = (int)time(0);
srand(iseed);
cout << "Beat the computer! \n";
int roll1 = 1+rand()%6; /// make a random number for die # 1 for user
int roll2 = 1+rand()%6; /// make a random number for die # 2 for user
int UserRoll = roll1 + roll2; /// totals the sum of die 1 and die 2 for the user
char keep;
do
{
cout << "You rolled a " << roll1 << " and a " << roll2 << " for a total of: " << UserRoll << "\n";
cout << "\n";
do
{
cout << "Would you like to keep this total, or roll again? \n";
cout << "\n";
cout << "Enter \"K\" for keep and \"R\" for roll again: \n";
cin >> keep;
if (keep != 'K' && keep != 'R')
{
cout << "That is not a valid choice. Please choose Y to keep your total or N to roll again. " << endl;
cout << "\n";
}
} while(keep != 'K' && keep != 'R');
if (keep == 'R')
{
cout << "You chose R--let's roll again. \n";
}
else
{
cout << "Great! Your total is " << UserRoll << "\n";
}
} while (keep == 'R');
int roll3 = 1+rand()%6; /// make a random number for die # 1 for computer
int roll4 = 1+rand()%6; /// make a random number for die # 2 for computer
int ComputerRoll = roll3 + roll4; /// totals the sum of die 1 and die 2 for the computer
cout << "The computer rolled a " << roll3 << " and a " << roll4 << " for a total of: " << ComputerRoll << "\n";
cout << "\n";
if (ComputerRoll < UserRoll)
{
cout << "Congratulations! You won! \n";
}
if (ComputerRoll > UserRoll)
{
cout << "Sorry. You lose. \n";
}
if (ComputerRoll == UserRoll)
{
cout << "It's a tie. \n";
}
return 0;
}
/*
SAMPLE RUNS:
------------
Beat the computer!
You rolled a 4 and a 6 for a total of: 10
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 4 and a 6 for a total of: 10
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
K
Great! Your total is 10
The computer rolled a 4 and a 6 for a total of: 10
It's a tie.
Process returned 0 (0x0) execution time : 8.763 s
Press any key to continue.
*/
最佳答案
您只掷一次并再次使用该掷骰的结果。如果用户再次按 R。它应该再次滚动以更新新数字。为此,只需添加
void roll(){
roll1 = 1+rand()%6; /// make a random number for die # 1 for user
roll2 = 1+rand()%6; /// make a random number for die # 2 for user
UserRoll = roll1 + roll2; /// totals the sum of die 1 and die 2 for the user
}
Remember: roll1,roll2 and UserRoll should declared globally before roll() function
就在 main() 方法之前,当用户按下 R 时调用它在
之后cout << "Beat the computer! \n";
并将其从 main 方法中移除。这就是您需要做的一切
关于C++问题在循环内/出现提示时生成新的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50548662/
我编写了一个函数来随机从 [-10,10] 中获取一对。 import System.Random main = do { s State g a randomSt = S
好的,我了解如何在 Scala 中实现随机数生成器以及如何设置生成的随机数的上限,但我对如何更改下限感到困惑。例如: var computerGuess= scala.util.Random
我写了一个函数来从 [-10,10] 中随机得到一对。 import System.Random main = do { s State g a randomSt = St
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在做一个项目,我需要在其中生成 8 个随机数。由于某种原因,我遇到随机数部分非常耗时的问题。 8 个随机数的意思是我需要一个由数字 0-9 组成的 8 个字符长的字符串。例如 01234567 或
这个问题已经有答案了: Why do I always get the same sequence of random numbers with rand()? (12 个回答) 已关闭 9 年前。
我看到这个问题可能已经在这里得到回答:Random using WELL512 但是,它对用户不太友好,也没有提供如何在“真实世界”的代码片段中使用它的示例。 这是我目前拥有的: #define m
我想知道是否有人可以为我澄清这一行。 Create a function die(x) which rolls a die x times keeping track of how many time
我正在制作一款有 6 名防守球员的足球比赛。我将这段代码设置为随机让他们都向四分卫移动。 我想知道是否有更好的方法来做到这一点。我知道必须有一种方法可以在没有这么多 if 语句的情况下循环它,但我对
在以下位置:http://www.fredosaurus.com/notes-cpp/misc/random.html 它提到如果我们想生成一个1-10范围内的随机数,我们可以这样做: r = (ra
如何在 Linux 和 C++ 中使用随机数? 我找到了一些我想使用的代码,它有一行 srand((unsigned)time(0));//seed 但是 gcc 说 board.cpp:94:24:
这个问题在这里已经有了答案: Generating random whole numbers in JavaScript in a specific range (40 个答案) 关闭 9 年前。
我有以下脚本: Timer=0; function countdown(auctionid){ var auctions; var divs; Timer=Timer+1;
利用oracle的dbms_random包结合rownum来实现,示例如下,随机取499户: select * from ( select * from busi.t_ar_
我需要获取随机数,但它不应该等于之前的数字。这是我的一段代码。但这不起作用。 function getNumber(){ var min = 0; var max = 4; var i;
我对 Haskell 还很陌生。我有一个数据类型: data Sentence= Prop Int | No Sentence | And [Sentence]
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
这个问题已经有答案了: How do I generate random integers within a specific range in Java? (73 个回答) 已关闭 7 年前。
function getRandomArbitrary(min, max) { var r = Math.floor(Math.random() * (max - min + 1) + m
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Generate random number with non-uniform density 我尝试识别/
我是一名优秀的程序员,十分优秀!