我是一名学生,试图弄清楚如何解决一个看似简单的问题.尝试在FOR循环中初始化2个变量时,我一直收到错误.我正在尝试为游戏板创建行.为什么我收到此错误?
这是方法:
public String [] board;
public void printBoard(){
for(int i, j = 0; i < this.board.length; i++, j++)
if(j > 10)
System.out.println();
else
System.out.print(this.board[i]);
> java:39: error: variable i might not have been initialized
Run Code Online (Sandbox Code Playgroud) 我是一名学生,正在做一个Chutes and Ladders游戏.我正在使用方法来确定应该在游戏板上放置多少个滑槽和梯子.我在主要使用参数中为每个指定10,但我总是从6到11位置全面放置.
这两种方法是否会相互干扰?
或者我为随机放置设置for循环的方式有问题吗?
我是这个网站的新手,如果您需要更多说明,请告诉我,我不想将整个程序放在这里.谢谢.
//main
ChutesAndLadders cl = new ChutesAndLadders();
cl.setBoard(new String[100]);
cl.makeChutes(10);
cl.makeLadders(10);
//methods
public String [] board;
private int chutes, ladders;
public int position;
public Random rand = new Random();
//set board
public void setBoard(String [] n){
board = n;
for(int i = 0; i < board.length; i++)
board[i] = " ";
}
//set and place chutes
public void makeChutes(int n){
chutes = n;
for(int i = 0; i <= chutes; i++)
board[rand.nextInt(board.length)] = "C" + …Run Code Online (Sandbox Code Playgroud) 我是JavaScript的新手.我试图在HTML中增加一个变量,它不起作用.我的语法错了吗?
<script>
function rps() {
computerScore.value++;
computerScore.innerHTML = computerScore.value;
}
</script>
<html>
<b>computer score:</b><p id="computerScore" value="0">-</p>
<button type="button" onclick="rps()">Go</button><br>
</html>
Run Code Online (Sandbox Code Playgroud) 我是一名学生正在研究一个使用抽象数据类型"ArrayIntLog"的简单程序(TestLuck).它应该生成用户确定的日志量,并使用"compare()"方法检查在找到匹配项之前循环的日志条目数.我收到这个错误:
TestLuck.java:27:错误:变量totalRuns可能尚未初始化totalRuns + = currentRun; ^
我如何错误地初始化这些变量?它是否与我在for循环中使用它们的事实有关?
public class TestLuck{
public static void main (String [] args){
Random rand = new Random();
int n = rand.nextInt(100); // gives a random integer between 0 and 99.
Scanner kbd = new Scanner(System.in);
double average = 0;
int totalRuns, currentRun, upperLimit = 0;
System.out.println("Enter the upper limit of the random integer range: ");
ArrayIntLog arr = new ArrayIntLog(kbd.nextInt());
System.out.println("Enter the number of times to run the test: ");
int numTests = kbd.nextInt();
for(int …Run Code Online (Sandbox Code Playgroud)