java实验四云南大学_云南大学软件学院Java实验四

41528d3028836879cd698677c3999917.gif云南大学软件学院Java实验四

云南大学软件学院 实 验 报 告 姓名: 王定欢 学号: 班级: 日期: 2016.10.13 成绩: JAVA实验四 一、 实验目的: Fundamental Programming Structures in Java: Strings 二、 实验要求: 1. Write a program called String1.java and do the following: Create a String object that accepts from the user. Referencing the JDK docs, cute 2 different s on the String. Print the results of the s. 2. Write a program called StringBuffer1.java that does the following: Create a StringBuffer object. Referencing the JDK docs, cute 2 different s on the StringBuffer. Print the results of the s. 3. Write a program called StringTokenizer1.java that accepts a string, looks for commas within the string, and breaks the string up into the parts separated by the comma. For example, the string “Kunming, Yunnan, China“ would return three strings: String1 = “Kunming“ String2 = “Yunnan“ String3 = “China“ 三、 实验内容: 1、①代码: package lab4; import java.util.Scanner; public class String1 { public static void main(String[] args){ System.out.println(“please a string“); Scanner in = new Scanner(System.in); String = in.nextLine(); System.out.println(“the string is:“+); .toUpperCase();//将字符串中的小写字母全部转化为大写字母 System.out.println(“the uppercase string is:“+.toUpperCase()); System.out.println(“the string length is:“+.length()); } } ② 实验结果: 2、①代码: package lab4; public class StringBuffer1 { public static void main(String[] args){ StringBuilder builder = new StringBuilder(); builder.append(“hello“).append(“world“); String completeString = builder.toString();//构建字符串方法 System.out.println(“the complete string is:“+completeString); StringBuilder insertString = builder.insert(10,“I love java“);//将字符插入到字符串中 System.out.println(“after insert string is:“+insertString); } } ②、实验结果: 3、①代码: package lab4; import java.util.Scanner; import java.util.StringTokenizer; public class StringTokenizer1 { public static void main(String[] args){ Scanner in = new Scanner(System.in); String test = new String(); System.out.println(“please a string with commas:“); test = in.nextLine(); StringTokenizer str = new StringTokenizer(test,“,“);//用StringTokenizer方法将字符串分段 int j=1; while(str.hasMoreElements()){ System.out.println(“String“+j+“=“+str.nextToken()); j++; } StringBuilder rs = new StringBuilder();//倒序输出 for(int i = test.length()-1;i>-1;i--){ rs.append(test.charAt(i)); } System.out.println(“逆序为:“+rs); } } ②实验结果: 4、附加题: ①代码:package lab4; import java.util.StringTokenizer; public class lab4 { public static void main(String[]args){ String name = new String(“My name is lilei“); System.out.println(“the length of string is:“+name.length()); char firstChar = name.charAt(0);//第一个位置 System.out.println(“the first char is:“+firstChar); System.out.printl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值