下是《java 2核心技术 卷I》上的例子(当然不全满足你要求,借鉴一下):
Mortgage.java
/**
* @version 1.10 10 Mar 1997
* @author Gary Cornell
*/import corejava.*;
import java.text.*;public class Mortgage
{ public static void main(String[] args)
{ double principal;
double yearlyInterest;
int years;
principal = Console.readDouble
("Loan amount (no commas):");
yearlyInterest = Console.readDouble
("Interest rate in % (ex: use 7.5 for 7.5%):")/100;
years = Console.readInt("The number of years:");
double monthlyInterest = yearlyInterest / 12;
double payment = principal * monthlyInterest
/ (1 - (Math.pow(1/(1 + monthlyInterest),
years * 12)));
System.out.println("Your payment is ");
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println(nf.format(payment));
}
} 这是上面程序中用到的类所在的包中的内容:
要涉及到包的知识,你自己云翻书看看,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!");
}
}
}
}
Mortgage.java
/**
* @version 1.10 10 Mar 1997
* @author Gary Cornell
*/import corejava.*;
import java.text.*;public class Mortgage
{ public static void main(String[] args)
{ double principal;
double yearlyInterest;
int years;
principal = Console.readDouble
("Loan amount (no commas):");
yearlyInterest = Console.readDouble
("Interest rate in % (ex: use 7.5 for 7.5%):")/100;
years = Console.readInt("The number of years:");
double monthlyInterest = yearlyInterest / 12;
double payment = principal * monthlyInterest
/ (1 - (Math.pow(1/(1 + monthlyInterest),
years * 12)));
System.out.println("Your payment is ");
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println(nf.format(payment));
}
} 这是上面程序中用到的类所在的包中的内容:
要涉及到包的知识,你自己云翻书看看,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!");
}
}
}
}
我看不懂呀。那个问题是在java语言基础后面出的题(这章只介绍了基本的数据类型和流程控制语句等等)中间有几个例子(能看懂),有没有更简单的方法?
我的这本书是《java语言与面星对象的程序设计>。是什么清华大学计算机基础教育课程系列教材。
是很简单的,就像是一套做好的零件,然后把他们拼到一起,发挥不同的作用,基本上与生活中
间的机器的组成和做事情的思维差不多。思想很重要,建议不急着编程,好好领会对象,类,接
口等东西的概念,我自己也算才入门,很多东西也没有领会到。有了JAVA的概念和方法,编程用
JBuilder等IDE不需要记住那么多方法(JAVA是没有关键字的,一切都是对象),就可以搞定了。
当然,我个人的意见很肤浅,就算做个参考吧。
import java.lang.*;
class MaxMin
{
public static void main(String args[])
{
int max=Integer.parseInt(args[0]);//将键盘输入的第一个字符串转换成整型数
int min=Integer.parseInt(args[0]);
for(int n=1;n<4;n++)
{
if(max<Integer.parseInt(args[n]))
{
max=Integer.parseInt(args[n]);
}
if(min>Integer.parseInt(args[n]))
{
min=Integer.parseInt(args[n]);
}
}
System.out.println("Max::"+max);
System.out.println("Min::"+min);
}
}/*编译时写javac MaxMin.java
执行时写 java MaxMin 2 3 4 5 2 68 9 8 4 10
要输入10个个数
*/
java MaxMin 1 2 3 4 ;