java面试题

逻辑计算类

1、求1到100的质数

/**
 * Created by Administrator on 2018/8/25.
 */
public class JavaMach {
    /* 求1到100 的质数.
    * 质数: 是1和本身能整除为0的才算, 质数*/
    public static void PrimeNumbers(){
        int i, j, k;
        for (i = 2; i < 100; i++) {
            // Math.sqrt(i) 是对i 开方操作,如果i=2 那它的开方根为 1.14 取整为1
            k = (int) Math.sqrt(i);
            System.out.println(" k的平方: "+k+"  i: "+i);
            for (j = 2; j <= k; j++) {
                if (i % j == 0)
                    break;

            }
            if (j > k) {
                System.out.println(i + " ");
            }
        }

    }

    public static void shaixuanfa(){
        int[] a = new int[101];
        int i, j = 2;

        while (j < 101) {
            if (a[j] == 0) {
                for (i = j + 1; i < 101; i++) {
                    //这里对数据进行帅选 从2开始帅选
                    if (i % j == 0) {
                        a[i] = 1;
                    }
                }
            }
            j++;
        }
        for (int k = 0; k < 101; k++) {
            if (k >= 2 && a[k] == 0) {
                System.out.println(k);
            }
        }
    }

    public static void main(String[] args) {
        shaixuanfa();
//      PrimeNumbers();
//      testMath();
    }
}

 

java 值传递与引用传递

总结: 基本数据类型是值传递

1,对象的引用传递并且在调用里面改变对象的值 将会是值传递

package com.sn.framework.test;

/**
 * Created by Administrator on 2018/8/25.
 */
public class ParamTest {

    public static void main(String[] args) {

        //基础数据类型是值传递
//        int price = 5;
//        doubleValue(price);
//        System.out.print(price);
          //对对象的引用设值 是值传递
//        Student stu = new Student(80);
//        raiseScore(stu);
//        System.out.print(stu.getScore());

        //对对象的值调用是 值传递
        Student a = new Student(0);
        Student b = new Student(100);

        System.out.println("交换前:");
        System.out.println("a的分数:" + a.getScore() + "--- b的分数:" + b.getScore());

        swap(a, b);

        System.out.println("交换后:");
        System.out.println("a的分数:" + a.getScore() + "--- b的分数:" + b.getScore());
    }

    public static void doubleValue(int x) {
        x = 2 * x;
    }
    //对对象的引用作为参数 是值传递
    public static void raiseScore(Student s) {
        s.setScore(s.getScore() + 10);
    }
    //对对象的值调用是值传递
    public static void swap(Student x, Student y) {
        Student temp = x;
        x = y;
        x.setScore(y.getScore());
        y = temp;
        y.setScore(temp.getScore());
        System.out.println("  "+x.getScore()+"  y: "+y.getScore());
        temp.setScore(111);
    }
}

class Student {

    private float score;

    public Student(float score) {
        this.score = score;
    }

    public void setScore(float score) {
        this.score = score;
    }

    public float getScore() {
        return score;
    }


}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值