注册 登录
编程论坛 新人交流区

java输出最大数

wxzyn123 发布于 2007-10-17 19:18, 1194 次点击

import java.io.*;
public class MyMath
{
//static int a,b,c;
public static void Max(int a,int b,int c)
{
if(a>b)
{
if(a>c) System.out.println("the bigest number is "+a);
else System.out.println("the bigest number is "+c);
}
else
{
if(b>c) System.out.println("the bigest number is "+b);
else System.out.println("the bigest number is "+c);

}
}
public static void main(String[] args)throws IOException
{
MyMath m=new MyMath();
int n1;
int n2;
int n3;
System.out.println("please enter the first number:");
n1=(int)System.in.read();
System.out.println("please enter the second number:");
n2=(int)System.in.read();
System.out.println("please enter the third number:");
n3=(int)System.in.read();
m.Max(n1,n2,n3);
}
}

就是这个,就是要求输入三个数,然后输出最大数

运行时只输入一个数就结束程序了,不知道问题出在哪


11 回复
#2
werth2007-10-17 19:31
回复:(wxzyn123)java输出最大数
灌水的
#3
aaron4532007-10-17 20:19
#4
zzhang08212007-10-17 20:30
铺路的
不明白 看不懂你的代码!
#5
zmzlx2007-10-18 08:38

import java.io.*;
public class MyMath
{
//static int a,b,c;
public static void Max(int a,int b,int c)
{
if(a>b)
{
if(a>c) System.out.println("the bigest number is "+a);
else System.out.println("the bigest number is "+c);
}
else
{
if(b>c) System.out.println("the bigest number is "+b);
else System.out.println("the bigest number is "+c);

}
}
public static void main(String[] args)throws IOException
{
MyMath m=new MyMath();/*请问你定义的类中有这个构造函数吗?*/
int n1;
int n2;
int n3;
System.out.println("please enter the first number:");
n1=(int)System.in.read();
System.out.println("please enter the second number:");
n2=(int)System.in.read();
System.out.println("please enter the third number:");
n3=(int)System.in.read();
m.Max(n1,n2,n3);
}
}
还有你的Max(int a,int b,int c)方法是比较烦琐的,象这类比较小规模问题,用递归思想是比较简单的.

#6
凡喻2007-10-18 09:04
进来学习学习  呵呵
#7
baby662007-10-18 09:33

import java.io.*;
public class MyMath
{
//static int a,b,c;
public static void Max(int a,int b,int c)
{
if(a>b)
{
if(a>c) System.out.println("the bigest number is "+a);
else System.out.println("the bigest number is "+c);
}
else
{
if(b>c) System.out.println("the bigest number is "+b);
else System.out.println("the bigest number is "+c);

}
}
public static void main(String[] args)throws IOException
{
MyMath m=new MyMath();/*请问你定义的类中有这个构造函数吗?*/
int n1;
int n2;
int n3;
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));

System.out.println("please enter the first number:");
//n1=(int)System.in.read();
n1=Integer.parseInt(bf.readLine());
System.out.println("please enter the second number:");
//n2=(int)System.in.read();
n2=Integer.parseInt(bf.readLine());
System.out.println("please enter the third number:");
//n3=(int)System.in.read();
n3=Integer.parseInt(bf.readLine());
m.Max(n1,n2,n3);
}
}

是因为(int)System.in.read();这句话只能作为一个输入源进行输入
所以帮你换了个输入流:BufferedReader InputStreamReader
System.in这个代表从键盘输入
然后在数字的处理上用到了包装类的思想,先把你从键盘中输入的数转化成字符串bf.readLine()然后用Integer.parseInt(bf.readLine());
转化成int数值就可以了

#8
liwanlei2007-10-18 10:18
伟大的
#9
wxzyn1232007-10-18 16:19
谢谢你们
#10
beiny2007-10-19 03:18

学习JAVA中

#11
beiny2007-10-19 03:18

搞不清楚的你代码

#12
junjinram2007-10-20 13:21

谢谢分享

1