按这个方法 键盘输入 怎么样 可以分开输入
public static void main(String[] args) {
//创建对象
MaxDemo m=new MaxDemo();
System.out.println("请输入第一个数:");
Scanner s=new Scanner(System.in);
System.out.println("请输入第二个数:");
Scanner d=new Scanner(System.in);
m.Max(s.nextFloat(), d.nextFloat());
}
//创建一个比大小的方法
public void Max(float a,float b){
float max;
if(a>b){
max=a;
}else{
max=b;
}
System.out.println(a+"和"+b+"的最大值为:"+max);
}
怎么样搞成以下这样
//请输入第一个数 : 20
//请输入第二个数 : 30
26
收起
正在回答 回答被采纳积分+1
3回答
一介农夫
2017-09-08 14:34:56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Scanner; public class IsMax { public static double ismax( double one, double tow){ return one < tow ? tow : one; } public static void main(String[] args) { Scanner input= new Scanner(System.in); int one,tow; System.out.print( "请输入第一个数:" ); one = input.nextInt(); System.out.print( "请输入第二个数:" ); tow = input.nextInt(); System.out.print(ismax(one,tow)); } } |
可以参考上面或下面的来写
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.Scanner; public class IsMax { public static double ismax( double one, double tow){ System.out.print( "输入的第一个数为:" +one+ " " + "输入的第二个数为:" +tow+ " " ); return one < tow ? tow : one; } public static void main(String[] args) { Scanner input= new Scanner(System.in); System.out.print( "请输入两个数(整数|小数):" ); System.out.print( "最大数为:" +ismax(input.nextInt(),input.nextInt())); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.Scanner; public class IsMax { //public static double ismax(double one,double tow){ return one < tow ? tow : one; } public static void main(String[] args) { Scanner input= new Scanner(System.in); //System.out.print("请输入两个数(整数|小数):"); //System.out.print("最大数为:"+ismax(input.nextInt(),input.nextInt())); MAX max = new MAX(); System.out.print( "请输入第一个数:" ); max.setOne(input.nextInt()); System.out.print( "请输入第二个数:" ); max.setTow(input.nextInt()); System.out.print( "最大数为:" +max.ismax()); } static class MAX{ double one,tow; public void setOne( double one){ this .one = one; } public void setTow( double tow){ this .tow = tow; } public double ismax(){ return one < tow ? tow : one; } } } |
相似问题
登录后可查看更多问答,登录/注册
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧