static int[] getInput() throws IOException { System.out.println("Please input some numbers: "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = in.readLine(); StringTokenizer st = new StringTokenizer(str); int count = st.countTokens(); int[] res = new int[count]; for (int i = 0; i < count; i++) { res[i] = Integer.parseInt(st.nextToken()); } return res; }// 需要java.io包和java.util包。
还有个问题,如果我要输入的是字符类型而不是INT,应如何改动??是不是改CHAR???万分感谢
// Test.javaimport java.io.*; import java.util.*;public class Test { public static void main(String[] args) throws IOException { System.out.println("Please input some numbers: "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = in.readLine(); StringTokenizer st = new StringTokenizer(str); int count = st.countTokens(); int[] res = new int[count]; for (int i = 0; i < count; i++) { res[i] = Integer.parseInt(st.nextToken()); } for (int i = 0; i < res.length; i++) { System.out.println(res[i]); } }
如果你要char,输入为类似"a c e h f r"这样// Test.javaimport java.io.*; import java.util.*;public class Test { public static void main(String[] args) throws IOException { System.out.println("Please input some numbers: "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = in.readLine(); StringTokenizer st = new StringTokenizer(str); int count = st.countTokens(); char[] res = new char[count]; for (int i = 0; i < count; i++) { res[i] = st.nextToken().charAt(0); } for (int i = 0; i < res.length; i++) { System.out.println(res[i]); } }
如果你要char,输入为类似"acehfr"这样 // Test.javaimport java.io.*;public class Test { public static void main(String[] args) throws IOException { System.out.println("Please input some chars: "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = in.readLine(); char[] res = str.getChars(); for (int i = 0; i < res.length; i++) { System.out.println(res[i]); } }
最后一例中, char[] res = str.getChars(); 改为: char[] res = str.toCharArray();
System.out.println("Please input some numbers: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = in.readLine();
StringTokenizer st = new StringTokenizer(str);
int count = st.countTokens();
int[] res = new int[count];
for (int i = 0; i < count; i++) {
res[i] = Integer.parseInt(st.nextToken());
}
return res;
}// 需要java.io包和java.util包。
import java.util.*;public class Test {
public static void main(String[] args) throws IOException {
System.out.println("Please input some numbers: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = in.readLine();
StringTokenizer st = new StringTokenizer(str);
int count = st.countTokens();
int[] res = new int[count];
for (int i = 0; i < count; i++) {
res[i] = Integer.parseInt(st.nextToken());
}
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}
import java.util.*;public class Test {
public static void main(String[] args) throws IOException {
System.out.println("Please input some numbers: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = in.readLine();
StringTokenizer st = new StringTokenizer(str);
int count = st.countTokens();
char[] res = new char[count];
for (int i = 0; i < count; i++) {
res[i] = st.nextToken().charAt(0);
}
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}
// Test.javaimport java.io.*;public class Test {
public static void main(String[] args) throws IOException {
System.out.println("Please input some chars: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = in.readLine();
char[] res = str.getChars();
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}
char[] res = str.getChars();
改为:
char[] res = str.toCharArray();