Your are to output the length of a string. There are several test cases for each test group.
Input: Test case count T following by each test case. Example:
4
asdf
avc asd as
00100
Output:
4
8
10
5
我的解答:
- import java.util.Scanner;
- public class Main {
- public static void main(String args[]) {
- Scanner s = new Scanner(System.in);
- int n = s.nextInt();
- s.nextLine();
- for(int i = 0; i < n; i++) {
- String str = s.nextLine();
- System.out.println(str.length());
- }
- }
- }
其他同学的解答:
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args){
- int count;
- Scanner in = new Scanner(System.in);
- count = Integer.parseInt(in.nextLine());
- for(int i=0; i<count; i++){
- String str = in.nextLine();
- System.out.println(str.length());
- }
- in.close();
- }
- }
语法:
该方法有两种变型
static int parseInt(String s)
static int parseInt(String s,int radix)
参数:下面是参数的细节
String s:这是十进制的字符串表示形式
Int radix:这将用于将字符串转换为整数
返回值:
parseInt(String s):This returns an integer(decimal only).
parseInt(int i):This returns an integer,given a string representation of decimal,binary,octal,or hexadecimal(radix equals 10,2,8 or 16 respectively) numbers as input.
实例:
public class Test{
public static void main(String args[]){
int x =Integer.parseInt("9");
double c = Double.parseDouble("5");
int b = Integer.parseInt("444",16);
System.out.println(x);
System.out.println(c);
System.out.println(b);
}
}
这将输出以下结果:
9
5.0
1092
其中int b = Integer.parseInt("444",16); 是将444作为16进制数转换为10进制数
总之,这是一道很简单的题目,只要清楚string.length()函数就可以