WEEK7 JAVA

本文提供多个Java编程实例,包括生成指定范围内的随机数、字符串比较、集合操作等,通过具体代码展示不同场景下的解决方案。

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

import java.util.Scanner;

public class Homework1 {

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();

int num=getRandom(start,end);

System.out.println(num);

}

public static int getRandom(int start,int end ){

int number= (int )(Math.random()*(end-start+1))+start;

return number;

}

}

 

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

 

6、下列代码的执行结果是:

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

 

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

    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)编译错误 Bnull C0         D1

Ccccccc

 

 

编程题

1:集合的嵌套遍历

  需求:

   我们班有学生,每一个学生是不是一个对象。所以我们可以使用一个集合表示我们班级的学生。ArrayList<Student>

   但是呢,我们旁边是不是还有班级,每个班级是不是也是一个ArrayList<Student>

  而我现在有多个ArrayList<Student>。也要用集合存储,怎么办呢?

import java.util.AbstractList;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

 

public class Homework3 {

static ArrayList<ArrayList<Student>> bigList = new ArrayList<ArrayList<Student>>();

public static void main(String[] args) {

ArrayList<Student> list1 = new ArrayList<Student>();

Student a = new Student("rng",666);

Student b = new Student("edg",233);

list1.add(a);

list1.add(b);

bigList.add(list1);

 

ArrayList<Student> list2 = new ArrayList<Student>();

 

Student c= new Student("rng2",667);

Student d = new Student("edg3",234);

list2.add(c);

list2.add(d);

bigList.add(list2);

Iterator<ArrayList<Student>> it = bigList.iterator();

int num = 1;

while(it.hasNext()){

    System.out.println("班级" + num);

    Iterator<Student> itt = it.next().iterator();

    while(itt.hasNext()){

        Student s = itt.next();

        s.show();

    }

    num++;

}

}

}

 

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

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

 

public class Homework4{

 

public static void main(String[] args) {  

           Random random = new Random();  

           int count = 0;  

           List list = new ArrayList();

           Integer it = new Integer(0);

                   list.add(it);

           while(count<10){  

                 int num = random.nextInt(20)+1;  

                if(!list.contains(num)){  

                    list.add(num);  

                    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 Homework5 {

public static void main(String[] args) {

ArrayList a = new ArrayList();

Student s1 = new Student("RNG",666);  

    Student s2 = new Student("EDG",233);  

      a.add(s1);

      a.add(s2);

      //方法一迭代

      Iterator it = a .iterator();

      while(it.hasNext()){

       Student s =(Student)it.next();

       System.out.println(s.getName()+s.getName());

      }

      //方法二 for遍历

      for(int x =0;x<a.size();x++){

       String s =(String)a.get(x);

       System.out.println(s.getName1()+s.getAge());

      }

}

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值