日期:2014-05-20  浏览次数:20692 次

编译问题
我最近刚学Java,写了一个小程序,功能是让其以正三角的形式打印“*”,但用javac编译时,提示“错误,无法读取”,请高手指教一下,感激不尽!
代码如下:
import   java.util.Scanner;
/**
  *   用*输出一个三角形
  *  
  *   @author   wang
  *  
  */
public   class   SignOutput   {
public   static   void   main(String[]   args)   {
System.out.println( "请输入一个1到9内的整数! ");

try   {
Scanner   in   =   new   Scanner(System.in);
String   str   =   in.next();
int   line   =   Integer.parseInt(str);
if   (line   >   1   ||   line   >   9)   {
System.out.println( "请输入一个0到9内的整数! ");
}   else   {
SignOut   so   =   new   SignOut(line);
so.signOutput(line);
}
}   catch   (Exception   e)   {
System.out.println( "请输入一个0到9内的整数!! ");
e.printStackTrace();
}
}
}

public   class   SignOut   {
/**
  *   Constructor
  */
public   SignOut()   {
}

/**
  *   Constructor
  *  
  *   @param   line
  */
public   SignOut(int   line)   {
this.line   =   line;
}

/**
  *   以三角形的形式输出 "* "
  *  
  *   @param   line
  *                         int   输入的行数
  */
public   void   signOutput(int   line)   {
int   k   =   0;
for   (int   i   =   0;   i   <   line;   i++)   {
if   (line   %   2   ==   0)   {
k   =   line   /   2;
}   else   {
k   =   (2   *   (line)   +   1)   /   2;
}
for   (int   j   =   0;   j   <   k   -   1;   j++)   {
System.out.print( " ");
}
k--;
for   (int   j   =   0;   j   <   2   *   line   -   1;   j++)   {
System.out.print( "* ");
}
System.out.println( " ");
}
}

private   int   line   =   0;
}


------解决方案--------------------
帮你改好了,记得给分.
import java.util.Scanner;

/**
* 用*输出一个三角形
*
* @author wang
*
*/
public class SignOutput {
public static void signOutput(int line) {
for (int i = 0; i < line; i++) {
for (int j = 0; j < line - i; j++) {
System.out.print( " ");
}
for (int j = 0; j <(2 * i - 1); j++) {
System.out.print( "* ");
}
System.out.println( " ");
}
}
public static void main(String[] args) {
int line;
do {
do {
System.out.println( "请输入一个1到9内的整数! ");
Scanner in = new Scanner(System.in);
String str = in.next();
line = Integer.parseInt(str);
if (line <= 1 || line > 9)
break;

} while (line <= 1 || line > 9);
} while (line <= 1 || line > 9);
SignOutput.signOutput(line);