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 },
};
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) {
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) {
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