Java实验六.doc

  1. 1、本文档共5页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Java实验六

【一】实验内容及要求 实验名称:字符串、数组、异常处理 实验目的: 掌握数组的使用方法 掌握字符串类的使用方法 实验内容: 编写一程序,随机生成5个不大于100的整数存入一数组,计算该数组的平均值,输出该 5个整数及平均值。 2、编写一程序,该程序输入一个字符串参数,返回该字符串的反序字符串 3、编写一程序,计算矩阵A={{7,9,4},{5,6,8}}与矩阵 B={{9,5,2,8},{5,9,7,2},{4,7,5,8}}相乘,把结果存入矩阵C,并在屏幕输出结果。【二】完成报告 1.编写一程序,随机生成5个不大于100的整数存入一数组,计算该数组的平均值,输出该5个整数及平均值。 import java.util.Random; public class test1 { public static void main(String[] args) { Random a= new Random(); int b[] = new int[5]; double ave; int sum = 0; for (int i=0; i<5; i++) { b[i]=a.nextInt(100); System.out.println("b["+i+"]="+b[i]); sum = sum + b[i]; } ave = sum/5.0; System.out.println("平均数ave为:"+ave); } } 结果: b[1]=52 b[2]=75 b[3]=99 b[4]=23 平均数ave为:54.4import java.util.Scanner; public class test2 { public static void main(String[] args) { Scanner sc = new Scanner (System.in); String a = sc.next();//或者sc.nextLine(); byte [] b = a.getBytes(); byte [] c = new byte[b.length]; for (int i=0; i<b.length; i++) { c[i] = b[b.length - i - 1]; } String str = new String (c); System.out.println(str); } } 结果: abcd dcba 3、编写一程序,计算矩阵A={{7,9,4},{5,6,8}}与矩阵 B={{9,5,2,8},{5,9,7,2},{4,7,5,8}}相乘,把结果存入矩阵C,并在屏幕输出结果。 public class test3 { public static void main(String[] args) { int a[][] = {{7,9,4},{5,6,8}}; int b[][] = {{9,5,2,8},{5,9,7,2},{4,7,5,8}}; int x=a.length; int y=b[0].length; int z=b.length; int c[][] = new int [x][y]; for (int i=0; i<x; i++) { for (int j=0; j<y; j++) { c[i][j]=0; for (int k=0; k<z; k++) { c[i][j] = c[i][j]+a[i][k]*b[k][j]; } System.out.print(c[i][j]+" "); } System.out.println(); } } } 结果: 124 144 97 106 107 135 92 116 从键盘输入: import java.util.Scanner; public class test3 { public static void main(String[] args) { Scanner sc = new Scanner (System.in); System.out.println("请输入矩阵A的n1*n2中n1与n2的值"); System.out.print("n1:"); int n1 = sc.nextInt(); System.out.print("n2:");

文档评论(0)

kakaxi + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档