| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1641 人关注过本帖
标题:[求助]我的Boolean statement和loop 作业,希望大家帮忙。。
只看楼主 加入收藏
kickky
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2004-10-11
收藏
 问题点数:0 回复次数:5 
[求助]我的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
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
can not help you, please DIY, if you meet some error or block, you can ask us in here.
2004-10-12 22:16
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
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
随便,本无所谓。但是你题目不是写有三局吗?怎么只有两局?
2004-10-15 23:24
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
你搞那么多if和else,不如用switch美观一点,易读性高一点。
2004-10-15 23:25
快速回复:[求助]我的Boolean statement和loop 作业,希望大家帮忙。。
数据加载中...
 
   



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

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