版权声明:转载请注明出处 https://blog.csdn.net/qq_26769591/article/details/81517444
JAVA
1输入一行字符串 并进行切分
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
/*
* */
public class print {
public static void main(String[] args) throws ParseException, IOException {
BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
//字符串
String str ;
//字符
char c;
//c在str中的下标
int index;
str = scanner.readLine();
String [] arrOfStr = str.split(" ", 5);//5表示分为5段
for (String a : arrOfStr)
System.out.println(a);
}
}
输出
12 34 56
12
34
56
Process finished with exit code 0
4 输出多行数据,有逗号 有分号 输出结果放在一个整形的动态List中 字符串的转换
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class c {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// System.out.println(n);
List<Integer> input = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
String[] str = sc.next().split(";");//str={1,3;2,3}
for (int j = 0; j < str.length; j++) {
String[] sss = str[j].split(",");//ss={1;3}
for (String a : sss) {
int x = Integer.parseInt(a);
input.add(x);
}
}
}
System.out.println(input);
}
}
输出
3
1,2;3,4
1,2;5,6
2,4;7,8
[1, 2, 3, 4, 1, 2, 5, 6, 2, 4, 7, 8]
2输入三行字符 字符串分割 匹配(华为第三题type的简易版)
public class print {
public static void main(String[] args) throws ParseException, IOException {
BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
//空的字符串数组
String []str=new String[3];
String [][] arrOfStr=new String[3][];
for( int i=0;i<3;i++) {
str[i] = scanner.readLine();
arrOfStr[i] = str[i].split(" ", 5);
}
if(arrOfStr[0][2].equals(arrOfStr[1][1]))
{
System.out.println(arrOfStr[0][2]+arrOfStr[1][2]);
}
}
}
输出
type int a
type a A
A
aA
Process finished with exit code 0
3 输入多行 空格分隔
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.Scanner;
public class c {
public static void main(String[] args) throws ParseException, IOException {
BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
//空的字符串数组
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String []str=new String[3];
String [][] arrOfStr=new String[3][];
for( int i=0;i<n;i++) {
str[i] = scanner.readLine();
arrOfStr[i] = str[i].split(" ");
};
//输出
for( int i=0;i<n;i++) {
for (String a : arrOfStr[i])
System.out.print(a+' ');
};
}
}
输出
2
1 2
2 3
1 2 2 3
C
C++
1.仅仅输入一行数字,数字之间以空格分隔 以换行符结束
#include<stdio.h>
#include<iostream>
//仅仅输入一行数字,数字之间以空格分隔 以换行符结束
using namespace std;
int main()
{
int nArr[100];
char c;
int i=0;
while((c=getchar())!='\n') //流中一个字符一个字符判断,直到换行,输入结束
{
if(c != ' ')
{
ungetc(c,stdin);//如果字符c不是空格,就要将c字符还回到流中,不然造成数据读取错误
cin>>nArr[i++];//流输入会将第一个数据全读入,直到遇到空格
}
}
return 0;
}
2.仅输入三行文字 行中可以有空格
#include<stdio.h>
#include<iostream>
//仅输入三行文字 行中可以有空格
using namespace std;
int main()
{
string str[3];
for(int i=0;i<3;i++)
getline(cin,str[i]);
return 0;
}
3不知道输入行数
输入数字
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int num;
vector <int>nums;
while(cin>>num)
{
nums.push_back(num);
}
vector<int>::iterator it; //vector的指针
for(it=nums.begin();it!=nums.end();it++)
{
cout<<(*it)<<endl;
}
return 0;
}
输入字符串
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string q;
vector<string>save_q;
while(getline(cin,q)) //以换行符为一行的结束
{
save_q.push_back(q);
}
vector<string>::iterator it;
for(it=save_q.begin();it!=save_q.end();it++)
{
cout<<(*it)<<endl;
}
return 0;
}