| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1641 人关注过本帖
标题:[求助]我的Boolean statement和loop 作业,希望大家帮忙。。
取消只看楼主 加入收藏
kickky
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2004-10-11
收藏
 问题点数:0 回复次数:2 
[求助]我的Boolean statement和loop 作业,希望大家帮忙。。
Example Welcome to rock-paper-scissors rock crushes scissors, paper covers rock, scissors cut paper The initial score is 0-0, you will play 3 games.

Player 1, enter r for rock, p for paper, or s for scissors Player 2, enter r for rock, p for paper, or s for scissors Player 1 picked rock Player 2 picked scissors rock crushes scissors, player 1 wins

Player 1, enter r for rock, p for paper, or s for scissors Player 2, enter r forr rock, p for paper, or s for scissors Player 1 picked scissors Player 2 picked paper scissors cut paper, player 1 wins

Player 1 has 3 points, player 2 has 0 point, Player 1 is the winner!

Goodbye!

_______________________________________________

Supposed to use char GetChoice Function NOTE: a "GetChoice" function has been supplied to handle the // problem of obtaining a user's choice without showing // the input on the screen. This function also handles // the error message if the user enters an invalid choice char GetChoice() // This has been supplied to handle the problem of obtaining a user's choice // without showing the input on the screen. This function also handles // the error message if the user enters an invalid choice. { char choice; system("stty -echo"); cin >> choice; system("stty echo"); if ((choice == 'r') || (choice == 's') || (choice == 'p')) return choice; else { cout << "ERROR: " << choice << " is not a valid choice, please try again" << endl; return GetChoice(); }

搜索更多相关主题的帖子: statement loop Boolean 作业 
2004-10-11 04:09
kickky
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2004-10-11
收藏
得分:0 

#include <iostream> using namespace std; char GetChoice(); char choice1; char choice2; void GameInformation(); void DisplayResults();

int main(void) {

GameInformation(); choice1 = GetChoice(); choice2 = GetChoice(); DisplayResults(); return 0;

}

void GameInformation() {

cout << "Welcome to rock-paper-scissors" << endl; cout << " rock crushes scissors," << endl; cout << " paper covers rock," << endl; cout << " scissors cut paper" << endl; cout << "The initial score is 0-0, you will play 3 games." << endl; cout << endl;

}

char GetChoice() {

char choice; system("stty -echo"); cin >> choice; if ((choice == 'r') || (choice == 's') || (choice == 'p')) { system("stty echo"); return choice; }

else { cout << "ERROR: " << choice << " is not a valid choice, please try again" << endl; system("stty echo"); return GetChoice(); }

}

void DisplayResults() {

if (choice1 == 'r') { if (choice2 == 's') cout << "rock crushes scissors, player 1 wins" << endl; else if (choice2 == 'p') cout << "paper covers rock, player 2 wins" << endl; else cout << "You tied" << endl; }

else if (choice1 == 'p') { if (choice2 == 'r') cout << "paper covers rock, player 1 wins" << endl; else if (choice2 == 's') cout << "scissors cut paper, player 2 wins" << endl; else cout << "You tied" << endl; }

else if (choice1 == 's') { if (choice2 == 'p') cout << "scissors cut paper, player 1 wins" << endl; else if (choice2 == 'r') cout << "rock crushes scissors, player 2 wins" << endl; else cout << "You tied" << endl; }

}

2004-10-13 13:00
kickky
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2004-10-11
收藏
得分:0 

还没有作loop, 不过在这里还有个问题

Expected Output

Player 1, enter r for rock, p for paper, or s for scissors Player 2, enter r for rock, p for paper, or s for scissors Player 1 picked rock Player 2 picked scissors rock crushes scissors, player 1 wins

要求 先cout Player 1, enter r for rock, p for paper, or s for scissors

然后 cin choice1,

之后 cout Player 2, enter r for rock, p for paper, or s for scissors

最后cin choice2,

这步应该写到哪里?新的function好点。。。还是写到GetChoice里面?

2004-10-13 13:06
快速回复:[求助]我的Boolean statement和loop 作业,希望大家帮忙。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034354 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved