【Java作业】2017.11.04 / 11.05

来源:互联网 发布:闲鱼 淘宝二手 编辑:程序博客网 时间:2024/05/22 04:44

作业一

/* * 需求:获取任意范围内的随机数 * */import java.util.Scanner;public class RandomDemo2 {public static void main(String[] args) {System.out.print("请输入最小值:");Scanner sc1 = new Scanner(System.in);int min = sc1.nextInt();System.out.print("请输入最大值:");Scanner sc2 = new Scanner(System.in);int max = sc2.nextInt();//产生随机数int randomNumber = (int)(Math.random()*(max-min))+min;//输出System.out.println("随机数:"+randomNumber);}}







作业二

/* * 需求:产生指定范围内的N个随机数(不可重复),并对产生的随机数进行升序排序 * */import java.util.Scanner;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Iterator;public class Test{public static void main(String[] args){System.out.print("请输入最小值:");Scanner sc1 = new Scanner(System.in);int min = sc1.nextInt();System.out.print("请输入最大值:");Scanner sc2 = new Scanner(System.in);int max = sc2.nextInt();System.out.print("请输入要产生的随机数个数:");Scanner sc3 = new Scanner(System.in);int count = sc3.nextInt();//创建List对象List list = new ArrayList();//调用randomNumber()方法list = randomNumber(min,max,count);//使用迭代器进行遍历输出Iterator it = list.iterator();while(it.hasNext()){System.out.println(it.next());}}//工具方法:输出指定范围内的N个不重复随机数public static List randomNumber(int min, int max, int n){  List list = new ArrayList();//进行集合中元素个数判断while(list.size()<n){Integer number = (int)(Math.random()*(max-min))+min;//进行重复元素判断if(!list.contains(number)){list.add(number);}}//进行升序排序Object [] objs = list.toArray();//将集合转为对象型数组Arrays.sort(objs);list = Arrays.asList(objs);//讲数组转回集合return list;}}






作业三

/* * 需求:使用ArrayList储存自定义的对象并遍历(三种方法实现) * */import java.util.List;import java.util.ArrayList;import java.util.ListIterator;public class ErgodicDemo {public static void main(String[] args) {Football f1 = new Football("伊卡尔迪",24);Football f2 = new Football("伊布拉希莫维奇",36);Football f3 = new Football("穆里尼奥",54);Football f4 = new Football("雷科巴",41);//创建List对象,并赋值List list = new ArrayList();list.add(f1);list.add(f2);list.add(f3);list.add(f4);//方式一:数组遍历(toArray()方法)Object [] objs = list.toArray();for(int i=0; i<objs.length ;i++) {System.out.println(objs[i]);}System.out.println("----------------");//方式二:集合遍历(get()方法)for(int i=0; i<list.size() ;i++) {System.out.println(list.get(i));}System.out.println("----------------");//方式三:使用迭代器遍历(listiterator()方法)ListIterator it = list.listIterator();while(it.hasNext()) {System.out.println(it.next());}}}class Football {String name;int age;Football() {}Football(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String toString() {return "["+name+","+age+"]";}}





作业四

/* * 需求:获取10个1-20之间的随机数,不能重复 * */import java.util.ArrayList;import java.util.Collection;public class RandomDemo {public static void main(String[] args) {//创建Collection对象Collection c = new ArrayList();//使用while循环进行元素数量判断while(c.size()<10) {//获取随机数Integer i = (int)(Math.random()*20)+1 ;//进行重复元素判断if(!c.contains(i)) {c.add(i);}}//输出System.out.println("产生的十个随机数为:"+c);}}





作业五

/* * 集合的嵌套遍历 *  * 需求:  我们班有学生,每一个学生是不是一个对象。所以我们可以使用一个集合表示我们班级的学生。ArrayList<Student>  但是呢,我们旁边是不是还有班级,每个班级是不是也是一个ArrayList<Student>。 而我现在有多个ArrayList<Student>。也要用集合存储,怎么办呢? * */import java.util.Collection;import java.util.ArrayList;public class StudentTest {public static void main(String[] args) {//班级1Student s1 = new Student("伊卡尔迪");Student s2 = new Student("佩里西奇");Student s3 = new Student("坎德雷瓦");Collection c1 = new ArrayList();c1.add(s1);c1.add(s2);c1.add(s3);//班级2Student s4 = new Student("穆里尼奥");Student s5 = new Student("斯帕莱蒂");Student s6 = new Student("弗朗西斯科利");Collection c2 = new ArrayList();c2.add(s4);c2.add(s5);c2.add(s6);//年级Collection c = new ArrayList();c.add(c1);c.add(c2);System.out.println(c);}}class Student{String name;Student(){}Student(String name){this.name = name;}public void setName(String name) {this.name = name;}public String getName() {return name;}public String toString() {return name;}}










作业六


1.下面代码执行的结果是:
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.print(s1 == s2);
System.out.print(",");
System.out.println(s1.equals(s2));
}
}
false;

true;

2.下面代码执行的结果是:
public static void main(String arg[]) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate(a, b);
System.out.println(a + "," + b);
}
static void operate(StringBuffer x, StringBuffer y) {
x.append(y);
y = x;
}

AB,B


3.下列代码的执行结果是:
String str1 = "This is a test!";
StringBuffer str2 =new StringBuffer( "This is a test!");
str1 = str1+"Hi";
str2.append("Hi");
System.out.println("str1 == " + str1);
System.out.println("str2 == " + str2);


str1 == This is a test!Hi
str2 == This is a test!Hi



4.下面代码能最后打印的值是?
    public class TestValue {
private static int a;


public static void main(String[] args) {
modify(a);
System.out.println(a);
}


public static void modify(int a) {
a++;
}


}
A)编译错误 B)null C)0         D)1