2016-04-05 30 views
0

這是我的Java代碼Java錯誤不是一個語句elecricity法案

import java.lang.*; 
import java.io.*; 
class Ele 
{ 

    public static void main(String args[]) 
    { 

     int c=220,total; 

     if(c<=50) 
     { 
      total=c*1.5; 
     } 

     else if(c<=100) 
     { 
      total=c*2; 
     } 

     else if(c<=200) 
     { 
      total=c*2.8; 
     } 

     else(c>300) 
     { 
      total=c*3; 
     } 

     System.out.println("Amt="+total); 
    } 
} 

我的兩個錯誤

Ele.java:20: error: not a statement 
else(c>300) 
    ^
Ele.java:20: error: ';' expected 
else(c>300) 
+1

有一個普通的'else'語句沒有布爾條件。用else替換else(c> 300)。 – Berger

+0

Ele.java:10:錯誤:不兼容的類型:從double到int的可能的有損轉換 total = c * 1.5; ^ Ele.java:18:錯誤:不兼容的類型:可能有損地從double轉換爲int total = c * 2.8; –

回答

0

變化INT翻一番 是INT C = 220,總;加倍c = 220,總數;

1

看起來你實際上需要你的數據類型爲double,並且你還需要在else子句中取出布爾條件。檢查下面的代碼,看看它是如何工作的:

public static void main(String args[]) { 

     // use doubles instead of ints! 
     double c = 220.0; 
     double total = 0.0; 

     if(c <= 50.0) { 
      total=c*1.5; 
     } 
     else if(c <= 100.0) { 
      total = c * 2.0; 
     } 
     else if(c <= 200.0) { 
      total = c * 2.8; 
     } 
     // no boolean needed for "else" conditions 
     else { 
      total = c * 3.0; 
     } 

     System.out.println("Amt = " + total); 
    } 
}