云大JAVA实验4Lab4陆歌浩_第1页
云大JAVA实验4Lab4陆歌浩_第2页
云大JAVA实验4Lab4陆歌浩_第3页
云大JAVA实验4Lab4陆歌浩_第4页
云大JAVA实验4Lab4陆歌浩_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、云南大学软件学院 实验报告课程: java程序设计实验 学期: 2012-2013学年 第一学期 任课教师: 陆歌浩 专业: 软件工程 学号: 序号: 姓名: 成绩: 实验4 fundamental programming structures in java: strings一、实验目的练习java基本编程结构,包括字符串,输入输出等。二、实验内容1. program 1write a program called string1.java and do the following: create a string object that accepts input from the use

2、r. referencing the jdk docs, execute 2 different methods on the string. print the results of the methods.result:length of string is 11first 5 bytes of string are how aimport javax.swing.joptionpane;public class string1public static void main(string arg)string inputvalue = joptionpane.showinputdialog

3、("请输入一个字符串:"); /接受用户输入的(字符串)stringbuffer temp=new stringbuffer();temp.append(inputvalue);joptionpane.showmessagedialog(null, "length of string is: " + temp.length()+".", "执行结果", joptionpane.plain_message); joptionpane.showmessagedialog(null, "first 5 byte

4、s of string are: " + temp.substring(0,5)+".", "执行结果", joptionpane.plain_message); 运行截图: 图1.1(输入文本框) 图1.1(显示字符串长度)图1.3(显示字符串的前5个字符)2. program 2write a program called stringbuffer1.java that does the following: create a stringbuffer object. referencing the jdk docs, execute 2

5、different methods on the stringbuffer. print the results of the methods.for example, using reverse() and replace() methods:output:input string reversed is: tset a si sihtreplacing characters 3 thru 5 with * looks like: ts* si sihtimport javax.swing.joptionpane;public class stringbuffer1public static

6、 void main(string arg)string inputvalue = joptionpane.showinputdialog("请输入一个字符串:"); /接受用户输入的(字符串)stringbuffer temp=new stringbuffer();temp.append(inputvalue);string str="*"joptionpane.showmessagedialog(null, "input string reversed is: " + temp.reverse(), "执行结果"

7、;, joptionpane.plain_message); joptionpane.showmessagedialog(null, "replacing characters 3 thru 5 with * looks like: " + temp.replace(2, 5, str)+".", "执行结果", joptionpane.plain_message); 运行截图: 图2.1(输入) 图2.2(字符串倒置)图2.3(替换后结果)3. program 3write a program called stringtokeni

8、zer1.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

9、" display the input string and the resulting strings.kunmingyunnanchinaimport javax.swing.joptionpane;public class stringtokenizer1public static void main(string arg)string inputvalue = joptionpane.showinputdialog("input a list separated by commas."); /接受用户输入的(字符串)string a=inputvalue.

10、split(",");/调用split方法for(string temp:a) system.out.print(temp); system.out.println(); 运行截图:图3.1(输入)图3.2(切割后结果)4. program 4design and write a java class named salesarray.java that will: 1. declare a two-dimensional integer array named sales.  populate the first four columns using the f

11、ollowing data.quarter 1quarter 2quarter3quarter 4totaldept. 1750660910800dept. 2800700950900dept. 3700600750600dept. 48508001000950dept. 5900800960980total2. write a loop to compute and populate the total column.  within the loop, dispaly each department total as it is computed.3. write a loop

12、to compute and populate the total row.  within the loop, display each quarter's total as it is computed.public class salesarray public static void main(string args) int a= 750,660, 910, 800, 800,700, 950, 900, 700,600, 750, 600, 850,800, 1000,950, 900,800, 960, 980 ;/建立一个已知值的数组; string s=&q

13、uot; ","quarter1 ","quarter2 ","quarter3 ","quarter4","total " for (int j = 0; j <s.length; j+) system.out.printf("%9s",sj); system.out.println( );/用于显示首行int total=0;int total2=0;int total3=0;for(int i=0;i<a.length;i+)system.out.

14、print("dept."+(i+1)+":");for(int j=0;j<ai.length;j+)system.out.printf("%9d", aij);total+=aij;total3+=aij;/用于存放数组元素的总和system.out.println(" "+total);total=0;/打印出每一行的总和后将总和置为零,用于统计下一行的总和。 /total用于统计数组元素的总和,故不需要置零。/打印出数组并计算显示行的total值;system.out.print("total :");for(int i=0;i<a0.length;i+)for(int j=0;j<a.length;j+)total2+=aji;system.out.printf("%9d", total2);total2=0; /打印出每一列

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论