[2017.11.04]作业08

1:需求:请设计一个方法,可以实现获取任意范围内的随机数。

public class Homework {  
    public static void main(String[] args) {  
        Scanner sc = new Scanner(System.in);  

        System.out.println("请输入开始范围:");  
        int start = sc.nextInt();  
        System.out.println("请输入结束范围:");  
        int end = sc.nextInt();  

        for (int x = 0; x < 100; x++) {  
            int result = getRandom(start, end); 
            System.out.println("result:" + result);  
        }  
    }  


    public static int getRandom(int start, int end) {  
        return (int) (Math.random()*(end-start+1)) + start;  
    }  
}  

2:下面代码执行的结果是:

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

3:下面代码执行的结果是:

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

4:下面代码执行的结果是

    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

5:下面代码能最后打印的值是?

   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

A

编程题
1:集合的嵌套遍历
需求:
我们班有学生,每一个学生是不是一个对象。所以我们可以使用一个集合表示我们班级的学生。ArrayList
但是呢,我们旁边是不是还有班级,每个班级是不是也是一个ArrayList。
而我现在有多个ArrayList。也要用集合存储,怎么办呢?

嵌套存储集合,即ArrayList<ArrayList<Student>>

2:获取10个1-20之间的随机数,要求不能重复

public class Homework {  
        public static void main(String[] args) {  
            Random random = new Random();  
            int count = 0;  
            List list = new List();  

            while(count<10){ 
                int randomNumber = random.nextInt(20)+1;  
                if(!list.contains(randomNumber)){  
                    list.add(randomNumber);  
                    count++;  
                }  
            }  

            //输出
            for(int i=0;i<list.size();i++){  
                System.out.println(list.get(i));  
            }  
        }  
}

3:使用ArrayList集合存储自定义对象并遍历(三种方式去实现)

import java.util.ArrayList;
import java.util.Iterator;


public class ArrayList {
    public static void main(String[] args) {

        ArrayList array = new ArrayList();

        Student s1 = new Student("王", 20);
        Student s2 = new Student("张", 21);
        Student s3 = new Student("刘", 22);

        array.add(s1);
        array.add(s2);
        array.add(s3);

        //遍历
        Iterator it = array.iterator();

        while(it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()  + s.getAge());
        }

        for (int x = 0; x < array.size(); x++) {
            Student s = array.get(x);
            System.out.println(s.getName() + s.getAge());
        }
    }
}
public class Student {

    private String name;
    private int age;

    public Student() {
        super();
    }

    public Student(String name, int age) {
        super();
        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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值