我只是想知道。 你看,我们为一款名为Magic number game(3x3)的游戏制作了一个程序。但在我们的程序中,用户将输入所有九个数字,程序将检查输入的数字在水平,垂直和对角线上添加时是否总计为15。
我们使用简单的cin/cout
方法得到了正确的算法,输出正确,但我们需要使用类。
当我们这样做时,输入是正确的,但是当打印数字和总和时,程序会打印垃圾记忆,而不是输入用户输入的内容。以下是我们目前的代码。
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <stdlib.h>
using namespace std;
class magicNumber {
public:
void inputNum(int, int, int, int, int, int, int, int, int);
void check();
void displayResults();
void decision();
magicNumber();
private:
int a, b, c, d, e, f, g, h, i;
int row1, row2, row3, col1, col2, col3, dia1, dia2;
};
magicNumber::magicNumber() {
int a, b, c, d, e, f, g, h, i = 0;
int row1, row2, row3, col1, col2, col3, dia1, dia2 = 0;
}
void magicNumber::inputNum(int, int, int, int, int, int, int, int, int) {
cout << "Enter three numbers for the first row (seperate by space): ";
cin >> a >> b >> c;
cout << "Enter three numbers for the next row (seperate by space): ";
cin >> d >> e >> f;
cout << "Enter three numbers for the last row (seperate by space): ";
cin >> g >> h >> i;
cout << endl;
}
void magicNumber::check() {
int check = 0;
int a, b, c, d, e, f, g, h, i;
int row1, row2, row3, col1, col2, col3, dia1, dia2;
row1 = a + b + c;
if (row1 != 15)
(check++);
row2 = d + e + f;
if (row2 != 15)
(check++);
row3 = g + h + i;
if (row3 != 15)
(check++);
col1 = a + d + g;
if (col1 != 15)
(check++);
col2 = b + e + h;
if (col2 != 15)
(check++);
col3 = c + f + i;
if (col3 != 15)
(check++);
dia1 = c + e + g;
if (dia1 != 15)
(check++);
dia2 = a + e + i;
if (dia2 != 15)
(check++);
}
void magicNumber::displayResults() {
int a, b, c, d, e, f, g, h, i;
int row1, row2, row3, col1, col2, col3, dia1, dia2;
cout << " = " << dia1 << "\n";
cout << "+++++++++++++++++++++++++\n";
cout << "+ + + +\n";
cout << "+ " << a << " + " << b << " + " << c << " + = " << row1
<< "\n";
cout << "+ + + +\n";
cout << "+++++++++++++++++++++++++\n";
cout << "+ + + +\n";
cout << "+ " << d << " + " << e << " + " << f << " + = " << row2
<< "\n";
cout << "+ + + +\n";
cout << "+++++++++++++++++++++++++\n";
cout << "+ + + +\n";
cout << "+ " << g << " + " << h << " + " << i << " + = " << row3
<< "\n";
cout << "+ + + +\n";
cout << "+++++++++++++++++++++++++\n";
cout << " = " << col1 << " = " << col2 << " = " << col3 << " = "
<< dia2 << "\n";
cout << endl;
cout << endl;
}
void magicNumber::decision() {
int check = 0;
char ans;
if (check != 0) {
cout << "YOU FAILED. TRY AGAIN? [Y/N]: ";
cin >> ans;
switch (ans) {
case 'Y':
case 'y':
int main();
case 'N':
case 'n':
cout << "\nTHANK YOU, PLAY AGAIN!!";
break;
}
}
else if (check == 0)
cout << "YOU WON!! CONGRATULATIONS!\n\n";
cout << endl;
}
int main() {
system("CLS");
int check;
char ans;
magicNumber numbers;
cout << "Welcome to Magic Square Number game!\n"
<< "Please fill out the 3 x 3 grid with numbers \n"
<< "1 to 9 without repeating any digits. The sum\n"
<< " should be 15 when the numbers are added horizontally, \n"
<< "vertically, or diagonally.\n\n";
numbers.inputNum(0, 0, 0, 0, 0, 0, 0, 0, 0);
numbers.check();
numbers.displayResults();
numbers.decision();
system("PAUSE");
}
答案 0 :(得分:1)
您将在每个例程中本地重新声明整数变量。这不仅隐藏了成员变量,而且将它们初始化为它们之前的堆栈中的任何变量。正确的解决方法是删除它们,然后使用成员变量。
在实践中,在类成员变量上放置某种前缀/后缀以将它们与局部变量区分开是有帮助的。一些例子:
class Foo
{
int fVariable;
int variable_;
int variable_m;
int m_variable;
};
通过这些装饰,可以更容易地辨别变量的范围和所有权:
void Foo::ClassRoutine()
{
int variable;
variable = 0; // local variable
variable_m = 0; // class member variable
}
答案 1 :(得分:0)
您声明了这些成员变量:
class magicNumber
{
//...
int a,b,c,d,e,f,g,h,i;
int row1, row2, row3, col1, col2, col3, dia1, dia2;
然后,在check()
:
void magicNumber::check()
{
int check = 0;
int a,b,c,d,e,f,g,h,i;
和displayResults()
:
void magicNumber::displayResults()
{
int a,b,c,d,e,f,g,h,i;
您声明具有相同名称的局部变量。任何方法都不会查看或触及共享的a
,b
等。
删除局部声明,以便改为使用成员变量。
void magicNumber::displayResults()
{
cout<<" = "<<dia1<<"\n";
// etc.
答案 2 :(得分:0)
对于直接问题,只需删除与成员变量相同名称的局部变量。
既然你被告知要上课,我想我最好对此发表评论。
您已使用类机制实现过程逻辑:执行此操作,然后执行此操作,依此类推。
但是想想,如果控制台i / o被GUI取代,那么这个类有多大用处呢?就此而言,它现在有用吗?答案,它不是很有用,它只是一个复杂的问题。
相反,将类的对象视为自动化,具有一些内部状态(私有数据成员)和一些可见按钮(公共成员函数)。按下任何按钮都会使自动化做一些事情,可能会改变其内部状态。在这种特殊情况下,有用的内部状态是9个值,有用的“按钮”可以是通过bool
函数结果报告这些数字是否构成幻方的成员函数。
其他功能可能包括设置数字。
最后,您可能需要考虑std::array
来保存数字,并循环来进行检查。
答案 3 :(得分:0)
代码中的一些问题:
magicNumber
的成员变量具有相同的名称。我假设您只想使用成员变量而不是隐藏(当您在内部作用域中声明一个与外部作用域相同的名称时,后者隐藏,前者在内部作用域中可见)。decision
方法中的case语句。当您致电main()
时,您随后会执行cout << "\nTHANK YOU, PLAY AGAIN!!";
。main
方法确实是个坏主意。提取调用其他方法所需的功能。固定代码(不知道它是否有效,check
方法对于我能看到的内容是不完整的,但是编译好并修复所描述的问题):
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <stdlib.h>
void gameplay();
using namespace std;
class magicNumber {
public:
void inputNum(int, int, int, int, int, int, int, int, int);
void check();
void displayResults();
void decision();
magicNumber();
private:
int a, b, c, d, e, f, g, h, i;
int row1, row2, row3, col1, col2, col3, dia1, dia2;
};
magicNumber::magicNumber() {
a = b = c = d = e = f = g = h = i = 0;
row1 = row2 = row3 = col1 = col2 = col3 = dia1 = dia2 = 0;
}
void magicNumber::inputNum(int, int, int, int, int, int, int, int, int) {
cout << "Enter three numbers for the first row (seperate by space): ";
cin >> a >> b >> c;
cout << "Enter three numbers for the next row (seperate by space): ";
cin >> d >> e >> f;
cout << "Enter three numbers for the last row (seperate by space): ";
cin >> g >> h >> i;
cout << endl;
}
void magicNumber::check() {
int check = 0;
row1 = a + b + c;
if (row1 != 15)
(check++);
row2 = d + e + f;
if (row2 != 15)
(check++);
row3 = g + h + i;
if (row3 != 15)
(check++);
col1 = a + d + g;
if (col1 != 15)
(check++);
col2 = b + e + h;
if (col2 != 15)
(check++);
col3 = c + f + i;
if (col3 != 15)
(check++);
dia1 = c + e + g;
if (dia1 != 15)
(check++);
dia2 = a + e + i;
if (dia2 != 15)
(check++);
}
void magicNumber::displayResults() {
cout << " = " << dia1 << "\n";
cout << "+++++++++++++++++++++++++\n";
cout << "+ + + +\n";
cout << "+ " << a << " + " << b << " + " << c << " + = " << row1
<< "\n";
cout << "+ + + +\n";
cout << "+++++++++++++++++++++++++\n";
cout << "+ + + +\n";
cout << "+ " << d << " + " << e << " + " << f << " + = " << row2
<< "\n";
cout << "+ + + +\n";
cout << "+++++++++++++++++++++++++\n";
cout << "+ + + +\n";
cout << "+ " << g << " + " << h << " + " << i << " + = " << row3
<< "\n";
cout << "+ + + +\n";
cout << "+++++++++++++++++++++++++\n";
cout << " = " << col1 << " = " << col2 << " = " << col3 << " = "
<< dia2 << "\n";
cout << endl;
cout << endl;
}
void magicNumber::decision() {
int check = 0;
char ans;
if (check != 0) {
cout << "YOU FAILED. TRY AGAIN? [Y/N]: ";
cin >> ans;
switch (ans) {
case 'Y':
case 'y':
gameplay();
break;
case 'N':
case 'n':
cout << "\nTHANK YOU, PLAY AGAIN!!";
break;
}
}
else if (check == 0)
cout << "YOU WON!! CONGRATULATIONS!\n\n";
cout << endl;
}
void gameplay() {
magicNumber numbers;
cout << "Welcome to Magic Square Number game!\n"
<< "Please fill out the 3 x 3 grid with numbers \n"
<< "1 to 9 without repeating any digits. The sum\n"
<< " should be 15 when the numbers are added horizontally, \n"
<< "vertically, or diagonally.\n\n";
numbers.inputNum(0, 0, 0, 0, 0, 0, 0, 0, 0);
numbers.check();
numbers.displayResults();
numbers.decision();
}
int main() {
system("CLS");
gameplay();
system("PAUSE");
}
其他一些建议:
decision
方法中使用循环来重复游戏。