我定义了个可以输出特定格式的函数,放在CONSOLE
package corejava;/**
   An easy interface to read numbers and strings from 
   standard input   @version 1.10 10 Mar 1997
   @author Cay Horstmann
*/public class Console
{  /**
      print a prompt on the console but don't print a newline
      
      @param prompt the prompt string to display
    */   public static void printPrompt(String prompt)
   {  System.out.print(prompt + " ");
      System.out.flush();
   }
   
   /**
      read a string from the console. The string is 
      terminated by a newline      @return the input string (without the newline)
    */
    
   public static String readLine()
   {  int ch;
      String r = "";
      boolean done = false;
      while (!done)
      {  try
         {  ch = System.in.read();
            if (ch < 0 || (char)ch == '\n')
               done = true;
            else if ((char)ch != '\r') // weird--it used to do \r\n translation
               r = r + (char) ch;
         }
         catch(java.io.IOException e)
         {  done = true;
         }
      }
      return r;
   }   /**
      read a string from the console. The string is 
      terminated by a newline      @param prompt the prompt string to display
      @return the input string (without the newline)
    */
    
   public static String readLine(String prompt)
   {  printPrompt(prompt);
      return readLine();
   }   /**
      read an integer from the console. The input is 
      terminated by a newline      @param prompt the prompt string to display
      @return the input value as an int
      @exception NumberFormatException if bad input
    */
    
   public static int readInt(String prompt)
   {  while(true)
      {  printPrompt(prompt);
         try
         {  return Integer.valueOf
               (readLine().trim()).intValue();
         } catch(NumberFormatException e)
         {  System.out.println
               ("Not an integer. Please try again!");
         }
      }
   }   /**
      read a floating point number from the console. 
      The input is terminated by a newline      @param prompt the prompt string to display
      @return the input value as a double
      @exception NumberFormatException if bad input
    */
    
   public static double readDouble(String prompt)
   {  while(true)
      {  printPrompt(prompt);
         try
         {  return Double.parseDouble(readLine().trim());
         } catch(NumberFormatException e)
         {  System.out.println
         ("Not a floating point number. Please try again!");
         }
      }
   }
}
,现在我如何在有MAIN的主JAVA文件里用CONSOLE里面的函数

解决方案 »

  1.   

    Console.xxx()
    你这个相当于一个工具类了
      

  2.   

    import corejava.Console;...
    Console.yourMethod(parameter);
    ...
      

  3.   

    我用Console.yourMethod(parameter);
    还是报错--------------------Configuration: MortgageLoop - j2sdk1.4.2_02 <Default> - <Default>--------------------
    C:\Program Files\Xinox Software\JCreatorV3\MyProjects\MortgageLoop\src\MortgageLoop.java:7: package corejava does not exist
      import corejava.Console;
                      ^
    C:\Program Files\Xinox Software\JCreatorV3\MyProjects\MortgageLoop\src\MortgageLoop.java:20: cannot resolve symbol
    symbol  : variable Console 
    location: class MortgageLoop
          principal = Console.readDouble
                      ^
    C:\Program Files\Xinox Software\JCreatorV3\MyProjects\MortgageLoop\src\MortgageLoop.java:22: cannot resolve symbol
    symbol  : variable Console 
    location: class MortgageLoop
          yearlyInterest = Console.readDouble
                           ^
    C:\Program Files\Xinox Software\JCreatorV3\MyProjects\MortgageLoop\src\MortgageLoop.java:24: cannot resolve symbol
    symbol  : variable Console 
    location: class MortgageLoop
          years = Console.readInt
                  ^
    C:\Program Files\Xinox Software\JCreatorV3\MyProjects\MortgageLoop\src\MortgageLoop.java:34: cannot resolve symbol
    symbol  : variable Format 
    location: class MortgageLoop
             Format.printf("With rate %6.3f", 100 * y);
             ^
    C:\Program Files\Xinox Software\JCreatorV3\MyProjects\MortgageLoop\src\MortgageLoop.java:35: cannot resolve symbol
    symbol  : variable Format 
    location: class MortgageLoop
             Format.printf
             ^
    6 errors
      

  4.   

    删掉package这一句,或者src下面加corejava文件夹,把你的java文件放到里面
      

  5.   

    各位大虾
    本人是 java的初学者
    System.out.flush();
    和in
    的功能分别是什么啊?
    谢谢!
      

  6.   

    src下面加corejava文件夹,把你的java文件放到里?什么意思?怎么做?