当前位置:网站首页>实验七 常用类的使用
实验七 常用类的使用
2022-07-06 09:22:00 【文文喜欢郭子吖】
实验七 常用类的使用
实验目的
1. 了解类库的概念及API的使用方法。
2. 掌握常用类的使用方法。
实验学时 2学时
实验内容
1. 输入两个字符串str1、str2,统计字符串str2出现在str1中的次数。
如:str1=”aaas lkaaas” ,str2=” as” ,则应输出 2
提示:文本输入
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in); //定义扫描键盘输入的对象
String s = sc.nextLine(); //从键盘读入一行文本
...
sc.close();
}
package code71;
import java.util.Scanner;
public class code71 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int sum=0;
System.out.print("请输入第一个字符串:");
String str1=in.nextLine(); //输入第一个字符串
System.out.print("请输入第二个字符串:");
String str2=in.nextLine(); //输入第二个字符串
int a=str1.indexOf(str2); //搜索str2在str1中出现的位置
while(a!=-1) {
sum++;
a=str1.indexOf(str2,a+1); //统计次数
}
System.out.println("字符串str2出现在str1中的次数为:"+sum+"次!");
}
}
2. 从键盘输入一串字符,输出有多少个不同的字符、每个字符出现的次数。
package code72;
import java.util.Scanner;
public class code72 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String str=in.nextLine();
StringBuffer b=new StringBuffer(str); //插入字符串缓冲区StringBuffer
int length=b.length(); //字符总数
System.out.println("总共有"+length+"个字母!");
String a; //声明单个字符
int index,sum;
for(int i=0;i<b.length();) {
sum=0;
a=b.substring(0,1); //截取单个字符
index=b.indexOf(a); //搜索单个字符在整个字符串中出现的位置
while(index!=-1) {
sum++;
b.deleteCharAt(index); //删除字符
index=b.indexOf(a,index); //统计次数
}
System.out.println(a+"字母有"+sum+"个!");
}
}
}3. 编写一个 Java 程序,将用户输入的句子当中每一个单词的第一个字母大写。
package code73;
import java.util.Scanner;
public class code73 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println("请输入一个句子:");
Scanner in = new Scanner(System.in);
String str = in.nextLine(); //输入原字符串
String str1 = ""; //最后输出的字符串
String[] a=str.split(" "); //将字符串按空格分割
for(int i=0;i<a.length;i++){ //遍历字符串,将每个字符串数组中的每个单词首字母替换为大写
a[i]=a[i].replace(a[i].charAt(0),a[i].toUpperCase().charAt(0));
str1+=a[i]+" "; //将替换后的字符串拼接到空串后
}
System.out.println("更改后的句子为:"+str1.trim());
}
}4. 输入一个八进制数字串(在整型数据范围内),分别以2进制、10进制、16进制输出。
提示:Integer.parseInt("100",8); //将8进制100转换成十进制数
Integer.toBinaryString(100); //将十进制整数100转换成二进制数
package code74;
import java.util.Scanner;
public class code74 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("输入一个八进制数字串(在整型数据范围内):");
String a=in.nextLine(); //输入原字符串
int b=Integer.parseInt(a); //原字符串转换为数字
System.out.println("2进制为:"+Integer.toBinaryString(b)); //将b转换为2进制数
System.out.println("8进制为:"+Integer.toOctalString(b)); //将b转换为8进制数
System.out.println("16进制为:"+Integer.toHexString(b)); //将b转换为16进制数
}5. 编写一段可生成一个随机字母的程序。
package code75;
import java.util.Arrays; //导入Arrays
public class code75 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
char a[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
//建立一个包含26个大小写字母的数组
char ch[]=new char[1]; { //一个字母
for(int i=0;i<1;i++)
{
int b;
b=(int)(Math.random()*(a.length)); //生成一个随机数
ch[i]=a[b]; //根据位置找到相应字母
}
System.out.println(Arrays.toString(ch)); //数字类型转换为字符串
}
}
}6. 编程求100天以后是几月几号星期几。
package code76;
import java.time.LocalDate; //导入LocalDate
public class code76 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String[] weekday = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"}; //建立星期数组
LocalDate today = LocalDate.now(); //获取当前时间
System.out.println("今天是:" + today + "," + weekday[today.getDayOfWeek().getValue()-1]); //输出今天的格式化时间
LocalDate hundredDayLater = LocalDate.now().plusDays(100); //获取100天后的时间
System.out.println("100天后是:" + hundredDayLater + "," + weekday[hundredDayLater.getDayOfWeek().getValue()-1]); //输出100天后的格式化时间
}
}实验小结
- Java系统中有很多预定义类,根据其功能不同划分成不同的包,所有包合称类库;
- 搜素指定字符或者字符串在另一个字符串中出现的位置,可以用indexOf()方法;
- 在字符串中截取子字符串时使用方法substring();
- StringBuffer类表示的是一个本身内容可变的字符串对象,包含一个缓冲区,主要用于完成字符串的动态添加、插入和替换等操作;
- 数字和字符串之间的转换,主要使用parseInt()方法和toString()方法;
- toBinaryString()、toHexString()、toOctalString()方法,可以分别将一个值以二进制、十六进制和八进制形式转化成字符串;
- Math类中的Random()方法可以用来获取随机数。
边栏推荐
- 4. Binary search
- [the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
- MySQL事务及实现原理全面总结,再也不用担心面试
- Leetcode.3 无重复字符的最长子串——超过100%的解法
- [the Nine Yang Manual] 2018 Fudan University Applied Statistics real problem + analysis
- 【九阳神功】2020复旦大学应用统计真题+解析
- C语言实现扫雷游戏(完整版)
- 仿牛客技术博客项目常见问题及解答(三)
- 强化学习系列(一):基本原理和概念
- 更改VS主题及设置背景图片
猜你喜欢

FAQs and answers to the imitation Niuke technology blog project (III)

It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan

Programme de jeu de cartes - confrontation homme - machine

6.函数的递归

4. Branch statements and loop statements

5. Function recursion exercise

3. Number guessing game

Caching mechanism of leveldb

View UI plus released version 1.3.0, adding space and $imagepreview components

SRC挖掘思路及方法
随机推荐
Relationship between hashcode() and equals()
[modern Chinese history] Chapter V test
3.C语言用代数余子式计算行列式
5.函数递归练习
1. C language matrix addition and subtraction method
2.C语言初阶练习题(2)
4. Binary search
MySQL中count(*)的实现方式
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
Caching mechanism of leveldb
vector
Safe driving skills on ice and snow roads
hashCode()与equals()之间的关系
强化学习系列(一):基本原理和概念
It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan
This time, thoroughly understand the MySQL index
2022泰迪杯数据挖掘挑战赛C题思路及赛后总结
杂谈0516
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
Why use redis