实验内容及步骤:
(1)代码: public class TeatString3 { public static void main(String[] args) { String str="abcd"; System.out.print("将字符串转换为字符数组后的结果:"); char[]charArray=str.toCharArray();//字符串转换为字符数组 for(int i=0;i<charArray.length;i++) { if(i!=charArray.length-1) { System.out.print(charArray[i]+","); } else { System.out.println(charArray[i]); } } System.out.println("将int类型转换为String类型之后的结果:"+String.valueOf(12)); System.out.println("将字符串转换成大写之后的结果:"+str.toUpperCase()); System.out.println("将字符串转换成小写之后的结果:"+str.toLowerCase()); System.out.println(new String(charArray)); String a=String.valueOf(charArray); 实验内容及步骤: System.out.println(a); } } (2)运行结果:
(1)代码: public class StudentManager { private student[] students = new student[6]; private int count; //添加 public void add(student m){ students[count]=m; count++; } //遍历 public void list(){ for (int i=0;i<count;i++) students[i].print(); } //修改 public void update(int no,int grade){ student s = students[no-1]; if(s != null && grade > 10){//输入值大于10为修改,小于10为增加 s.setGrade(grade); s.print();} else { s.setGrade(s.getGrade() + grade); } } 实验内容及步骤: } import java.util.Random; public class student { public String sno; public String sname; public int grade; Random random = new Random(); public student(){ } public student(String sno,String sname){ this.sno=sno; this.sname=sname; this.grade=random.nextInt(41)+60 ; } public String getSno(){ return sno; } public String getSname(){ return sname; } public int getGrade(){ return grade; } public void setGrade(int grade){ this.grade=grade; } public void print() { System.out.println("学号:" + sno + " 姓名:" + sname + " 成绩:" + grade); } } public class test { public static void main(String[] args) { StudentManager ArrayList = new StudentManager(); ArrayList.add(new student("001","崔朗")); ArrayList.add(new student("002","翠兰")); ArrayList.add(new student("003","小兴奋")); ArrayList.add(new student("004","小情绪")); 实验内容及步骤: ArrayList.add(new student("005","小love")); ArrayList.add(new student("006","青柠")); ArrayList.list(); System.out.println("---修改第2个学生成绩为98---"); ArrayList.update(2,98); } } (2)运行结果:
(1)代码: import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; import java.math.RoundingMode; public class TestBig { public static void main(String[] args) { BigInteger bi1 = new BigInteger("123456789"); BigInteger bi2 = new BigInteger("987654321"); System.out.println("bi2与bi1之间的较大值: " + bi2.max(bi1)); System.out.println("bi2与bi1之间的较小值: " + bi2.min(bi1)); BigInteger bigInteger1 = BigInteger.valueOf(100); BigDecimal bd1 = new BigDecimal("0.001"); // 创建BigDecimal对象 BigDecimal bd2 = BigDecimal.valueOf(0.009);// 创建BigDecimal对象 实验内容及步骤: System.out.println("bd2与bd1的和: " + bd2.add(bd1)); System.out.println("bd2与bd1的差: " + bd2.subtract(bd1)); System.out.println("bd2与bd1的积: " + bd2.multiply(bd1)); System.out.println("bd2与bd1的商: " + bd2.divide(bd1)); System.out.println("bd2与bd1之间的较大值: " + bd2.max(bd1)); System.out.println("bd2与bd1之间的较小值: " + bd2.min(bd1)); BigDecimal bigDecimal = new BigDecimal("3.1415926"); BigDecimal bigDecimal1 = bigDecimal.setScale(0, BigDecimal.ROUND_HALF_UP); System.out.println(bigDecimal1); System.out.println(bigDecimal.divideToIntegralValue(new BigDecimal("100"))); BigDecimal b1 = new BigDecimal("4.2585"); MathContext m = new MathContext(3, RoundingMode.CEILING);//4 precision BigDecimal b2 = b1.round(m); System.out.println(b1); System.out.println(b2); } } (2)运行结果: | |
你的问题: 1.在创建 ArrayList 和 HashMap 时,可以使用泛型指定集合存储的元素类型。在使用集合元素时,存在类型不正确的问题。 |
《Java程序设计》实验报告(三)之常用类和集合类
于 2024-02-07 11:06:19 首次发布