如何调用其他类中的方法

denny008 2005-12-17 02:34:48
我定义了个可以输出特定格式的函数,放在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里面的函数
...全文
956 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
denny008 2005-12-21
  • 打赏
  • 举报
回复
src下面加corejava文件夹,把你的java文件放到里?什么意思?怎么做?
huang2005 2005-12-17
  • 打赏
  • 举报
回复
各位大虾
本人是 java的初学者
System.out.flush();
和in
的功能分别是什么啊?
谢谢!

believefym 2005-12-17
  • 打赏
  • 举报
回复
删掉package这一句,或者src下面加corejava文件夹,把你的java文件放到里面
denny008 2005-12-17
  • 打赏
  • 举报
回复
我用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
rower203 2005-12-17
  • 打赏
  • 举报
回复
import corejava.Console;

...
Console.yourMethod(parameter);
...
yuzl32 2005-12-17
  • 打赏
  • 举报
回复
Console.readLine() .....
denny008 2005-12-17
  • 打赏
  • 举报
回复
请问怎么调用?怎么写?
believefym 2005-12-17
  • 打赏
  • 举报
回复
Console.xxx()
你这个相当于一个工具类了

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧