關於java錯誤

這是原始碼

import java.io.*;

class PPP {

public static void main(String[] args)

throws IOException {

System.out.println("請輸入目前打擊率(單位:成)");

System.out.println("->");

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

String str = br.readLine();

int liter = Integer.parseInt(str);

if(liter<2) {

System.out.println("打擊不理想");

else if (liter<3){

System.out.println("打擊尚可");

else if (liter>=3) {

System.out.println("打擊穩定");

}

}

}

}

}

這是出現錯誤

C:\Documents and Settings\twbak\My Documents\PPP.java:13: 'else' without 'if'

else if (liter<3){

^

C:\Documents and Settings\twbak\My Documents\PPP.java:15: 'else' without 'if'

else if (liter>=3) {

^

2 errors

Process completed.

請問要怎麼修改阿??

1 Answer

Rating
  • Lv 5
    1 decade ago
    Favorite Answer

    你好..這括號也太多了吧..

    if(liter<2) { =>拿掉

    System.out.println("打擊不理想");

    else if (liter<3){ =>拿掉

    System.out.println("打擊尚可");

    else if (liter>=3) { =>拿掉

    System.out.println("打擊穩定");

    } =>拿掉

    } =>拿掉

    } =>拿掉

    } => main 的括號

    } => PPP 的括號

    若 if 跟 else if 的敘述只有一行..要加括號..應要如下

    if(liter<2) {

    System.out.println("打擊不理想"); }

    else if (liter<3) {

    System.out.println("打擊尚可"); }

    else if (liter>=3) {

    System.out.println("打擊穩定"); }

    2008-09-13 15:46:16 補充:

    你的程式 括號 位置標示錯誤...造成語法錯誤

    if(liter<2) {

    System.out.println("打擊不理想");

    else if (liter<3){ //這裡表示在if(liter<2)內還有個判斷式..但少 if 敘述..所以錯誤

    System.out.println("打擊尚可");

    else if (liter>=3) { //這裡表示在else if(liter<3)內還有個判斷式..但少 if 敘述..所以錯誤

    System.out.println("打擊穩定");

    }

    }

    }

    Source(s): 自己
Still have questions? Get your answers by asking now.