9 个解决方案
#1
import java.util.Arrays;
import java.util.Scanner;
public class F {
public static void main(String[] agrs) {
/*接收数据*/
System.out.println("数组的大小?");
Scanner r = new Scanner(System.in);
int n = r.nextInt();
System.out.println("输入数组:");
int[] num = new int[n];
for (int i = 0; i < n; i++) {
num[i] = r.nextInt();
}
/*排序*/
Arrays.sort(num);
/*输出*/
for (int i : num) {
System.out.println(i);
}
}
}
这是一个简单的例子~实现从键盘键入一个整型数组并排序输出~~
看看吧~~
具体参见相关API~~~~~~
#2
你要实现仅仅是键盘输入还是键盘的监听?
#3
Scanner,I/O流都可以
#4
import java.util.Scanner;
#5
#6
Scanner r = new Scanner(System.in);
int n = r.nextInt();
int n = r.nextInt();
#7
可以个Scanner类 如1楼写的测试代码。也可以自己写一个控制台输入类。
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
/**
A class to read strings and numbers from an input stream.
This class is suitable for beginning Java programmers.
It constructs the necessary buffered reader,
handles I/O exceptions, and converts strings to numbers.
*/
public class ConsoleReader
{ /**
Constructs a console reader from an input stream
such as System.in
@param inStream an input stream
*/
//such as system.in this is the inputstream
public ConsoleReader(InputStream inStream)
{ reader = new BufferedReader
(new InputStreamReader(inStream));
}
/**
Reads a line of input and converts it into an integer.
The input line must contain nothing but an integer.
Not even added white space is allowed.
@return the integer that the user typed
*/
public int readInt()
{ String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
/**
Reads a line of input and converts it into a floating-
point number. The input line must contain nothing but
a nunber. Not even added white space is allowed.
@return the number that the user typed
*/
public double readDouble()
{ String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
/**
Reads a line of input. In the (unlikely) event
of an IOException, the program terminates.
@return the line of input that the user typed, null
at the end of input
*/
public String readLine()
{ String inputLine = "";
try
{ inputLine = reader.readLine();
}
catch(IOException e)
{ System.out.println(e);
System.exit(1);
}
return inputLine;
}
private BufferedReader reader;
}
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
/**
A class to read strings and numbers from an input stream.
This class is suitable for beginning Java programmers.
It constructs the necessary buffered reader,
handles I/O exceptions, and converts strings to numbers.
*/
public class ConsoleReader
{ /**
Constructs a console reader from an input stream
such as System.in
@param inStream an input stream
*/
//such as system.in this is the inputstream
public ConsoleReader(InputStream inStream)
{ reader = new BufferedReader
(new InputStreamReader(inStream));
}
/**
Reads a line of input and converts it into an integer.
The input line must contain nothing but an integer.
Not even added white space is allowed.
@return the integer that the user typed
*/
public int readInt()
{ String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
/**
Reads a line of input and converts it into a floating-
point number. The input line must contain nothing but
a nunber. Not even added white space is allowed.
@return the number that the user typed
*/
public double readDouble()
{ String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
/**
Reads a line of input. In the (unlikely) event
of an IOException, the program terminates.
@return the line of input that the user typed, null
at the end of input
*/
public String readLine()
{ String inputLine = "";
try
{ inputLine = reader.readLine();
}
catch(IOException e)
{ System.out.println(e);
System.exit(1);
}
return inputLine;
}
private BufferedReader reader;
}
#8
这些东西嘛 简单啊 你问的情况基本是控制台输入
百度上有例子的 如果是做页面 什么数据都是键盘输入了啊
百度上有例子的 如果是做页面 什么数据都是键盘输入了啊
#9
至于你说要try catch 块 那个是因为有些东西会产生异常 需要去捕捉异常或是抛出
你只要把你需要执行的代码放在他们之间就可以了 至于异常信息 可以在catch块你输出
当然 也可以在方法名后直接抛出异常 丢给上级自动处理或继续抛出
你只要把你需要执行的代码放在他们之间就可以了 至于异常信息 可以在catch块你输出
当然 也可以在方法名后直接抛出异常 丢给上级自动处理或继续抛出
#1
import java.util.Arrays;
import java.util.Scanner;
public class F {
public static void main(String[] agrs) {
/*接收数据*/
System.out.println("数组的大小?");
Scanner r = new Scanner(System.in);
int n = r.nextInt();
System.out.println("输入数组:");
int[] num = new int[n];
for (int i = 0; i < n; i++) {
num[i] = r.nextInt();
}
/*排序*/
Arrays.sort(num);
/*输出*/
for (int i : num) {
System.out.println(i);
}
}
}
这是一个简单的例子~实现从键盘键入一个整型数组并排序输出~~
看看吧~~
具体参见相关API~~~~~~
#2
你要实现仅仅是键盘输入还是键盘的监听?
#3
Scanner,I/O流都可以
#4
import java.util.Scanner;
#5
#6
Scanner r = new Scanner(System.in);
int n = r.nextInt();
int n = r.nextInt();
#7
可以个Scanner类 如1楼写的测试代码。也可以自己写一个控制台输入类。
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
/**
A class to read strings and numbers from an input stream.
This class is suitable for beginning Java programmers.
It constructs the necessary buffered reader,
handles I/O exceptions, and converts strings to numbers.
*/
public class ConsoleReader
{ /**
Constructs a console reader from an input stream
such as System.in
@param inStream an input stream
*/
//such as system.in this is the inputstream
public ConsoleReader(InputStream inStream)
{ reader = new BufferedReader
(new InputStreamReader(inStream));
}
/**
Reads a line of input and converts it into an integer.
The input line must contain nothing but an integer.
Not even added white space is allowed.
@return the integer that the user typed
*/
public int readInt()
{ String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
/**
Reads a line of input and converts it into a floating-
point number. The input line must contain nothing but
a nunber. Not even added white space is allowed.
@return the number that the user typed
*/
public double readDouble()
{ String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
/**
Reads a line of input. In the (unlikely) event
of an IOException, the program terminates.
@return the line of input that the user typed, null
at the end of input
*/
public String readLine()
{ String inputLine = "";
try
{ inputLine = reader.readLine();
}
catch(IOException e)
{ System.out.println(e);
System.exit(1);
}
return inputLine;
}
private BufferedReader reader;
}
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
/**
A class to read strings and numbers from an input stream.
This class is suitable for beginning Java programmers.
It constructs the necessary buffered reader,
handles I/O exceptions, and converts strings to numbers.
*/
public class ConsoleReader
{ /**
Constructs a console reader from an input stream
such as System.in
@param inStream an input stream
*/
//such as system.in this is the inputstream
public ConsoleReader(InputStream inStream)
{ reader = new BufferedReader
(new InputStreamReader(inStream));
}
/**
Reads a line of input and converts it into an integer.
The input line must contain nothing but an integer.
Not even added white space is allowed.
@return the integer that the user typed
*/
public int readInt()
{ String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
/**
Reads a line of input and converts it into a floating-
point number. The input line must contain nothing but
a nunber. Not even added white space is allowed.
@return the number that the user typed
*/
public double readDouble()
{ String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
/**
Reads a line of input. In the (unlikely) event
of an IOException, the program terminates.
@return the line of input that the user typed, null
at the end of input
*/
public String readLine()
{ String inputLine = "";
try
{ inputLine = reader.readLine();
}
catch(IOException e)
{ System.out.println(e);
System.exit(1);
}
return inputLine;
}
private BufferedReader reader;
}
#8
这些东西嘛 简单啊 你问的情况基本是控制台输入
百度上有例子的 如果是做页面 什么数据都是键盘输入了啊
百度上有例子的 如果是做页面 什么数据都是键盘输入了啊
#9
至于你说要try catch 块 那个是因为有些东西会产生异常 需要去捕捉异常或是抛出
你只要把你需要执行的代码放在他们之间就可以了 至于异常信息 可以在catch块你输出
当然 也可以在方法名后直接抛出异常 丢给上级自动处理或继续抛出
你只要把你需要执行的代码放在他们之间就可以了 至于异常信息 可以在catch块你输出
当然 也可以在方法名后直接抛出异常 丢给上级自动处理或继续抛出