为什么在这个while循环中不能使用方括号?

时间:2019-03-22 11:51:58

标签: java

为清楚起见,我想在以下while循环中使用方括号。但是,出现带括号的编译错误。

我可以运行这段代码

while (in.findInLine("00-01") == null)  in.nextLine();
    in.nextLine();
    for (int j = 0; j < data[0].length; j++) 
    {
        data[0][j] =  Integer.parseInt(in.findInLine("[0-9]+|-"));
        in.nextLine();
    }

但是我在方括号中出现错误

while (in.findInLine("00-01") == null) 
{ 
    in.nextLine();
    in.nextLine();
    for (int j = 0; j < data[0].length; j++) 
    {
        data[0][j] =  Integer.parseInt(in.findInLine("[0-9]+|-"));
        in.nextLine();
    }
}

错误:NumberFormatException:null

所以,我应该怎么解释

while (in.findInLine("00-01") == null)  in.nextLine();
    in.nextLine();

3 个答案:

答案 0 :(得分:1)

不带括号,循环仅适用于第一条语句。所以你的代码应该是

while (in.findInLine("00-01") == null) 
{
    in.nextLine();
}
in.nextLine();
for (int j = 0; j < data[0].length; j++) 
{
    data[0][j] =  Integer.parseInt(in.findInLine("[0-9]+|-"));
    in.nextLine();
}

答案 1 :(得分:1)

为清楚起见添加括号确实是个好主意。这个问题是为什么的一个主要例子:您的缩进会在这里引起混乱:)

while (in.findInLine("00-01") == null)  in.nextLine();

等于(并且应该转换为):

while (in.findInLine("00-01") == null) {
  in.nextLine();
}

那么,问题来了:

  

所以,我应该怎么解释

while (in.findInLine("00-01") == null)  in.nextLine();
    in.nextLine();

答案是:

while (in.findInLine("00-01") == null) {
  in.nextLine();
}
in.nextLine();

答案 2 :(得分:0)

您的第一个代码块等效于

<div [ngSwitch]="type"

    <ng-template [ngSwitchCase]="TYPE.T_INTEGER">
        <span *ngIf="v != null" [style.white-space]="pre ? 'pre' : null">{{v}}</span>
        <span *ngIf="v == null" style="color:#ccc">Not set</span>
    </ng-template>

    <ng-template [ngSwitchCase]="TYPE.T_STRING">
        <span *ngIf="v != null" style="white-space: pre">{{v}}</span>
        <span *ngIf="v == null" style="color:#ccc">Not set</span>
    </ng-template>

    <ng-template [ngSwitchCase]="TYPE.T_BOOLEAN">
        <div [style.color]="v ? '#00aa00' : '#990000'">
            <i style="line-height:1;font-size:1.25rem" [ngClass]="v ? 'fa-check' : 'fa-cancel'"></i>
            <span style="position:relative;top:-.313rem">{{v ? 'true' : 'false'}}</span>
        </div>
    </ng-template>

    <ng-template [ngSwitchCase]="TYPE.T_STRING_ARRAY">
        <ng-template [ngIf]="v != null">
            <span class="layout vertical" style="text-align:left">
                <small *ngFor="let l of v" style="font-weight:bold">{{l}}</small>
            </span>
        </ng-template>
        <span *ngIf="v == null" style="color:#ccc">Not set</span>
    </ng-template>

    <ng-template ngSwitchDefault>
        <div class="layout vertical end">
            <small style="color:#900">unhandled type: {{type}}</small>
            <pre style="margin:0;color:inherit;max-width:700px;max-height:500px;text-align:left;overflow:auto">{{value | json}}</pre>
        </div>
    </ng-template>

</div>

在没有更多上下文的情况下,您想要执行的操作不会纠正您的代码。