如何在SIZE和a上修复这些错误 [英] How do I fix these errors on the SIZE and a

查看:53
本文介绍了如何在SIZE和a上修复这些错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

final int SIZE = 5;

for(int i = 0; i< = SIZE; i ++);

SIZE [i] = i;

int A [] [] = {{1,2},{3,4}};

A [1,1] = 5;



我尝试了什么:



我尝试过使用另一个变量

解决方案

首先,SIZE被声明为一个整数

  final   int  SIZE =  5 ; 

但是你试图将它用作两者for循环行中的整数:

  for  int  i =  0 ; i< = SIZE; i ++); 

以分号结尾,所以 i 超出范围,以后不能使用 - 并且会尝试访问不存在的元素:当你声明一个 n 元素,索引开始为零,因此您可以访问元素[0],[1],...,[n - 1]但不能访问[n],因为它不存在。

并且还使用SIZE作为后面一行中的数组:

 SIZE [i] = i; 

它不能同时出现!

下一行没问题!

  int  A [] [] = {{ 1  2 },{ 3  4 }}; 



然后再次出错,使用错误的语法:

 A [ 1  1  ] =  5 ; 





试试这个:

  final   int  SIZE =  5 ; 
int data [] = new int < /跨度> [SIZE];
for int i = 0 ; i< SIZE; i ++){
data [i] = i;
}
int A [] [] = {{ 1 2 },{ 3 4 }};
A [ 1 ] [ 1 ] = 5 ;


  for  int  i =  0 ; i< = SIZE; i ++);  //   由于此处有分号,  
SIZE [i] = i; // 此行不在循环中


final int SIZE = 5;
for (int i = 0; i <= SIZE; i++);
SIZE [i] = i;
int A [] [] = {{1,2},{3,4}};
A [1,1] = 5;

What I have tried:

I have tried using a different variable

解决方案

Firstly, SIZE is declared as an integer

final int SIZE = 5;

But you are trying to use it as both an integer in your for loop line:

for (int i = 0; i <= SIZE; i++);

Which ends at the semicolon, so i goes out of scope and can't be used later - and would try to access elements that don't exist: when you declare an array of n elements, the indexes start are zero, so you can access elements [0], [1], ..., [n - 1] but not [n] as it doesnt exist.
And also use SIZE as an array in the line after it:

SIZE [i] = i;

It can't be both at the same time!
The next line is fine!

int A [] [] = {{1,2},{3,4}};


But then you go wrong again, by using it with the wrong syntax:

A [1,1] = 5;



Try this:

final int SIZE = 5;
int data[] = new int[SIZE];
for (int i = 0; i < SIZE; i++){
    data[i] = i;
}
int A[][] = { {1, 2}, {3, 4} };
A[1][1] = 5;


for (int i = 0; i <= SIZE; i++);  // Because of semicolon here,
SIZE [i] = i;  // this line is not in the loop


这篇关于如何在SIZE和a上修复这些错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆