作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
谁能帮我解释一下为什么calculateCoin函数不显示吗?基本上它的作用是计算硬币,该硬币是在 drawMap 函数中以 20% 的几率随机生成的。
我所做的但不确定做得是否正确的是,我在drawMap函数中调用了calculateCoin函数,然后在main函数中调用了drawMap。
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/
我是一名优秀的程序员,十分优秀!