读书人

为什么小弟我的while(left gt; 1) 一直报

发布时间: 2012-12-17 09:31:40 作者: rapoo

为什么我的while(left > 1) 一直报错??
本帖最后由 yangkaiyun 于 2012-10-27 17:13:50 编辑
public static void main(String[] args) {
boolean[] a = new boolean[10];
for(int i = 0;i < a.length;i++) {
a[i] = true;
}
}

int left = a.length;
int counts = 0;
int index = 0;

while(left > 1) {
if(a[index] == true) {
counts++;
if(counts == 3) {
counts = 0;
a[index] = false;
left--;
}
}

index++;

if(index == a.length) {
index = 0;
}
}
……
[最优解释]
你的括号,不对称哟~少了好几对。


public class Derf {
public static void main(String[] args) {
boolean[] a = new boolean[10];
for(int i = 0;i < a.length;i++) {
a[i] = true;
}
int left = a.length;
int counts = 0;
int index = 0;
while(left > 1) {
if(a[index] == true) {
counts++;
if(counts == 3) {
counts = 0;
a[index] = false;
counts--;
}
}

index++;

if(index == a.length) {
index = 0;
}
}
}
}

[其他解释]
public class Derf
{
public static void main(String[] args)
{
boolean[] a = new boolean[10];
for (int i = 0; i < a.length; i++)
{
a[i] = true;
}

int left = a.length;
int counts = 0;
int index = 0;

while (left > 1)
{
if (a[index] == true)
{
counts++;
if (counts == 3)
{
counts = 0;
a[index] = false;
left--;
}
}

index++;

if (index == a.length)
{
index = 0;
}
}
}
}
这样看看
[其他解释]
重点不是括号!是while报错,我那是程序的大部分。。。
[其他解释]
这个过程没有问题,似乎你想改index=3,6,9的值,也许是你的逻辑错误了,并无语法问题。
不知道你在一个循环里反复改变a[3,6,9]的值是什么意思。或许你想这样写:
while(--left>2){
if(left%3==0)a[left]=false;
}
[其他解释]

public class TestLeft {

public static void main(String[] args) {
boolean[] a = new boolean[10];

for(int i = 0;i < a.length;i++) {


a[i] = true;
}

int left = a.length;
int counts = 0;
int index = 0;

while(left > 1) {
if(a[index] == true) {
counts++;
if(counts == 3) {
counts = 0;
a[index] = false;
left--;
}
}
index++;
if(index == a.length) {
index = 0;
}
}
}
}

[其他解释]
不是那样的,我的错误提示是while。。郁闷啊、

[其他解释]
counts到3,a[index] = false变成FAlse不是死循环了么- -!
[其他解释]
谢谢大家,我终于发现了,很低级的错误,是while放在了main函数外边了。。。所以while出错了、

读书人网 >J2SE开发

热点推荐