gpt4 book ai didi

java - 谁能告诉我为什么我的calculateCoin 函数没有出现?

转载 作者:行者123 更新时间:2023-12-02 09:21:32 25 4
gpt4 key购买 nike

谁能帮我解释一下为什么calculateCoin函数不显示吗?基本上它的作用是计算硬币,该硬币是在 drawMap 函数中以 20% 的几率随机生成的。

我所做的但不确定做得是否正确的是,我在drawMap函数中调用了calculateCoin函数,然后在main函数中调用了drawMap。

enter image description here

public static void main(String[] args) {
Main main = new Main();

System.out.println(main.drawMap());
}

public int[][] drawMap(){
int[][] map = new int[5][5];
char coin = 'o';

for(int i =0; i<map.length; i++){
for(int j =0; j<map[i].length; j++){
map[i][j] = (int)(Math.random()*10);
if(map[i][j]<2){
System.out.print(coin+ " ");
}
else
System.out.print("*"+ " ");
}
System.out.println("");
}

calculateCoin(map, coin);
System.out.println("");

return map;
}

public int calculateCoin(int[][] map, char coin){
int result = 0;

for(int i = 0; i<map.length; i++){
for(int j = 0; j<map[i].length; j++){
if(map[i][j] == coin){
result++;
}
}
}
return result;
}

最佳答案

该函数实际上正在被调用,但从中返回的值未存储在任何变量中。如果您希望在打印 map 后发生某些事情,请将调用结果存储在变量中,然后打印它。

int calculatedCoin = calculateCoin(map, coin);
System.out.println("Calculated coin: " + calculatedCoin)

关于java - 谁能告诉我为什么我的calculateCoin 函数没有出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58662199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com