1。二维数组的应用,键盘输入9 个double类型数据,将数据转为int型。
import java.util.Scanner;
public class Demo07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a[][]= new double [3][3];
System.out.println("输入9个数");
for(int i =1;i<=3;i++)
{
for(int j =1;j<=3;j++)
{
a[i-1][j-1] = sc.nextDouble();
}
}
System.out.println("这些数为:");
for(double[] b:a)
{
for(double c:b)
System.out.print(c+" ");
}
System.out.println();
int b[][]= new int[3][3];
for(int i = 1;i<=3;i++)
{
for(int j =1;j<=3;j++){
b[i-1][j-1] = (int) a[i-1][j-1];
}
}
System.out.println("转为int数组数为:");
for(int[] c:b)
{
for(int d:c)
System.out.print(d+" ");
}
}
}
2.斐波那契数列
1 1 2 3 5 8.。。。。30数字是多少?
public class Demo01 {
public static void main(String[] args) {
/*
* 斐波那契数列 1+1+2+3+5+。。。。。
*/
int a[] = new int[30];
a[0] = a[1] = 1;
for (int j = 1; j <= a.length - 2; j++) {
a[j + 1] = a[j] + a[j - 1];
}
System.out.println(a[29]);
for (int b : a) {
System.out.print(b + " ");
}
}
}
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
public class Demo08 {
// 五子棋。。1024的逻辑
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = new int[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 0 || i == j) {
a[i][j] = 1;
} else if ((j < i && j >= 1) && (i >= 2 && i < 10)) {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
} else {
a[i][j] = 0;
}
}
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= i; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
}
}
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
public class Dem09 {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
*
*/
int a[][] = new int[5][5];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0) {
a[i][j] = j + 1;
}
if (i == 1 && j <= 3) {
a[i][j] = j + 16;
}
if ((i == 2) && (j == 1 || j == 2)) {
a[i][j] = j + 23;
}
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 4) {
a[i][j] = 5 + i;
}
if ((j == 3) && (i >= 1 && i <= 3)) {
a[i][j] = 18 + i;
}
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 4)
a[i][j] = 13 - j;
if ((i == 3) && (j == 1 || j == 2 || j == 3)) {
a[i][j] = 24 - j;
}
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if ((j == 0) && (i >= 1))
a[i][j] = 17 - i;
if ((j == 2) & (i == 2))
a[i][j] = 27 - i;
if ((j == 2) & (i == 3))
a[i][j] = 25 - i;
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
}
4.判断输入的是否是邮箱地址
import java.util.Scanner;
public class Demo13 {
/*
* 判断输入的是否是邮箱地址
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("输入邮箱地址:");
String s = sc.next();
// boolean bo =s.indexOf("@")!=-1;
if ((s.contains("@")) && s.endsWith(".com") && (s.indexOf(".") - s.indexOf("@") > 1)
&& s.indexOf("@") > 0) {
System.out.println(s + "为邮箱地址");
} else
System.out.println("邮箱地址输入有误!");
}
}
}
5.首先让计算机随机产生出10个两位正整数,然后按照从小到大的次序显示出来。
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.首先让计算机随机产生出10个两位正整数,然后按照从小到大的次序显示出来。
int a[] = new int[10];
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.println("输入十个数:");
for (int i = 0; i < a.length; i++) {
a[i]= r.nextInt(90)+10;
}
Arrays.sort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
for (int i = 0; i < a.length; i++) {
for(int j = 0;j< a.length-i-1;j++)
{
if(a[j]>a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
System.out.println("排序后:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
}
6.从键盘上输入4行4列的一个实数矩阵到一个二维数组中,然后求出主对角线上元素之乘积以及副对角线上元素之乘积。
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args) {
System.out.println("输入4*4的数组");
int b [][] = new int [4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
b[i][j]= sc.nextInt();
}
}
int ji =1;
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if(i == j){
ji = ji*b[i][j];
}
}
}
System.out.println("主对角线成绩为:"+ji);
int ji2 =1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j <4; j++) {
if(i+j==3)
ji2 = ji2*b[i][j];
}
}
System.out.println("副对角线成绩为:"+ji2);}}
7.输入字符串,大写到小写的转换,输出前后字符调换的结果。
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args) {
String s = sc.next();
String s1 = s.toLowerCase();
//System.out.println("大写变小写:"+s1);
String s2 = s1.toUpperCase();
//System.out.println("小写变大写:"+s2);
char[] c = s.toCharArray();
for (int i = 0; i < c.length/2; i++) {
int oldindex = i;
int newindex = c.length-1-i;
char temp = c[oldindex];
c[oldindex] = c[newindex];
c[newindex] = temp;
}
System.out.println("交换后的字符串");
String str =new String(c);
System.out.println(c);
}
}