第2章 基本数据类型与数组

例子1

public class Example2_1 {
    public static void main(String[] args) {
        char chinaWord = '好',japanWord = 'あ';
        char you = '\u4F60';
        int position = 20320;
        System.out.println("汉字:"+ chinaWord+"的位置:"+(int)chinaWord);
        System.out.println("日文" + japanWord +"的位置:"+(int)japanWord);
        System.out.println(position+"位置上的字符是:"+(char)position);
        position = 21319;
        System.out.println(position+"位置上的字符是:"+(char)position);
        System.out.println("you:" +you);
    }
}

运行结果为:在这里插入图片描述

例子2

public class Example2_2 {
    public static void main(String[] args) {
        byte b = 22;
        int n = 129;
        float f = 123456.6789f;
        double d = 123456789.123456789;
        System.out.println("b = " + b);
        System.out.println("n = " + n);
        System.out.println("f = " + f);
        System.out.println("d = " + d);
        b = (byte) n;   //导致精度的损失
        f = (float) d;  //导致精度的损失
        System.out.println("b = " + b);
        System.out.println("f = " + f);
    }
}

运行结果为:在这里插入图片描述

例子3

import java.util.Scanner;

public class Example2_3 {
    public static void main(String[] args) {
        System.out.println("请输入若干个数,每输入一个数回车确认");
        System.out.println("最后输入数字0结束输入操作");
        Scanner reader = new Scanner(System.in);
        double sum = 0;
        double x = reader.nextDouble();
        while(x != 0){
            sum = sum + x;
            x = reader.nextDouble();
        }
        System.out.println("sum = " + sum);
    }
}

运行结果为:在这里插入图片描述

例子4

public class Example2_4 {
    public static void main(String[] args) {
        int a[] = {1,2,3,4};
        int b[] = {100,200,300};
        System.out.println("数组a的元素个数 = " + a.length);
        System.out.println("数组b的元素个数 = " + b.length);
        System.out.println("数组a的引用 = " + a);
        System.out.println("数组b的引用 = " + b);
        a = b;
        System.out.println("数组a的元素个数 = " + a.length);
        System.out.println("数组b的元素个数 = " + b.length);
        System.out.println("a[0] = " + a[0] + "a[1] = " + a[1] + "a[2] = " + a[2]);
        System.out.println("b[0] = " + b[0] + "b[1] = " + b[1] + "b[2] = " + b[2]);
    }
}

运行结果为:在这里插入图片描述

例子5

import java.util.Scanner;

public class Example2_5 {
    public static void main(String[] args) {
        int start = 0,end,middle;
        int a[] = {12,45,67,89,123,-45,67};
        int N = a.length;
        for(int i = 0 ; i < N ; i++){
            for(int j = i + 1 ; j < N ; j++){
                if(a[j] < a[i]){
                    int t = a[j];
                    a[j] = a[i];
                    a[i] = t;
                }
            }
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入整数,程序判断该整数是否在数组中:");
        int number = scanner.nextInt();
        int count = 0;
        end = N;
        middle = (start + end) / 2;
        while(number != a[middle]){
            if(number > a[middle]){
                start = middle;
            }
            else if(number < a[middle]){
                end = middle;
            }
            middle = (start + end) / 2;
            count++;
            if(count > N/2){
                break;
            }
        }
        if(count > N / 2){
            System.out.printf("%d不在数组中.\n",number);
        }
        else{
            System.out.printf("%d在数组中.\n",number);
        }
    }
}

运行结果为:
加粗样式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值