class Integer: static int parseInt(String s) Parses the string argument as a signed decimal integer.this method returns an int value,for your request of float,you can use Float.parseFloat(String s) :static float parseFloat(String s) Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.
如果我输入浮点数,怎么才能分别输出整数部分和小数部分。import java.io.*;public class H39 { public static void main(String args[]) {
float f = 0.0F; String s;
try { System.out.print("enter:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); s = br.readLine(); f = Float.parseFloat(s); }catch(IOException e){ } System.out.println("output:" + f); System.out.println("output:" + i); } }现在这样是输出浮点
建议去下载个J2SE的API 要学会查,自己查。。 建议~~:)
import java.io.*; public class ch3_e3_9 { public static void main(String args[]) { String s; double d; int i; boolean b=false; do{ try{ System.out.println("请输入一个浮点数:"); BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); s=br.readLine(); i=s.indexOf("."); d=Double.parseDouble(s); System.out.println(d+"整数部分为:"+(long)d); if(i == -1) System.out.println(d+"小数部分为:0.0"); else System.out.println(d+"小数部分为:"+Double.parseDouble(s.substring(i+1,s.length()))); b=false; } catch(IOException ioe) { b=false; } }while(b); } }写出来了,J2SE的API 下来,但是有点摸不着头脑,才开始学java,都大3才开,疯了
static int parseInt(String s)
Parses the string argument as a signed decimal integer.this method returns an int value,for your request of float,you can use Float.parseFloat(String s) :static float parseFloat(String s)
Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.
{
public static void main(String args[])
{
float f = 0.0F;
String s;
try
{
System.out.print("enter:");
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
s = br.readLine();
f = Float.parseFloat(s);
}catch(IOException e){
}
System.out.println("output:" + f);
System.out.println("output:" + i);
}
}现在这样是输出浮点
建议~~:)
public class ch3_e3_9
{
public static void main(String args[])
{
String s;
double d;
int i;
boolean b=false;
do{
try{
System.out.println("请输入一个浮点数:");
BufferedReader br=
new BufferedReader(new InputStreamReader(System.in));
s=br.readLine();
i=s.indexOf(".");
d=Double.parseDouble(s);
System.out.println(d+"整数部分为:"+(long)d);
if(i == -1)
System.out.println(d+"小数部分为:0.0");
else
System.out.println(d+"小数部分为:"+Double.parseDouble(s.substring(i+1,s.length())));
b=false;
} catch(IOException ioe) { b=false; }
}while(b);
}
}写出来了,J2SE的API 下来,但是有点摸不着头脑,才开始学java,都大3才开,疯了