我越来越非法开始表达

问题描述:

import java.util.Scanner; 

//trying to print pyramid shape 
class try  
{ 
     public static void main(String[] args) 
     { 
      Scanner in=new Scanner(System.in); 
      System.out.println("Enter the number of lines:"); 
      int n=in.nextInt(); 
      for(int i=n;i>0;i--) 
      { 
      for(int j=1;j<=i;j++) 
      { 
       System.out.print(" "); 
      } 
      j=n-(i-1); 
      for(int k=0;k<j;k++) 
      { 
       System.out.print(j+" "); 
      } 
      System.out.println(); 
      } 
     } 
} 

但iam无法打印解决。我越来越非法开始表达

+0

它有编译问题!将'j'移动到循环中 – VinayVeluri 2014-09-12 10:15:01

+0

编译器会告诉你有问题的行。请告诉我们。 – 2014-09-12 10:15:09

try是Java中的保留关键字。你不能用它作为类名。只要你的类重命名 - 即以MyTry

参考:Java Language Keywords

+0

谢谢先生.. – manu123 2014-09-14 06:48:38

trykey word你必须首先改变你的类名。 (使用MyClass

如何做到这一点,您必须在使用它们之前定义和初始化变量。

for(int j=1;j<=i;j++){ 
     System.out.print(" "); // j visible inside for loop only 
    } 
j=n-(i-1); // access j from outside for-loop is not possible 

你可以通过你自己来纠正这个错误。你不使用IDE进行编码吗?

+0

谢谢你..和PLZ告诉我如何使用IDE编码先生。 – manu123 2014-09-14 06:48:10

这是您的问题的解决方案。

import java.util.Scanner; 

//trying to print pyramid shape 
class Try  
{ 
     public static void main(String[] args) 
     { 
      Scanner in=new Scanner(System.in); 
      System.out.println("Enter the number of lines:"); 
      int n=in.nextInt(); 
      int i,j; 
      for(i=n;i>0;i--) 
      { 
      for(j=1;j<=i;j++) 
      { 
       System.out.print(" "); 
      } 
      j=n-(i-1); 
      for(int k=0;k<j;k++) 
      { 
       System.out.print(j+" "); 
      } 
      System.out.println(); 
      } 
     } 
}