因此,一位老师要求我们将这个简单的游戏制作成c++程序。
英语不是我的主要语言,但我会尽力解释。我是一个初学者,所以这些不会很有效,但我仍然为我迄今为止所做的感到骄傲。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int secret[3], i = 0, x, y, z, a, b, c, guess = 1, tries = 9;
bool correct = false;
srand(time(0));
do
{
secret[i] = rand() % 10;
i++;
} while (i <= 2);
x = secret[0];
y = secret[1];
z = secret[2];
//cout << x << y << z << endl; <--- for debugging purposes
cout << "=================================================nn";
cout << " I HAVE THREE SINGLE DIGIT NUMBERSnn";
cout << " YOU HAVE TEN TRIES TO GUESS ALL THREE DIGITSnn";
cout << "=================================================nn";
do
{
cout << "nnGuess the first digit.n";
while (!(cin >> a))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid input. Please try again: n";
}
cout << "nnGuess the second digit.n";
cout << a;
while (!(cin >> b))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid input. Please try again: n" << a;
}
cout << "nnGuess the third digit.n";
cout << a << b;
while (!(cin >> c))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid input. Please try again: n" << a << b;
}
cout << "nn====================================================nn";
if (tries == 0)
{
cout << "YOU RAN OUT OF GUESSES! n";
cout << "The secret number is " << x << y << z << "!n";
cout << "PLEASE RESTART THE PROGRAM TO TRY AGAIN!n";
correct = true;
}
else if ((a == x) && (b == y) && (c == z))
{
cout << "YOU GUESSED THE SECRET NUMBER IN " << guess << " TRY / TRIES!n";
cout << "CONGRATULATIONS!nn";
correct = true;
}
else if ((a == x) && (b == y) && (c != z))
{
cout << "You guessed TWO of the numbers correctly!n";
cout << "You have " << tries << " tries left.n";
correct = false;
}
else if ((a == x) && (b != y) && (c != z))
{
cout << "You guessed ONE of the numbers correctly!n";
cout << "You have " << tries << " tries left.n";
correct = false;
}
else if ((a == x) && (b != y) && (c == z))
{
cout << "You guessed TWO of the numbers correctly!n";
cout << "You have " << tries << " tries left.n";
correct = false;
}
else if ((a != x) && (b == y) && (c == z))
{
cout << "You guessed TWO of the numbers correctly!n";
cout << "You have " << tries << " tries left.n";
correct = false;
}
else if ((a != x) && (b == y) && (c != z))
{
cout << "You guessed ONE of the numbers correctly!n";
cout << "You have " << tries << " tries left.n";
correct = false;
}
else if ((a != x) && (b != y) && (c == z))
{
cout << "You guessed ONE of the numbers correctly!n";
cout << "You have " << tries << " tries left.n";
correct = false;
}
else
{
cout << "You guessed NONE of the numbers correctly!n";
cout << "You have " << tries << " tries left.n";
correct = false;
}
cout << "n====================================================nn";
guess++;
tries--;
} while (correct == false);
}
不过,在程序的这一部分,我有一个小问题,
cout << "nnGuess the first digit.n";
while (!(cin >> a))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Invalid input. Please try again: n";
}
我可以输入任何无效的内容,它会正确地将其标识为无效输入。但如果我输入2位数字(例如22(,程序仍然输入22,它只是接受它
我不知道我的程序的这一部分是如何工作的,我只是复制粘贴了它。是否可以修改它,或者我的程序只接受一个数字,0-9,并在输入两个数字时将输入标识为无效?
我知道这只是一个小小的不便,并没有真正破坏程序,但如果我能让它变得更好,那就太好了。如果可能的话,我只希望它是完美的。
我猜,如果整数有_getche这样的东西,那会更好吗?
提前谢谢。
while (!(cin >> a))
表示保持循环,而不能将输入转换为a
中的整数值-您可以添加额外条件:
while (!(cin >> a) || a < 0 || a > 9)
在后一种情况下,不需要.clear()
和.ignore(...)
,但不会造成任何伤害。
如果它对你来说是新的,||
的意思是逻辑的,或者相当于英语的逻辑,比如:"a小于0或a大于9〃。