我需要一些帮助为我正在学习的一个java入门类编写一个程序。有一个家庭作业,要求我们生成一个分数不高于9的分数,并将它们与另一个得分相加为10的“人”与另一个人配对。这场“游戏”有20名参赛者。到目前为止,我为2个得分的配对创建了假设的行和列,并嵌套了2个for循环,并以if结束,以查看行和列是否会加到10。
程序确实编译了,但在运行时,它似乎没有给我一个解决方案。
public static void main(String[] args) {
int rows = 2;
int cols = 10;
int[][] scoreTotal = new int[rows][cols];
for (int row = 0; row < rows; row++) {
int teamBlue = (int)(Math.random()* 10);
for (int col = 0; col < cols; col++) {
int teamRed = (int)( Math.random()* 10);
if (scoreTotal[row][col] == 10) {
System.out.println(scoreTotal);
}
}
}
}如果这是一个非常令人困惑或新手的问题,我很抱歉,但就像我说的,我在java入门课程中。
发布于 2013-04-08 07:52:55
通常,当我们得到任务时,我们试图一次完成所有的任务,但你可以尝试将问题抽象为更小的“组件”。试着把它想象成烹饪时的食谱,你只需写下你需要采取的步骤,然后再写出代码。
首先,从你的main方法开始,你已经这样做了
public static void main(String[] args) {
}接下来,您有一个需求,声明
生成不高于9的分数
所以你能做的就是创建一个“分数”数组,并用每个玩家的分数填充它(我用的是player而不是person,因为你提到了"score“和"contestants")。
public static void main(String[] args) {
// generate a score for each of the contestants
},并将他们与另一个得分相加为10的“人”配对。
public static void main(String[] args) {
// generate a score for each of the contestants
// if score of player1 and another player is equal to 10, save pair
}if语句有点麻烦,所以我们可以试着分解它。为了简单起见,试着从一开始就只考虑用一个播放器进行测试。
public static void main(String[] args) {
// generate a score for each of the contestants
// take score of first player, s
// compare s + x = 10, where x is score of other player
// if x = 10 - s, save pair
}现在我们得到了一些更简单的东西。让我们试着输入一些代码。
public static void main(String[] args) {
int[] scores = new int[20];
// generate a score for each of the contestants
for (int i = 0; i < scores.length; ++i) {
scores[i] = getRandomNumber();
}
// take score of first player, s
int s = scores[0]; // the first player is at index 0
// compare s + x = 10, where x is score of other player
for (int i = 1; i < 20; ++i) {
// note: We don't have to test against index 0 (that's the first player)
// so we start at index 1
// if x = 10 - s
int x = scores[i];
if (x == 10 - s) {
// save pair
}
}
}
public static int getRandomNumber() {
// todo: generate a random score between 0 and 9 and return it
return 4;
}您似乎知道如何在原始代码中使用for循环。您没有做的是在数组中赋值。scores[i] = x; (其中x是一个整数)负责处理这一点,类似于给一个整数赋值,例如int a = 0。我使用了一种方法来生成返回int的分数,因为如何获取随机值的实现“没有意义”(所以我们将其抽象出来)。因此,for循环为数组的每个索引设置一个分数值。
接下来,由于scores数组包含参赛者的所有分数,因此我们需要将它们配对。使用一些简单的数学运算,我们构造了一个if语句,用于检查第一个玩家和第二个玩家的得分总和是否为10。
接下来,我们需要保存参赛者对。您已经正确地创建了一个二维数组,一个用于保存球员,另一个用于“球队”。填充此数组时,2D数组可能如下所示
| players |
| 0 1 |
-------+----+----+
team 0 | 0 | 1 |
team 1 | 3 | 7 |
team 2 | 8 | 9 |
-------+----+----+也就是说,我们将玩家0和1,玩家3和玩家7,玩家8和玩家9组合在一起。我们注意到团队索引(0-2)与实际球员无关,所以我们可以得出结论,我们需要一个单独的团队索引。
public static void main(String[] args) {
// generate a score for each of the contestants
// create the team array, 10 teams each with 2 participants
// for every player: take score of player, s
// compare s + x = 10, where x is score of every other contestant
// if x = 10 - s
// assign the current team to player s and player x
// increment team integer (assign next team)
}因此,使用与您的代码类似的代码,我们构造团队数组,并将第一个团队分配给我们的球员,然后再继续其他团队。
// create the team array, 10 teams, each with 2 participants
int[][] teams = new int[10][2];
// assign the first team to player 3 and player 7
teams[0][0] = 3;
teams[0][1] = 7;这只是一个示例,其中Team 0被分配了两个团队成员,一个在索引0,另一个在索引1,分别分配给player 3和player 7。
这太棒了。我们现在可以将一个玩家与其他玩家中的任何一个配对。由此,我们知道我们需要一个“当前团队”的计数器,因为在这一轮中每个参赛者都不需要有队友,当一个团队被分配时,我们应该分配下一个团队。
public static void main(String[] args) {
int[] scores = new int[20];
// generate a score for each of the contestants
// ... same as before
// create the team array, 10 teams each with 2 participants
int[][] pairs = new int[10][2];
// create a team integer
int currentTeam = 0;
// for every player: take score of player, s
for (int i = 0; i < 20; ++i) {
// todo: test if player i is already in teams array and continue; if it is
int s = scores[i];
for (int j = i + 1; j < 20; ++j) {
// compare s + x = 10, where x is score of every other contestant
int x = scores[j];
// if x = 10 - s
if (x == 10 - s) {
// assign the current team to player i and player j
pair[currentTeam][0] = i;
pair[currentTeam][1] = j;
// increment team integer (assign next team)
++currentTeam;
}
}
}
}注意,第二个for循环从i + 1开始,因为我们已经测试了索引较低的玩家。
另外,你没有提到它,但在这个问题中有一个“隐藏”的约束,即每对都是独占的,所以一个参赛者只能参与一次。您应该添加第三个for循环,用于检查teams数组是否已包含球员。
因此,通过分解问题,我们设法获得了一些可能工作或不工作的代码,但逻辑无论如何都是通过推理创建的,这在解决问题时总是很重要的!
发布于 2013-04-08 05:32:15
按照现在的情况,除了为你写出程序之外,没有人能很容易地帮助你,因为你没有提交任何代码,所以这里有一些指示,而不是简单地为你做家庭作业:首先,分解问题。仔细阅读问题,然后找出你必须做的事情。在这样做的过程中,您应该达到几个可以转换为代码的步骤。
发布于 2013-04-08 07:39:44
所以你在循环中:
for (int row = 0; row < rows; row++) {
int teamBlue = (int)(Math.random()* 10);// teamBlue never used
for (int col = 0; col < cols; col++) {
int teamRed = (int)( Math.random()* 10);// teamBlue never used
if (scoreTotal[row][col] == 10) {
// scoreTotal[row][col] never modified
// never reached because scoreTotal[row][col] == 0 by default
System.out.println(scoreTotal);
}
}
}换句话说,你的2D数组充满了0:
{ {0 0 0 0 0 0 0 0 0 0}
{0 0 0 0 0 0 0 0 0 0} }因为你没有对分数做任何处理。您可以:
public static void main(String[] args) {
int rows = 2;
int cols = 10;
int[][] scoreTotal = new int[rows][cols];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int randScore = (int)(Math.random()* 9) + 1;// in case you want a 1-9
scoreTotal[row][col] = randScore
}
}
}这样你就有了一个分数的二维数组:
{ {1 2 3 4 5 6 7 8 9 2} // row == 0, 1st player random scores
{1 2 3 4 3 5 6 7 8 9} } // row == 1, 2nd player random scores
//col index: 0 1 2 3 4 5 6 7 8 9
//eg. scoreTotal[2][4] would be 3现在你可以对分数做任何你想做的事情。配对将取决于您是否希望配对只加到10或其他一些规则。
https://stackoverflow.com/questions/15868016
复制相似问题