【Java_SE】作业练习1104

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

import java.util.Random;
import java.util.Scanner;

public class Demo1 {

    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 result=getRandom(start,end);
        System.out.println(result);
    }

    //产生随机数方法
    public static int getRandom(int start,int end){
        Random r=new Random();
        int random=0;
        //如果不在设定的上下限中,则继续产生随机数
        while(!(random>=start)){
            random=r.nextInt(end);
        }
        return random;
    }
}

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

public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        System.out.print(s1 == s2);//结果:false
        System.out.print(",");
        System.out.println(s1.equals(s2));//结果: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);//结果:AB,B
    }
    static void operate(StringBuffer x, StringBuffer y) {
        x.append(y);
        y = x;
    }
}

4.下列代码的执行结果是:

public static void main(String [] args){
    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);//结果:str1 == This is a test!Hi
    System.out.println("str2 == " + str2);//结果:str2 == This is a test!Hi
}

5:下面代码能最后打印的值是?   答案:C)0

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

你可能感兴趣的:(作业及练习)