2015-12-10 29 views
-1
public static void main(String[] args){ 
int passed = 0; 
int failed = 0; 
int N; 
int grades; 
{  
    while (passed + failed) < N { 

    if grades < 6 { 
     failed = failed + 1; 
    else 
     passed = passed + 1; 

    system.out.println passedperc = F/30*100 
    system.out.println failedperc = P/30*100 

    } 
    } 
} 
+0

Java是不是Python中,你必須把布爾表達式爲在括號*而*,* if *和* similar *語句。 – Keammoort

+0

謝謝,但我是新手,你能用簡單的話來解釋一下嗎? –

+0

你寫了「如果等級<6」,但它應該是「如果(等級<6)」,並且「(通過+失敗)「 – Keammoort

回答

0

您的語法完全不正確。

這是一個很好的開始,儘管傳承者,失敗者,F和P從未被宣佈過。另外,變量約定沒有被正確地遵守。

public class TestClass { 

    public static void main(String[] args) { 
     int passed = 0; 
     int failed = 0; 
     int n = 0; //don't leave uninitialized and uncaptialize 
     int grades = 0; //same here 

     while ((passed + failed) < n) { //parens not correct 

      if (grades < 6) {//same here and missing brackets 
       failed = failed + 1; //can be changed to failed += 1; 
      } else 
       passed = passed + 1; //also can be changed using += 

     } 
     //System print statements are wrong and variables are never declared or initialized 
     System.out.println(passedPerc = f/30 * 100);//use camelCase don't use capital variables unless they are constants 
     System.out.println(failedPerc = p/30 * 100);//same here 

    } 
} 
+0

謝謝。是非常有益的,你可以用這其中也有幫助: @邁克爾隊列 '公共靜態無效的主要(字串[] args){ INT平均; INT N; INT等級; INT總和= 0; 的。 (int i = 0; i

+0

局部變量未被賦予默認值。局部變量必須在使用前通過初始化或賦值來明確地賦予一個值,其方式可以由編譯器使用明確賦值的規則進行驗證。因爲你的變量在你的'main方法'裏面,所以他們需要給出一個初始值。換句話說,他們需要被初始化,所以,只需將'avg','N'和'grades'設置爲零。此外,只是一點點友好的建議......看看[如何問](https://stackoverflow.com/help/how-to-ask),否則你可能會降低你的問題。 –

+0

此外,您將'avg'分配爲int,然後在for循環中將其重新分配爲double。最後,你的循環必須重做。 N是0,所以我說int <0.length不會做任何事情。 –

1

您的代碼有一些語法錯誤...

我修改和註釋爲評論什麼,爲什麼

... 
{  // this is not nescesary 
     while (passed + failed < N) { // the hole condition mut be between() 
     if (grades < 6) { //same in the if condition 
      failed = failed + 1; 
     } //need to close the breakets 
     else { 
     passed = passed + 1; 
     } 
     System.out.println(passedperc = F/30*100); 
     System.out.println(failedperc = P/30*100); println is a method, so the parameters mus be enclosed in() 
} // this is not nescesary 
+2

'system'需要大寫爲'System'。 passperc,failedperc,F和P從不聲明。 –

+0

**謝謝! ** –