java课后作业一道关于统计字符串长度的题
2014-09-27 21:02
295 查看
题目:
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();
}
}
其中Integer.parseInt我没有见过,经过查阅资料,发现它是一个得到原始数据类型的一个特定的字符串
语法:
该方法有两种变型
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.
实例:
这将输出以下结果:
其中int b = Integer.parseInt("444",16); 是将444作为16进制数转换为10进制数
总之,这是一道很简单的题目,只要清楚string.length()函数就可以
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();
}
}
其中Integer.parseInt我没有见过,经过查阅资料,发现它是一个得到原始数据类型的一个特定的字符串
语法:
该方法有两种变型
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()函数就可以
相关文章推荐
- java字符串的课后作业
- 一道关于截取字符串的java面试题
- 问题求解,关于java随机生成指定长度的字符串
- 关于java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 无效的字符串或缓冲区长度问题
- 一道Java的课后作业题
- C++ primer plus 第六版课后作业和题——第四章——关于字符串、结构体、共同体、枚举的知识
- java编程---3.2 (关于String的练习题) 统计字符串中每个字符的个数,例如:把aaaabbaaccbb变成a6b4c2
- java 一道关于String字符串操作题目
- 关于此次java作业中C/S架构的初步设想
- 统计字符串的长度(带汉字)
- C#统计中英文混排字符串的长度
- C#统计中英文混排字符串的长度
- 用java截取字符串长度的方法
- 一道scjp题,关于java 变量引用的,知道为什么输出10,20吗?
- (转)java关于split分割字符串,空的字符串不能得到的问题
- C#区分中英文统计字符串的长度
- java中按字节数的长度来截取字符串,并以...来忽略
- java关于split分割字符串,空的字符串不能得到的问题
- 计算字符串长度(关于日文字符)
- 关于DataGrid中字符串长度的设置