当前位置:网站首页>实验七 常用类的使用
实验七 常用类的使用
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()方法可以用来获取随机数。
边栏推荐
- [the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
- 3. Input and output functions (printf, scanf, getchar and putchar)
- 5.MSDN的下载和使用
- [the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
- [中国近代史] 第六章测验
- 2.初识C语言(2)
- vector
- C language Getting Started Guide
- MySQL事务及实现原理全面总结,再也不用担心面试
- C language Getting Started Guide
猜你喜欢

Using spacedesk to realize any device in the LAN as a computer expansion screen

1.C语言矩阵加减法

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

【手撕代码】单例模式及生产者/消费者模式

1.C语言初阶练习题(1)

2. Preliminary exercises of C language (2)

5. Download and use of MSDN
![[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP

(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。

The difference between cookies and sessions
随机推荐
3.猜数字游戏
Custom RPC project - frequently asked questions and explanations (Registration Center)
2.C语言矩阵乘法
Using spacedesk to realize any device in the LAN as a computer expansion screen
1. First knowledge of C language (1)
Aurora system model of learning database
Relationship between hashcode() and equals()
canvas基础1 - 画直线(通俗易懂)
7-11 机工士姆斯塔迪奥(PTA程序设计)
ArrayList的自动扩容机制实现原理
1. C language matrix addition and subtraction method
C语言实现扫雷游戏(完整版)
[中国近代史] 第六章测验
2.初识C语言(2)
View UI plus released version 1.3.0, adding space and $imagepreview components
深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning
魏牌:产品叫好声一片,但为何销量还是受挫
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
[modern Chinese history] Chapter V test
1.初识C语言(1)