JAVA从控制台获取数据的语句_Java从控制台获取数据的方法

  • Post author:
  • Post category:java


http://www..com/qneverever/p/4423975.html

java从控制台获取数据,回车键结束输入:

1.http://blog..net/brian512/article/details/41844305

Scanner scanner = new Scanner(System.in);

ArrayList ns = new ArrayList<>();

do {

String string = scanner.nextLine();

if (string.equals(“”)) {

break;

}

ns.add(Integer.valueOf(string));

} while (true);

System.out.println(ns);

}

2.http://www.educity.cn/wenda/457294.html

ArrayList array = new ArrayList();

Scanner scn = new Scanner(System.in);

String line;

while (!”end”.equals(line = scn.nextLine())) {

array.add(line);

}

for(String str : array){

System.out.println(str);

}

3.

public static void main(String args[])

{

ArrayList array = new ArrayList();

ArrayList ns = new ArrayList<>();

Scanner scn = new Scanner(System.in);

String line;

while (!”end”.equals(line = scn.nextLine())) {

array.add(line);

ns.add(Integer.valueOf(line));

}

for(String str : array){

System.out.println(str);

}

System.out.println(ns);

}

4.http://blog..net/zzg1229059735/article/details/51727413

public static void main(String[] args)

{

StringBuilder stringbuilder = new StringBuilder();

Scanner scanner = new Scanner(System.in);

while(true)

{

String text = scanner.nextLine().trim();

if (“”.equals(text))

{

break;

}

stringbuilder.append(text);

}

System.out.println(stringbuilder.toString());

}

5.http://bbs..net/topics/360005013

public static void main(String[] args) {

try {

String line = null;

StringBuilder builder = new StringBuilder();

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

while ((line = br.readLine()) != null) {

if (line.equalsIgnoreCase(“EOF”)){

break;

} else{

builder.append(line);

}

}

String ss = builder.toString();

System.out.println(ss.replaceAll(“you”, “we”));

} catch (Exception e) {

e.printStackTrace();

}

}

6.

public static void main(String[] args) throws IOException {

InputStreamReader is = new InputStreamReader(System.in);

char[] chs = new char[1000];

int len = is.read(chs);

String str = new String(chs, 0, len);

if (str.equals(“\r\n”))

{

System.out.println(“ok”);

}

System.out.println(str);

}

7.http://blog..net/wusahaoshuai/article/details/7777922

public static String[] getStr() {

String ab = “”;

try {

BufferedReader br = new BufferedReader(new InputStreamReader(

System.in));

System.out.print(“请输入:”);

ab = br.readLine();

} catch (IOException e) {

}

return setStr(ab);

}

public static String[] setStr(String first) {

String[] arry = new String[50];

String flag = “”, chart = “”;

int j = 0;

for (int i = 0; i < first.length(); i++) {

flag = first.substring(i, i + 1);

if (!flag.equals(” “)) {

chart += flag;

}

if (flag.equals(” “) && chart != “” || i == first.length() – 1) {

arry[j] = chart;

j++;

chart = “”;

}

}

return arry;

}

public static void show(String arry[]) {

for (int i = 0; (i < arry.length && arry[i] != null); i++) {

System.out.println(“第” + (i + 1) + “个数是:” + arry[i]);

}

}

public static void main(String[] args) {

String[] arry = new String[50];

arry = getStr();

show(arry);

}

8.

public static void main(String args[]) {

int[] a = new int[10];

int i = 0;

ArrayList ns = new ArrayList<>();

ArrayList array1 = new ArrayList();

String line = null;

Scanner reader = new Scanner(System.in);

line=reader.nextLine();

String[] array =line.split(“\\s+”);

for (int j = 0; j < array.length; j++) {

ns.add(Integer.valueOf(array[j]));

array1.add(array[j]);

}

// for(String s:array)

// {

// System.out.println(s);

// }

System.out.println(array1);

System.out.println(ns);

}

9

public static void main(String[] args) {

Scanner input = new Scanner(System.in); // TODO 自动生成方法存根

String[] names = new String[10];

for (int i = 0; i < 10; i++) {

System.out.println(“请输入第” + (i + 1) + “学生姓名:”);

names[i] = input.nextLine();

}

for (String s : names) {

System.out.println(s);

}

}

10. 获取一行字符串,按空格切割存到数组中,将数组中的每个数转换为整形,存到集合,然后输出。

public static void main(String args[]) {

int[] a = new int[10];

int i = 0;

ArrayList ns = new ArrayList<>();

ArrayList array1 = new ArrayList();

String line = null;

Scanner reader = new Scanner(System.in);

line = reader.nextLine();

String[] array = line.split(“\\s+”);

for (int j = 0; j < array.length; j++) {

ns.add(Integer.valueOf(array[j]));

array1.add(array[j]);

}

System.out.println(array1);

System.out.println(ns);

}



版权声明:本文为weixin_42601547原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。