1、字符串最后一个单词长度
题目描述
计算字符串最后一个单词的长度,单词以空格隔开。
输入描述:
一行字符串,长度小于128。
输出描述:
整数N,最后一个单词的长度。
输入例子:
hello world
输出例子:
5
<h5 style="font-family: arial, STHeiti, 'Microsoft YaHei', 宋体; margin: 10px 0px; padding: 0px; font-size: 16px;">提交代码</h5>
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str = in.nextLine();
String strs[] = str.split(" ");
int length = strs.length;
String lastWord=strs[length-1];
System.out.println(lastWord.length());
}
in.close();
}
}
2、计算字符个数
题目描述
写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
输入描述:
输入一个有字母和数字以及空格组成的字符串,和一个字符。
输出描述:
输出输入字符串中含有该字符的个数。
输入例子:
ABCDEF A
输出例子:
1
提交代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String str= in.nextLine().toUpperCase();
int count=0;
String str2 = in.nextLine().toUpperCase();
char ch = str2.charAt(str2.length()-1);
for(int i=0;i<str.length();i++){
char temp = str.charAt(i);
if (temp == ch) count++;
}
System.out.println(count);
in.close();
}
}
3、明明的随机数
题目描述
明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。 Input Param n 输入随机数的个数 inputArray n个随机整数组成的数组 Return Value OutputArray 输出处理后的随机整数 注:测试用例保证输入参数的正确性,答题者无需验证。
输入描述:
输入多行,先输入随机整数的个数,在输入相应个数的整数
输出描述:
返回多行,处理后的结果
输入例子:
11 10 20 40 32 67 40 20 89 300 400 15
输出例子:
10 15 20 32 40 67 89 300 400
提交代码
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int num = in.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < num; i++) {
int val= in.nextInt();
if (!list.contains(val)) {
list.add(val);
}
}
int[] arry = new int[list.size()];
for (int i = 0; i < arry.length; i++) {
arry[i]=list.get(i);
}
<span style="font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;">Arrays.sort(arry); </span>
for (int i = 0; i < arry.length; i++) {
System.out.println(arry[i]);
}
}
}
}
4、字符串分隔
题目描述
•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入描述:
连续输入字符串(输入2次,每个字符串长度小于100)
输出描述:
输出到长度为8的新字符串数组
输入例子:
abc 123456789
输出例子:
abc00000 12345678 90000000
提交程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str1 = in.nextLine();
String[] sb1 = getStrs(str1);
for (int i = 0; i < sb1.length; i++) {
System.out.println(sb1[i]);
}
}
in.close();
}
private static String[] getStrs(String str)
{
int length = str.length();
int cnt=length/8;
int mod=length%8;
if (mod !=0) {
cnt = cnt+1;
for (int i = 0; i < 8-mod; i++) {
str +="0";
}
}
String[] sb = new String[cnt];
for (int i = 0,index=0; i < cnt; i++,index += 8) {
sb[i] = (str.substring(index, index+8));
}
return sb;
}
}
5、进制转换
题目描述
写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。
输入描述:
输入一个十六进制的数值字符串。
输出描述:
输出该数值的十进制字符串。
输入例子:
0xA
输出例子:
10
输出例子:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String string = (String) in.next();
String str = string.substring(2, string.length());
int num = Integer.valueOf(str, 16);
// Integer num = Integer.decode(string);
System.out.println(num);
}
in.close();
}
}
6、编程总结
1、Scanner类
重用方法:
close()
delimiter()
useDelimiter()
hasNext();
next()
nextLine()
nextInt()
skip()
//从控制台读取字符串
//
hasNext()方法判断Sanner实例中的buffer中是否还有没有被读取的数据,有则返回true,没有则返回false
//next()方法读取以空白字符串结束的字符串(即""、'\t'、'\f'、'\r'或'\n')
//nextline()方法读取一整行文本,已读取按下enter回车键为结束的字符串
//useDelimiter()可以设置新的分隔符模式
//标记与期望的类型不匹配,抛出运行时异常java.util.InputMistmatchException
// String lineSeparator = System.getProperty("line.separator") 得到特定平台上的行分隔符
//行分隔符 Linux\r\n Windows \n
//重要警告:为了防止读取错误,不要在标记读取方法nextInt()等,后面调用nextLine()
//next()方法读取以空白字符串结束的字符串(即""、'\t'、'\f'、'\r'或'\n')
//nextline()方法读取一整行文本,已读取按下enter回车键为结束的字符串
//useDelimiter()可以设置新的分隔符模式
//标记与期望的类型不匹配,抛出运行时异常java.util.InputMistmatchException
// String lineSeparator = System.getProperty("line.separator") 得到特定平台上的行分隔符
//行分隔符 Linux\r\n Windows \n
//重要警告:为了防止读取错误,不要在标记读取方法nextInt()等,后面调用nextLine()
2、String类
常用方法
charAt
contains()
getBytes()
letgth()
split()
toLowerCase()
toCharArray()
valueOf(double d)
3、Comparable接口
定义了compareTo方法
java类库中许多类定义了对象的自然顺序,Byte,Short,Integer,Long,Double,Character,BigInteger,BigDecimal,Calender,String,Date都实现了comparable接口,
java.util.sort(object[] )