恩 面试被考到了
就四键盘输入一串数据,然后排序,
排序不是问题,但是如何 键盘接受数据呢?
就四键盘输入一串数据,然后排序,
排序不是问题,但是如何 键盘接受数据呢?
解决方案 »
- 求解和protected有关的用法
- 画图板——毕业设计
- 拜求:cookies与session的作用与区别?
- 请问Java开发的程序能否与MicroSoft的消息队列(MSMQ)之间实现相互通信
- incompatible types 问题
- 大家快来看看,这是不是rmi的问题???
- 怎样弹出一个对话框,并输入参数?
- 关于时间。如何根据一个日期来算出这个日期是星期几呢?
- 我想学一下jbuilder但是不知道在次之前应看些什么??(提建议者比送分)
- 请问这个可以实现吗,有什么方法来实现?
- 在java中用什么控件来显示数据库? 有没有类似DBGird的控件?
- 有偿转让Java即时通信工具源程序及设计文档
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line:");
System.out.print(stdin.readLine());
这是一次读一行.可以读完再排序, 或者一个一个的读, 边读边排序都行.
class Test2{
public static void main(String args[]){
int[] a = new int[10];
String s;
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<10;i++){
System.out.print("No"+i+":");
s = br.readLine();
a[i] = Integer.parseInt(s);
}
}catch(Exception e){
}
//排序啊
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
import java.util.Arrays;
class Test2{
public static void main(String args[]){
int[] a = new int[10];
String s;
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<10;i++){
System.out.print("No"+i+":");
s = br.readLine();
a[i] = Integer.parseInt(s);
}
}catch(Exception e){
}
Arrays.sort(a);
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
public class Sort
{
public static void main(String args[])
{
try
{
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
System.out.println("pleas Enter some Numbers!");
//System.out.println(bfr.readLine());
Sort.sortNum(bfr.readLine());
}
catch(Exception e)
{
System.err.println(e);
}
}
public static void sortNum(String str)
{
String str1[] = str.split(" ");
int ix[] = new int[str1.length];
for(int i=0;i<str1.length;i++)
{
ix[i]=toInt(str1[i]);
System.out.println(ix[i]);
}
}
public static int toInt(String str)
{
int i = Integer.parseInt(str);
return i;
}
public static
}//以上代码只是把读入的一行字符串转换成了整数数组,排序代码没有写
import java.io.InputStreamReader;
public class Test1 {
//
public static void main(String args[])
{
try
{ //这里就是接收键盘输入;
BufferedReader bfr = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("pleas Enter a line Numbers!");
int[] Array=Test1.sortNum(bfr.readLine());
Test1.maopaosort(Array);
//Test1.print(Array);
}
catch(Exception e)
{
System.err.println(e);
}
}
//将一行字符串转换成一个整型数组。
public static int[] sortNum(String string)
{ //这里用到了分隔符比喻string.split(","),把string字符串用逗号隔开;
String str[] = string.split(",");
int a[] = new int[str.length];
for(int i=0;i<str.length;i++)
{
a[i]=toInt(str[i]);
}
return a;
}
//将字符转换成整型。
public static int toInt(String str)
{
int i = Integer.parseInt(str);
return i;
}
//打印方法。
public static void print(int[] Array)
{
int p;
for(p = 0 ;p<Array.length; p++)
System.out.print(Array[p]+" ");
}
public static void maopaosort(int[] Array)
{
int i;
int j;
int temp= 0 ;
int count = 0;
boolean flag;
System.out.print("排序之前的数组:");
Test1.print(Array);//打印没排序之前的数组元素;
for(i=Array.length-1 ; i>=0;i--)
{
flag= false;
for(j=0;j<=i-1;j++)
{
if(Array[j]>Array[j+1])//交换条件。
{ flag = true;//交换了就把标量置为true;
temp = Array[j];
Array[j] = Array[j+1];
Array[j+1] = temp;
}
}
if(!flag) //本趟排序未发生交换,提前终止算法
continue;
count++;
}
System.out.println("\n") ;
System.out.print("排序之后的数组:");
Test1.print(Array);//打印排序好之后的数组;
System.out.println("\n") ;
System.out.println("数组中共有"+Array.length +"元素"+","+"共比较"+count+"次");
}}