如何在Java中结束此语句

时间:2019-01-16 18:08:40

标签: java

我什至无法结束这个结束声明而没有错误

import java.util.Arrays;

BufferedReader dataIn=new BufferedReader (new InputStreamReader (System.in));

    String a;
    int n=0;

    int array []= {98,9,212,21,11,49,63,70,100};

    System.out.print("My Values "+Arrays.toString(array));

    System.out.print("\nEnter Number: ");
    a=dataIn.readLine();
    array[n]=Integer.parseInt(a);



    if(array[n]==98) {
        System.out.print("The Index Number is 0");
    }

    if(array[n]==9) {
        System.out.print("The Index Number is 1");
    }
    if(array[n]==212) {
        System.out.print("The Index Number is 2");
    }

    }
     else{
        System.out.print("Error");
    }// how to end this?
// if correct the correct index number will go
// if wrong error

}

}

1 个答案:

答案 0 :(得分:1)

您在}之前有一个else,并且需要进行else/if的构建

if (array[n] == 98) {
    System.out.print("The Index Number is 0");
} else if (array[n] == 9) {
    System.out.print("The Index Number is 1");
} else if (array[n] == 212) {
    System.out.print("The Index Number is 2");
} else {
    System.out.print("Error");
}

用于:要索引的值的常规方法是:

String valueStr = dataIn.readLine();
int value = Integer.parseInt(valueStr);
boolean found = false;

for (int i = 0; i < array.length; i++) {
    if (array[i] == value) {
        System.out.println("The index number is " + i);
        found = true;
        break;
    }
}
if (!found) {
    System.out.println("Error");
}