2014-06-14 63 views
-1

這是一個algoritm類,我想獲得這個程序的輸出,當n = 8無法編譯JAVA程序。我需要的輸出,當n = 8

import java.io.*; 
public class Algorithm 
{ 
    public static void main(String args[]) 
    { 

     int i,j,n; 
     String s = in.readLine(); 
     n = Integer.parseInt(s); 
     i = 2; 
     while(i<= n) 
     { 
      for(j=1; j<= n/2; j++) 
      System.out.println(i + " "+j) 
      System.out.println(i + " " +j); 
      i = i+2; 
     } 
     for (j =0; j<= i ; j+=4) 
     System.out.println(" "+j); 
    } 
} 
+1

你在哪裏定義了'in' – CMPS

+0

我不小心寫了那條線,第一個println不是假設在那裏。仍然代碼不起作用 – cseabdul92

+0

@Amir,我發現這是我的教授給出的代碼,我試圖找到輸出,我不知道在 – cseabdul92

回答

1

嘗試這樣的:

import java.util.Scanner; 

public class Algorithm { 
    public static void main(final String args[]) { 

     int i, j, n; 
     final Scanner scanner = new Scanner(System.in); 
     try { 
      String s = scanner.nextLine(); 
      n = Integer.parseInt(s); 
      i = 2; 
      while (i <= n) { 
       for (j = 1; j <= n/2; j++) { 
        System.out.println(i + " " + j); 
       } 
       System.out.println(i + " " + j); 
       i = i + 2; 
      } 
      for (j = 0; j <= i; j += 4) { 
       System.out.println(" " + j); 
      } 
     } finally { 
      scanner.close(); 
     } 
    } 
} 
0

這應該工作:

import java.io.*; 
public class Algorithm 
{ 
    public static void main(String args[]) 
    { 

     int i,j,n; 
     BufferedReader in = null; 
     try 
     { 
      in = new BufferedReader(new InputStreamReader(System.in)); 
      String s = in.readLine(); 
      n = Integer.parseInt(s); 
      i = 2; 
      while(i<= n) 
      { 
       for(j=1; j<= n/2; j++) 
       System.out.println(i + " "+j) 
       System.out.println(i + " " +j); 
       i = i+2; 
      } 
      for (j =0; j<= i ; j+=4) 
      System.out.println(" "+j); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      if(in != null) 
      { 
       try 
       { 
        in.close(); 
       } 
       catch(IOException e) {} 
      } 
     } 
    } 
} 
+0

感謝您的幫助。代碼工作正常。 – cseabdul92