java基础练习题
程序员文章站
2024-03-05 14:26:30
...
1.在数组里面第一个元素和最后一个元素互换位置
//交换之前
int[] score = {12, 42, 32, 1, 34, 53, 10};
int temp = score[0];
score[0] = score[score.length - 1];
score[score.length - 1] = temp;
//交换之后
for(int i = 0; i < score.length; i++){
System.out.print(score[i] + "\t");
}
1.创建一个数组用来存储1-100之间的奇数
public static void main(String[] args) {
//1-100之间的奇数
int[] num = new int[50]; //奇数
int a = 0;
for (int j = 0; j < 100; j++) {
if (j % 2 == 1) {
num[a] = j;
a++;
}
}
System.out.println(Arrays.toString(num));
}
2.给定两个数组a{1, 2, 3}b{4, 5, 6}将两个数组内的元素对应位置互换
public static void main(String[] args) {
int[] a = {1, 2, 3};
int[] b = {4, 5, 6};
int i;
for (i = 0; i < 2; i++) {
int temp = 0;
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
}
3.给定一-个数组a{1, 2, 3, 4, 5,6}将数组中元素头尾互换位置
public static void main(String[] args) {
// 交换之前
int[] a = {1, 2, 3, 4, 5, 6};
int temp = a[0];
a[0] = a[a.length - 1];
a[a.length - 1] = temp;
// 数组首尾交换
System.out.println(Arrays.toString(a));
}
4.计算数组中所有元素的平均值
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5, 6};
int count = 0; //初始化
double average = 0.0;
for (int i = 0; i < a.length; i++) {
count += a[i];
average = count / a.length;
}
System.out.println("数组中所有元素的平均值:" + average);
}
5.合并两个数组
public static void main(String[] args) {
int[] a = {1, 2};
int[] b = new int[a.length];
System.arraycopy(a, 0, b, 0, a.length);
System.out.println(Arrays.toString(b));
//=======================================================
// 通过API方法
int[] i = {1, 2};
int[] n = {10, 12, 13, 14};
int[] s = new int[i.length + n.length];
System.arraycopy(i, 0, s, 0, i.length);
System.arraycopy(n, 0, s, i.length, n.length);
System.out.println(Arrays.toString(s));
}
6.按照数组中最大值,从最大值位置拆分成两个数组
public static void main(String[] args) {
int max = 0;
int[] array = {2, 1, 3, 9, 5, 6, 7, 0};
int i; //数组索引计数器
int a = 0; //计数下标
for (i = 0; i < array.length; i++) {
// 判断最大值
if (array[i] > max) {
max = array[i];
a = i;
}
}
//存放下标的数组
int[] aa = new int[a];
System.out.println(a);
System.out.println(max);
//存放最大数左边的数据的数组
int[] left = new int[a];
System.arraycopy(array, 0, left, 0, a);
System.out.println(Arrays.toString(left));
//存放数组最大值右边的数据的数组
int[] right = new int[array.length - a];
System.arraycopy(array, a, right, 0, array.length - a);
System.out.println(Arrays.toString(right));
}
7.去掉数组中的某一个元素, 如数组中所有为0的数组,或在1到3之间的数组
public static void main(String[] args) {
// 定义一个数组
int[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//新建一个数组
int[] b = new int[a.length];
int c = 0;
for (int i = 0; i < a.length; i++) {
if (i > 3) {
b[c] = i;
c++;
}
}
System.out.println(Arrays.toString(b));
}
8.创建一一个数组存储1-100之间的质数
public class demo08 {
// //判断能否被整除,质数是只能被1和自身整除
public static boolean Prime(int num) {
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int count = 0; //初始化
//判断是否是质数
for (int j = 2; j <= 100; j++) {
if (Prime(j)) {
System.out.print(j + " ");
count++;
}
}
}
}
九九乘法表
public static void main(String[] args) {
// 乘法表
int row; //定义行
int rol;//定义列
int count;
for(row=1;row<=9;row++){
for (rol=1;rol<=row;rol++){
count=row*rol;
System.out.print(rol+"*"+row+"="+count+" ");
}
System.out.println();
}
}
等边三角形
public static void main(String[] args) {
//外层循环控制行
for (int i = 1; i < 5; i++) {
// 打印空格
for (int k = 3; k >= i; k--) {
System.out.print(" ");
}
// 内层循环控制**
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
等边三角形
public static void main(String[] args) {
//dandan();
drawNo();
}
private static void drawNo() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4 - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i + 1; k++) {
System.out.print(k);
}
for (int k = i; k >= 1; k--) {
System.out.print(k);
}
System.out.println();
}
}
文件的加密,解密操作
public class FileEncAndDec1 {
private static final int numOfEncAndDec = 0x99; //加密解密秘钥
private static int dataOfFile = 0; //文件字节内容
public static void main(String[] args) {
File srcFile = new File("桌面.jpg"); //初始文件
File encFile = new File("encFile.tif"); //加密文件
File decFile = new File("decFile.bmp"); //解密文件
try {
EncFile(srcFile, encFile); //加密操作
} catch (Exception e) {
e.printStackTrace();
}
}
//加密操作
private static void EncFile(File srcFile, File encFile) throws Exception {
if (!srcFile.exists()) {
System.out.println("source file not exixt");
return;
}
if (!encFile.exists()) {
System.out.println("encrypt file created");
encFile.createNewFile();
}
InputStream fis = new FileInputStream(srcFile);
OutputStream fos = new FileOutputStream(encFile);
while ((dataOfFile = fis.read()) > -1) {
fos.write(dataOfFile ^ numOfEncAndDec);
}
fis.close();
fos.flush();
fos.close();
}
//解密操作
private static void DecFile(File encFile, File decFile) throws Exception {
if (!encFile.exists()) {
System.out.println("encrypt file not exixt");
return;
}
if (!decFile.exists()) {
System.out.println("decrypt file created");
decFile.createNewFile();
}
InputStream fis = new FileInputStream(encFile);
OutputStream fos = new FileOutputStream(decFile);
while ((dataOfFile = fis.read()) > -1) {
fos.write(dataOfFile ^ numOfEncAndDec);
}
fis.close();
fos.flush();
fos.close();
}
}
效果图:
//别人的代码
public class FlieEncAndDec {
private final int numEncAndDec = 0x99;//加密解密秘钥
private int dataOfFile = 0; //文件字节内容
/**
* 加密
*/
@Test
public void EncFile() {
//创建初始文件路径
File srcFile = new File("E:" + File.separator + "2.jpg");
//创建加密文件路径
File encFile = new File("E:" + File.separator + "3.jpg");
try {
if (!srcFile.exists()) {
System.out.println("source file not exist");
return;
}
if (!encFile.exists()) {
encFile.createNewFile();
}
InputStream fis = new FileInputStream(srcFile);
OutputStream fos = new FileOutputStream(encFile);
while ((dataOfFile = fis.read()) > -1) {
fos.write(dataOfFile ^ numEncAndDec);
}
fis.close();
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
/**
* 解密
*/
@Test
public void DecFile() {
// 创建源文件路径
File encFile = new File("E:" + File.separator + "3.jpg");
// 创建目标路径
File decFile = new File("E:" + File.separator + "4.jpg");
if (!encFile.exists()) {
System.out.println("encFile file not exist");
return;
}
if (!decFile.exists()) {
try {
decFile.createNewFile();
//创建输入流,输出流
InputStream fis = new FileInputStream(encFile);
OutputStream fos = new FileOutputStream(decFile);
while ((dataOfFile = fis.read()) > -1) {
fos.write(dataOfFile ^ numEncAndDec);
}
fis.close();
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
模拟换座位
public static void main(String[] args) {
int[][] array = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
Scanner scanner = new Scanner(System.in);
System.out.println("查看第几周的座位:");
int index = scanner.nextInt();
// int[][] temp = new int[array.length][4];
// for (int i = 0; i < 4; i++) {
//// temp[][]=array[0][0];
//// System.out.println(array[index][i]);
// }
//控制周数
for (int i = 0; i < index - 1; i++) {
// 二维数组长度 array[0].length
int[] temp = new int[array[0].length];
// 一维数组的长度 array.length
for (int j = 0; j < array.length; j++) {
//当一维数组的下标等于0时,取出第一个一维数组
if (j == 0) {
temp = array[0];
}
if (j < array.length - 1) {
array[j] = array[j + 1];
} else {
//当循环到最后一个数组的下标的时候,把第一个一维数组赋值给最后一个数组下标
array[j] = temp;
}
//当循环到最后一个数组的时候,打印输出
if (j == array.length - 1) {
for (int[] ints : array) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println("----------------------------------------");
}
}
}
}
}
遍历不规则二维数组
public class array01 {
public static void main(String[] args) {
int[][] a = {{1, 2}, {1, 3, 4}, {2, 3, 4, 5}};
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
数组中的最大值,最小值
public class array02 {
public static void main(String[] args) {
//数组中元素与默认的比较,如果比默认的大,就用数组元素作为默认值在进行比较
//定义一个数组
int[] array = {2, 1, 3, 19, 5, 6, 7};
//默认第一个为最大值
int max = array[0];
//默认第一个为最小值
int min = array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
} else if (array[i] < min) {
min = array[i];
}
}
System.out.println("最大值为:" + max);
System.out.println("最小值为:" + min);
}
}