java基础

package com;
import java.util.Arrays;

public class hk {
    public static void main(String args[]) {


        //升序排列
        int[] ns = {28, 12, 89, 73, 65, 18, 96, 50, 8, 36};

        for (int i = 0; i < ns.length; i++) {
            for (int j = 0; j < ns.length - 1; j++) {
            // 修改为> 降序排列
                if (ns[j] < ns[j + 1]) { 

                    int res = ns[j];
                    ns[j] = ns[j + 1];
                    ns[j + 1] = res;

                }


            }
        }
        System.out.println(Arrays.toString(ns));

    }


}


#多维数组求平均值
public class Main {
    public static void main(String[] args) {
        // 用二维数组表示的学生成绩:
        int[][] scores = {
                { 82, 90, 91 },
                { 68, 72, 64 },
                { 95, 91, 89 },
                { 67, 52, 60 },
                { 79, 81, 85 },
        };
        // TODO:
         double average;
         int count=0;
         double sum=0;
        for(int[] score:scores){
           for(int s:score){
                sum=sum+s;
                count+=1;
               

             }

     }
        average= sum/count;
        System.out.println(average);

        if (Math.abs(average - 77.733333) < 0.000001) {
            System.out.println("测试成功");
        } else {
            System.out.println("测试失败");
        }
    }
}


请使用StringJoiner 构造一个INSERT语句:


import java.util.StringJoiner;

public class Main {
    public static void main(String[] args) {
        String[] fields = { "name", "position", "salary" };
        String table = "employee";
        String select = buildSelectSql(table, fields);
        System.out.println(select);
        System.out.println("SELECT name, position, salary FROM employee".equals(select) ? "测试成功" : "测试失败");
    }
    static String buildSelectSql(String table, String[] fields) {
        // TODO:
       String str=" FROM "+table;
       StringJoiner s=new StringJoiner(", ","SELECT ",str);
       for(String field:fields){
          s.add(field);
         }
       
       return s.toString();
    }

}


请使用StringBuilder构造一个INSERT语句:

public class Main {
    public static void main(String[] args) {
        String[] fields = { "name", "position", "salary" };
        String table = "employee";
        String insert = buildInsertSql(table, fields);
        System.out.println(insert);
        String s = "INSERT INTO employee (name, position, salary) VALUES (?, ?, ?)";
        System.out.println(s.equals(insert) ? "测试成功" : "测试失败");
    }
    static String buildInsertSql(String table, String[] fields) {
        // TODO:
        String str="INSERT INTO "+table+" (";
        StringBuilder  s=new StringBuilder(1024);
         s.append(fields[0]);
         s.append(", ");
         s.append(fields[1]);
         s.append(", ");
         s.append(fields[2]);

        String f=str+s.toString()+") VALUES (?, ?, ?)";
        return f;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值