永发信息网

在java中怎样从键盘输入数字(新手问题)

答案:5  悬赏:30  手机版
解决时间 2021-12-26 08:41
  • 提问者网友:鐵馬踏冰河
  • 2021-12-25 16:43
帮我看看下面的这段代码哪里出错了,谢谢import java.util.*; public class Order1{ public static void main(String[] args){ Scanner sc = new Scanner(System.in);//获取键盘输入int a=sc.nextInt();int b=sc.nextInt();int c=sc.nextInt(); System.out.println("由大到小的顺序是:"); if(a<b) if(b<c) System.out.println(c+" "+b+" "+a); else if(a<c) System.out.println(b+" "+c+" "+a); else System.out.println(b+" "+a+" "+c); else if(a<c) System.out.println(c+" "+a+" "+b); else if(b<c) System.out.println(a+" "+c+" "+b); else System.out.println(a+" "+b+" "+c); }}
最佳答案
  • 五星知识达人网友:污到你湿
  • 2021-12-25 17:35
ava初学者,一定对从键盘输入数据感到困难,使用下面的类Input,可以
方便的从键盘输入数据:
使用方法举例: String s=Input.readString(); 读入字符串
int i=Input.readInt(); 读入整数
下面是java输入输出基本类Input类的源代码:

最后以从键盘输入10个整数为例说明之。

import java.io.*;
class Input
{static InputStreamReader in;
static BufferedReader reader;
static
{in=new InputStreamReader(System.in);
reader=new BufferedReader(in);
}

static String readString()
{String s="";
try
{ s=reader.readLine();

}

catch(IOException e)
{System.out.println(e);
System.exit(0);
}
return s;
}

static char readChar()
{char ch='a';
try
{
String s=readString();
ch=s.charAt(0);

}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return ch;
}

static int readInt()
{String s=readString();
int i=0;
try{
i=Integer.parseInt(s);
}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return i;
}
static double readDouble()
{String s=readString();
double d=0.0;
try
{d=Double.parseDouble(s);
}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return d;
}
static float readFloat()
{
String s=readString();
float f=0.0f;
try
{
f=Float.parseFloat(s);
}
catch(Exception e)
{ System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return f;
}
}


class InoutData
{public static void main(String args[])
{ int a[]=new int[10];
for(int i=0;i<10;i++)
{ System.out.println("请输入第"+(i+1)+"个数:");
a[i]=Input.readInt();
}
for(int i=0;i<10;i++)
System.out.println("a["+i+"]="+a[i]);
}
}
全部回答
  • 1楼网友:摆渡翁
  • 2021-12-25 20:47
1. 使用Scanner类 Scanner scanner = new Scanner(System.in); 2. 使用BufferedReader类 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  • 2楼网友:洎扰庸人
  • 2021-12-25 20:38
以下的代码是你的版本,是可以执行并且成功的。 import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in);// 获取键盘输入 int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); System.out.println("由大到小的顺序是:"); if (a < b) if (b < c) System.out.println(c + " " + b + " " + a); else if (a < c) System.out.println(b + " " + c + " " + a); else System.out.println(b + " " + a + " " + c); else if (a < c) System.out.println(c + " " + a + " " + b); else if (b < c) System.out.println(a + " " + c + " " + b); else System.out.println(a + " " + b + " " + c); } } 也可以用下面的,3个数的排序,可以用3目。 import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in);// 获取键盘输入 int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); System.out.println("由大到小的顺序是:"); // 还可以这么做 System.out.println(a > b ? (b > c ? a + " " + b + " " + c : a > c ? a + " " + c + " " + b : c + " " + a + " " + b) : (c < a ? b + " " + c + " " + a : b > c ? b + " " + c + " " + a : c + " " + b + " " + a)); } }
  • 3楼网友:迟山
  • 2021-12-25 20:13
import java.util.*; public class count { public static void main(string[] args) { scanner sc = new scanner(system.in);//获取键盘输入 int a=sc.nextint(); int b=sc.nextint(); int c=sc.nextint(); system.out.println("由大到小的顺序是:"); if(a>b) { if(b>c) { system.out.println(a+" "+b+" "+c); } else { system.out.println(a+" "+c+" "+b); } } else if(b>a) { if(a>c) { system.out.println(b+" "+a+" "+c); } else { system.out.println(b+" "+c+" "+a); } } else if(c>a) { if(a>b) { system.out.println(c+" "+a+" "+b); } else { system.out.println(c+" "+b+" "+a); } } } }
  • 4楼网友:酒者煙囻
  • 2021-12-25 18:42
你的if else语句写得太乱了,基本上按照你的意思来写的 OK了 import java.util.*; class Order1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in);//获取键盘输入 int a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); System.out.println("由大到小的顺序是:"); if(a>b && b>c) System.out.println(a+">"+b+">"+c); if(a>c && c>b) System.out.println(a+">"+c+">"+b); if(b>a && a>c) System.out.println(b+">"+a+">"+c); if(b>c && c>a) System.out.println(b+">"+c+">"+a); if(c>a && a>b) System.out.println(c+">"+a+">"+b); if(c>b && b>a) System.out.println(c+">"+b+">"+a); } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯