实验3数组和字符串 一、实验目的 1掌握一维数组的创建、初始化、数组元者的引用等方法, 2.掌握字符串处理的相关方法和StringBuffer类的用法。 二、实验要求 1,编写一维数组的程序, 2.编写字符串处理和StringBuffer类的相关的程序。 三、实验内容 (一)一维数组的练习 。程序功能:使用javalang.Math类,生成20个99之间的随机整数,并统计大于 50的整数个数。 程序代码 public class MathRandom Test public static void main(Stringl args) int count =0 int num[]=new int[20] inti, fori=1:i<=20:i++){ num[i-1]=(int)(100*Math.random()); System.out.print(num[i-]+(i%10==0?"n":"")) if(num[i-1]>50) count++; System out.println("The mumber of random more than 50 is:"+count): } 编译并运行程序,查看Java API文档,学习Math类的使用方法,写出Math.random( 的取值范围是多少
实验 3 数组和字符串 一、实验目的 1. 掌握一维数组的创建、初始化、数组元素的引用等方法。 2. 掌握字符串处理的相关方法和 StringBuffer 类的用法。 二、实验要求 1. 编写一维数组的程序,。 2. 编写字符串处理和 StringBuffer 类的相关的程序。 三、实验内容 (一) 一维数组的练习 程序功能:使用 java.lang.Math 类,生成 20 个 0~99 之间的随机整数,并统计大于 50 的整数个数。 程序代码: public class MathRandomTest { public static void main(String[] args) { int count = 0; int num[] = new int[20]; int i; for (i = 1; i <= 20; i++) { num[i - 1] = (int) (100 * Math.random()); System.out.print(num[i - 1] + (i % 10 == 0 ? "\n" : " ")); if (num[i - 1] > 50) count++; } System.out.println("The mumber of random more than 50 is:" + count); } } 编译并运行程序,查看 Java API 文档,学习 Math 类的使用方法,写出 Math.random() 的取值范围是多少?
(仁)字符串的练习 程序功能:字符串操作 程序代码: public class StringTest public static void main(String[]args){ String s ="ABCDEFGHIJKLMN" System.out.println(s): System.out.println("The string contains"+s.length(). System.out.println("The character at index 4 is:"+s.charAt(4)): System.out.println("The index of character N is:"+s.indexOf(N)); String alphabet ="ABCDEFGHIKLMN": System.out.println(alphabet). System.outprintn("The substring from index 4 to indexs alphabet.substring(4,8)) System.out.println("The substring from index 0 to index 8 is' alphabet.substring(0.8)): System.out.println("The substring from index8 to end is" alphabet.subs ring(8)) String sbis="StringBufferInputStream" System.out.println(sbis). String sbislc sbis toLowerCase() System out println(sbislc) String sbisup=sbis.toUpperCase() System.out.println(sbisup): String first "James" String last ="Gosling". System.out.println(first last) String str sis the Mississippi River System.out.printin(str int k=0. System.out.println("The next index of's is"+k); k=str.lastIndexOf('s): intn=44; System.out.println("n=+n) String strn=String.valueOf(n); System.out.println("strn="+strn) String today ="May 18.2009": ing=today.substring(46) -Integer.parselnt(o ysDayString) int nextWeeksDayInt =todaysDayInt+7. String nextWeek=today.substring(0,4)+nextWeeksDayInt +today.substring(6):
(二) 字符串的练习 程序功能:字符串操作 程序代码: public class StringTest { public static void main(String[] args) { String s = "ABCDEFGHIJKLMN"; System.out.println(s); System.out.println("The string contains" + s.length()); System.out.println("The character at index 4 is:" + s.charAt(4)); System.out.println("The index of character N is:" + s.indexOf('N')); String alphabet = "ABCDEFGHIJKLMN"; System.out.println(alphabet); System.out.println("The substring from index 4 to index 8 is" + alphabet.substring(4, 8)); System.out.println("The substring from index 0 to index 8 is" + alphabet.substring(0, 8)); System.out.println("The substring from index 8 to end is" + alphabet.substring(8)); String sbis = "StringBufferInputStream"; System.out.println(sbis); String sbislc = sbis.toLowerCase(); System.out.println(sbislc); String sbisup = sbis.toUpperCase(); System.out.println(sbisup); String first = "James"; String last = "Gosling"; System.out.println(first + last); System.out.println(first + "," + last); String str = "This is the Mississippi River"; System.out.println(str); int k = 0; System.out.println("The next index of 's' is " + k); k = str.lastIndexOf('s'); int n = 44; System.out.println("n= " + n); String strn = String.valueOf(n); System.out.println("strn= " + strn); String today = "May 18,2009"; String todaysDayString = today.substring(4, 6); int todaysDayInt = Integer.parseInt(todaysDayString); int nextWeeksDayInt = todaysDayInt + 7; String nextWeek = today.substring(0, 4) + nextWeeksDayInt + today.substring(6);
System.out.println("Today's date is"+today): System.("ext week's date) System.out.println("next week's date is"nextWeek); 编译并运行程序,对照JavaAPI文档,观察字符串类String的使用方法。 (三)StringBuffer类的练习: 分析下面的程序,运行结果应该是什么? public class Test public static void main(String[]args){ StringBuffer a=new StringBuffer("A"): StringBuffer b=new StringBuffer("B") method(a.b), System.out.println(a+"+b) static void method(StringBuffer x,StringBuffer y) x.append(y); y=x } ·编译并运行程序,验证你的分析是否正确。 (四)程序设计: ·(I)一次录入5位学生的Java成绩,并求出最高成绩,实现下图所示的效果: 请一次录入5位学生的Java成绩: 最高Java成绩:g6 (2)从健盘录入一个字符串,并逆序输出。实现效果如下图所示: 请输入一个字符串: 这个学行串的逆序是。 trevq
System.out.println("Today's date is" + today); System.out.println("Today's date is" + todaysDayInt); System.out.println("next week's date is" + nextWeeksDayInt); System.out.println("next week's date is" + nextWeek); } } 编译并运行程序,对照 Java API 文档,观察字符串类 String 的使用方法。 (三)StringBuffer 类的练习: 分析下面的程序,运行结果应该是什么? public class Test { public static void main(String[] args) { StringBuffer a = new StringBuffer("A"); StringBuffer b = new StringBuffer("B"); method(a, b); System.out.println(a + "," + b); } static void method(StringBuffer x, StringBuffer y) { x.append(y); y = x; } } 编译并运行程序,验证你的分析是否正确。 (四)程序设计: (1)一次录入 5 位学生的 Java 成绩,并求出最高成绩,实现下图所示的效果: (2)从键盘录入一个字符串,并逆序输出。实现效果如下图所示: