我有一些作业,直到我走到最后一步,我现在难倒,我真的很感激一些帮助.
该项目的前提是在给定电话号码的情况下创建可能的单词文件.用户打算输入格式为"### - ####"的数字.然后代码拉出连字符并将电话号码发送到方法wordGenerator.我知道在此之前一切正常.当需要输出时,我遇到问题的地方就是不同的词汇.这是我的方法的样子:
// function to form words based on phone number
void wordGenerator( const int * const n )
{
// set output stream and open output file
/* Write a declaration for an ofstream object called
outFile to open the file "phone.dat" */
ofstream outFile("phone.dat");
// letters corresponding to each number
/* Write a declaration for an array of 10 const char *'s
called phoneLetters. Use an initializer list to assign
each element of the …Run Code Online (Sandbox Code Playgroud) 所以我有一个功课问题,我应该使用递归方法"找到以指定节点为根的子树中的最小元素"
然后我将此作为我的出发点:
public TreeNode
{
int data;
TreeNode left;
TreeNode right;
}
Run Code Online (Sandbox Code Playgroud)
和
/**
Finds the minimum value for the subtree that is
rooted at a given node
@param n The root of the subtree
@return The minimum value
PRECONDITION: n is not null.
*/
int min(TreeNode n)
{
// COMPLETE THE BODY OF THIS METHOD
}
Run Code Online (Sandbox Code Playgroud)
现在,我已经编写了一个非常基本的驱动程序,用于将节点插入到树中,并且我已经编写了我的递归方法,但它似乎在计算而不是向下,这是我的方法:
int min(TreeNode n){
if(n.left != null) {
n = n.left;
min(n);
System.out.println("N is now " + n.value);
}
return n.value;
}
Run Code Online (Sandbox Code Playgroud)
输出我的代码: …
我有一个Android活动,用户指定游戏中玩家的数量,以及赢得游戏所需的积分.一旦用户单击"开始",就应该使用多个Player对象创建一个新的Game对象.我已经验证过正确地从EditText Views中提取了这些数字,但由于某种原因,我在我的Game类中收到了一个空指针异常:
players[i].name = "Player " + i;
Run Code Online (Sandbox Code Playgroud)
这是我的Game类的代码,还有一个Player类,它有一个数据成员名称(我没有包含).
public class Game{
public int i;
public int numOfPlayers;
public int maxScore;
Player[] players;
public Game(int numOfPlayers, int maxScore){
this.maxScore = maxScore;
this.numOfPlayers = numOfPlayers;
players= new Player[numOfPlayers];
for(i =0; i<numOfPlayers;i++){
players[i].name = "Player " + i;
}
}
}
Run Code Online (Sandbox Code Playgroud)
感谢您抽出宝贵时间阅读本文,感谢您的帮助.
我一直收到错误"没有重载函数的实例"printArray"匹配参数列表.有人请告诉我为什么?我正在尝试重载模板函数,以便它显示在指定位置开始和结束的数组元素.
我有我的初始模板和功能:
template< typename T >
void printArray( const T *array, int count )
Run Code Online (Sandbox Code Playgroud)
而我正试图超载的功能.
template< typename T >
void printArray(int lowSubscript, int highSubscript)
Run Code Online (Sandbox Code Playgroud)
和我的电话:
// display elements 1-3 of array a
cout << "Array a from positions 1 to 3 is:\n";
elements = printArray(1,3);
Run Code Online (Sandbox Code Playgroud)
我对第一个printArray的调用显示没有错误:
// display array a using original printArray function
cout << "\nUsing original printArray function\n";
printArray( a, ACOUNT );
Run Code Online (Sandbox Code Playgroud) c++ ×2
java ×2
android ×1
binary-tree ×1
function ×1
input ×1
overloading ×1
recursion ×1
stream ×1
templates ×1