我是c ++的新手,我写了这段代码.这是按照这个顺序设计的.1.要求姓名然后欢迎那个人2.要求他们选择的武器3.挑选一个随机数并损坏一只熊猫
我已经完成了所有这三个步骤.然后我决定也许我可以通过在我的rand()函数括号中使用变量来改变我的随机数的范围.这没有按计划运作,所以我试着回复.在此先收到任何帮助.我不知道如何通过互联网搜索这个,所以我来到这里..希望有人能发现我的问题.我正在使用netbeans IDE.
我的问题:它首先要求我的名字,然后我输入我的名字,它欢迎我.但随后它完成了代码.在尝试其余代码之前.我的想法是,我显然错过了一些我本应该改变的东西.
Welcome to panda hunter! Please enter your name: Darryl
Welcome!, Darryl!
RUN SUCCESSFUL (total time: 3s)
Run Code Online (Sandbox Code Playgroud)
但我已经多次查看它并且无法发现任何错误.另外我的想法是这条线路有问题,因为这是它无法做到的事情并且进一步发展:
cout << "Pick your weapon of choice! Then press enter to attack: ";
Run Code Online (Sandbox Code Playgroud)
.这是整个文件内容:
#include <iostream>
#include <cstdlib>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h>
using namespace std;
string getName(){
string name;
cin >> name;
return name;
}
string weaponChoice(){
string weapon;
cin >> weapon;
return weapon;
}
int rand(){
int damagePanda = rand() % 20 + 1;
return damagePanda;
}
int main() {
srand(time(0));
int pandaHealth = 100;
int userHealth = 100;
cout << ("Welcome to panda hunter! Please enter your name: ");
cout << "Welcome!, " << getName() << "!" << endl;
cout << "Pick your weapon of choice! Then press enter to attack: ";
cout << "You surprise the panda with your " << weaponChoice() << ", dealing " << rand() << " damage!";
pandaHealth = pandaHealth - rand();
cout << "Panda has " << pandaHealth << " health remaining";
char f;
cin >> f;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
use*_*353 10
int rand(){
int damagePanda = rand() % 20 + 1;
return damagePanda;
}
Run Code Online (Sandbox Code Playgroud)
递归调用.你可能在这里吹嘘.
编译器应该在这里警告你!不知道为什么没有.
改成
int myrand(){
int damagePanda = rand() % 20 + 1;
return damagePanda;
}
Run Code Online (Sandbox Code Playgroud)
也改变
cout << "You surprise the panda with your "
<< weaponChoice() << ", dealing " << rand() << " damage!";
Run Code Online (Sandbox Code Playgroud)
至
cout << "You surprise the panda with your "
<< weaponChoice() << ", dealing " << myrand() << " damage!";
Run Code Online (Sandbox Code Playgroud)
这也可能需要改变
pandaHealth = pandaHealth - rand();
Run Code Online (Sandbox Code Playgroud)
最后一次更改可能取决于您的应用程序逻辑 - 我还没有尝试理解它.
| 归档时间: |
|
| 查看次数: |
1078 次 |
| 最近记录: |