数组
- 先声明
- 申请空间
- 赋值
一维数组
方式1:int arr[ ] = { 1,2,3,4,5,6};
方式2:int arr1[ ] = new int[ 6 ];
arr[ 0 ] = 1;
arr[ 1 ] = 2;
arr[ 2 ] = 3;
arr[ 3 ] = 4;
arr[ 4 ] = 5;
arr[ 5 ] = 6;
方式3:
int arr2[ ] = new int [ ]{ 1,2,3,4,5,6};
二维数组
//1声明方式 中括号中不能指定个数
int[][] arr1;
int[] arr2[];
int arr3[][];
//2给数组赋值(初始化)
arr1 = new int[3][4];
arr2 = new int[4][];//第二个括号可以不指定个数
arr3 = new int[][4];//错误。
//给二维数组arr3申请空间(特殊情况)
arr3 = new int[3][];
arr3[0] = new int[5];
arr3[1] = new int[6];
arr3[2] = new int[4];
//赋值
arr1[0][0] = 34;
arr3[0][0] = 3;
arr3[0][1] = 4;
arr3[0][2] = 5;
//二维数组的遍历
//方法1
for (int i = 0; i < arr3.length; i++) {
for (int j = 0; j < arr3[i].length; j++) {
System.out.print(arr3[i][j]+" ");
}
System.out.println();
}
System.out.println("************");
//方法2:
//遍历二维数组中的每一个一维数组
for (int[] arr : arr3) {
//遍历一维数组中的每一个元素
for (int cell : arr) {
System.out.print(cell+" ");
}
System.out.println();
}
运行结果:
数组排序方法
//1Eclipse里自带排序的方法
Arrays.sort(arr[i]);
Arrays.parallelSort(arr[i]);
int[] arr2 = {4,5,6,1,6,7,8,13,15};
//2将arr2的前n个元素赋给arr3
int[] arr3 = Arrays.copyOf(arr2, n);
//3选择性的赋值(将arr2的第m个元素到n-1个元素赋给arr3)
//不包含to(第n个元素)位置
arr3 = Arrays.copyOfRange(arr2, m, n);
System.out.println(Arrays.toString(arr3));
//4填充(如果arr3本来就有元素,填充后新数组元素代替了前面的元素)
int[] arr3 = {4,5,6,1,6,7,8,13,15};
Arrays.fill(arr3, 888);
//5将arr3的第m个元素到n-1个元素之间的元素被新赋值为999;
Arrays.fill(arr3, m, n, 999);
//6判断两个数组的所有元素是否相等。返回值为True和False;
Arrays.equals(arr5, arr6)
//7判断字符串数组arr[i]的元素是否等于abc
"abc".equals(arr[i])
字符串拼接操作
//将字符串中的每个字符取出放入到一个字符数组中
String str=“abcdefghjkl”;
//toCharArray()方法将此字符串转换为一个新的字符数组。
char[] cs = str.toCharArray();
//定义一个字符串拼接的操作的类
StringBuffer sBuffer = new StringBuffer();
for(char c :cs) {//c为每次循环的变量,cs为每个字符
char c2 = (char)(c^MY);
sBuffer.append(c2);
}
number = input.nextInt();
int newnumber = number%7;
6. 2
22
222
2222打印这种数字:代码如下; p=p*10+2;
7. 输入一个字符串原码转密文,又转原文的题型。
package question1;
import java.nio.charset.Charset;
public class Mingmiwenzhuanhuan_question1 {
//key0是一种秘钥
private static final String key0 = "FECOI()*&<MNCXZPKL";
private static final Charset charset = Charset.forName("UTF-8");
//getBytes()方法使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
private static byte[] keyBytes = key0.getBytes(charset);
public static String encrypt(String enc) {
byte[] b = enc.getBytes(charset);
for (int i = 0, size = b.length; i < size; i++) {
for (byte keyBytes0 : keyBytes) {
b[i] = (byte) (b[i] ^ keyBytes0);
}
}
return new String(b);
}
public static String decode(String dec) {
byte[] e = dec.getBytes(charset);
byte[] dee = e;
for (int i = 0, size = e.length; i < size; i++) {
for (byte keyBytes0 : keyBytes) {
e[i] = (byte) (dee[i] ^ keyBytes0);
}
}
return new String(e);
}
public static void main(String[] args) {
String s = "you are right";
String enc = encrypt(s);
String dec = decode(enc);
System.out.println("加密后:" + enc);
System.out.println("解密后:" + dec);
}
}
8.一个年份有多少天
package lianxi1;
import java.util.Scanner;
public class Calc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean ist = true;
while (ist) {
System.out.println("请输入一个年份");
int year = input.nextInt();
System.out.println("请输入一年中的十二个月份中的任意一个月份!");
int month = input.nextInt();
while (month <= 0 || month > 12) {
System.out.println("你输入的月份有误,请重新输入!");
month = input.nextInt();
}
int day=0 ;
boolean isTrue = true;
while (isTrue) {
System.out.println("请输入一个日子");
day = input.nextInt();
if (month == 2 && month % 4 == 0 && month % 100 != 0) {
if (day <= 0 || day > 29) {
System.out.println("错误1:你输入月份对应的天数有误,请重新输入!");
day = input.nextInt();
}else {
break;
}
}
if (month == 2 && month % 4 != 0 && month % 100 == 0) {
if (day <= 0 || day > 28) {
System.out.println("错误2:你输入月份对应的天数有误,请重新输入!");
day = input.nextInt();
}else {
break;
}
}
if (month == 1 || month == 3 || month == 5 || month == 3 || month == 7 || month == 8 || month == 10
|| month == 12) {
if (day < 0 || day > 31) {
System.out.println("错误3:你输入月份对应的天数有误,请重新输入!");
day = input.nextInt();
}else {
break;
}
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day <= 0 || day > 30) {
System.out.println("错误4:你输入月份对应的天数有误,请重新输入!");
day = input.nextInt();
}else {
break;
}
}
isTrue = false;
}
int sumday = 0;
switch (month - 1) {
// 1 3 5 7 8 10 12 为大月
// 4 6 9 11 为小月
case 12:
sumday += day;
case 11:
sumday += 30;
case 10:
sumday += 31;
case 9:
sumday += 30;
case 8:
sumday += 31;
case 7:
sumday += 31;
case 6:
sumday += 30;
case 5:
sumday += 31;
case 4:
sumday += 30;
case 3:
sumday += 31;
case 2:
sumday += 28;
case 1:
sumday += 31;
}
if (month % 4 == 0 && month % 100 != 0) {
System.out.println(month + "月"+day+"日"+ "是一年的第" + (sumday + day) + "天");
}else if(month<2) {
System.out.println(month + "月"+day+"日"+ "是一年的第" + (sumday + day) + "天");
}
else {
System.out.println(month + "月"+day+"日"+ "是一年的第" + (sumday + day + 1) + "天");
}
}
}
}
9.1 9*9乘法表
package progect1_2;
public class Multiply1 {
public static void main(String[] args) {
for(int i=1;i<=9;i++) {
for(int j = 1;j<=i;j++) {
System.out.print(i+"*"+j+"= "+(i*j)+"\t\t");
}
System.out.println();
}
}
}
9.2 9*9乘法表
package progect1_2;
public class Multiply2 {
public static void main(String args[]) {
for (int i = 1, j = 1; i <= 9; j++) {
System.out.print( i + "*" + j + "=" + i * j+"\t");
if (i == j) {
//改变j的循环条件
j = 0;
i++;
System.out.println();
}
}
}
}
打印杨辉三角
package progect1_1;
public class Yanghui {
public static void main(String[] args) {
int arr[][] = new int[7][];
for (int i = 0; i < arr.length; i++) {//控制行数
arr[i] = new int [i+1];
for (int j = 0; j < arr[i].length; j++) {//控制列数
if (j == 0 || j == i ) {
arr[i][j] = 1;
}else {
arr[i][j] = arr[i-1][j]+arr[i-1][j-1];
}
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}