2016-11-05 66 views
-2

主文件:基本程序,但解決方案是什麼?

public static void main(String[] args) { 
    NewClass obj = new NewClass(); 
    Scanner inp = new Scanner(System.in); 
    System.out.println("Enter marks of 5 subjects: "); 
    obj.c1 = inp.nextInt(); 
    obj.c2 = inp.nextInt(); 
    obj.c3 = inp.nextInt(); 
    obj.c4 = inp.nextInt(); 
    obj.c5 = inp.nextInt(); 
    obj.percentage(); 
} 

}

類文件:

public class NewClass { 
int tmarks; 
int omarks; 
int c1, c2, c3, c4, c5; 

NewClass() { 
    this.tmarks = 500; 
} 

omarks = c1 + c2 + c3 + c4 + c5; 

public void percentage() { 
    double p = (omarks/tmarks) * 100; 
    System.out.println("Percentage: " + p); 
} 
} 

我在類文件中得到一個錯誤 「無法找到符號omarks」。有什麼問題?

注: tmarks是指佔總分 omarks是指獲得商標

+0

你不能指'omarks'的方法之外/構造函數。 – Kayaman

+1

任何方法之外都有一段雜亂的代碼。它不屬於那裏。 – David

+1

您是否閱讀過Java手冊? – Jay

回答

0

你不能使用omarks以外的構造函數或方法

public void percentage() { 
    omarks = c1 + c2 + c3 + c4 + c5; 
    double p = (omarks/tmarks) * 100; 
    System.out.println("Percentage: " + p); 
} 
+0

@Mubashir Mazhar做我的解決方案適合你嗎? –

相關問題