2017.11.4

来源:互联网 发布:看门狗pc优化补丁 编辑:程序博客网 时间:2024/06/05 07:18

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

package aa;import java.util.Random;import java.util.Scanner;/** *  * @author naughtymonkey * */public class Test1 {    public static void main(String[] args) {        System.out.println("请输入取值的范围:max   min");        Scanner scan=new Scanner(System.in);        int max=scan.nextInt();        int min=scan.nextInt();        int random=random(max,min);        System.out.println(random);    }    /**     *      * @param max       随机数的最大取值     * @param min       随机数的最小取值     * @return          范围内的随机数     */    public static int random(int max,int min){        if(max<=min){            return -1;        }else{            return new Random().nextInt(max)%(max-min+1)+min;        }    }}

这里写图片描述

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)编译错误  B)null  C)0         D)1选 C
原创粉丝点击