经过五天的学习,倍感充实,也学到了很多知识,经过一天的努力编写了一个小程序,将五天学习到的知识全部应用到了其中,演示效果如下:
代码编写如下;
import java.util.*;
class TestZongHe {
public static void main(String[] args) {
boolean flag = true;
while (flag) {
Scanner sc = new Scanner(System.in);
System.out.println("请选择你需要的操作:");
System.out.println("---------------------------");
System.out.println("1:for循环。");
System.out.println("2:数组。");
System.out.println("输入886结束程序");
int a = sc.nextInt();
switch (a) {
case 1:
System.out.println("请再次选择你需要的操作:");
System.out.println("---------------------------");
System.out.println("1:求1到n的和");
System.out.println("2:求1到n之间的偶数的和");
System.out.println("3:打印水仙花数");
System.out.println("4:统计水仙花数");
System.out.println("5:打印m行n列的矩形");
System.out.println("6:打印高为n的等直角三角形");
System.out.println("7:打印nn乘法表");
System.out.println("8:打印反nn乘法表");
System.out.println("输入886结束程序");
int aa = sc.nextInt();
switch (aa) {
case 1:
System.out.println("请输入n的值:");
int n1 = sc.nextInt();
int sum1 = add(n1);
System.out.println("1到"+n1+"的和是:"+sum1);
break;
case 2 :
System.out.println("请输入n的值:");
int n2 = sc.nextInt();
int sum2 = addOuShu(n2);
System.out.println("1到"+n2+"的偶数和是:"+sum2);
break;
case 3:
System.out.println("100到999之间的水仙花数是:");
printShui();
break;
case 4 :
countShui();
break;
case 5 :
System.out.println("请输入m的值:");
int m5 = sc.nextInt();
System.out.println("请输入n的值:");
int n5 = sc.nextInt();
printRectangle(m5,n5);
break;
case 6:
System.out.println("请输入n的值:");
int n6 = sc.nextInt();
printZhi(n6);
break;
case 7:
System.out.println("请输入n的值:");
int n7 = sc.nextInt();
printNNZheng(n7);
break;
case 8:
System.out.println("请输入n的值:");
int n8 = sc.nextInt();
printNNFan(n8);
break;
case 886 :
flag = false ;
break;
default :
System.out.println("您输入的数据有误");
}
break ;
case 2 :
System.out.println("请您定义数组的长度;");
int length = sc.nextInt();
int[] arr = new int[length];
for (int i = 0;i < length ;i++ ) {
System.out.println("请您输入数组的第"+(i+1)+"个元素");
arr = sc.nextInt();
}
System.out.println("请您输入数组为:");
for (int i = 0; i < arr.length;i++ ) {
System.out.print(arr+" ");
}
System.out.println();
System.out.println("请再次选择你需要的操作:");
System.out.println("---------------------------");
System.out.println("1:数组遍历");
System.out.println("2:数组求和");
System.out.println("3:查找您输入的数多对应的下标");
System.out.println("4:根据输入的下标查找对应的元素");
System.out.println("5:找最大值");
System.out.println("6:找最小值");
System.out.println("7:反转数组");
System.out.println("8:从小到大排序");
System.out.println("9:从大到小排序");
System.out.println("输入886结束程序");
int bb = sc.nextInt();
switch (bb) {
case 1:
arrayIterator( arr);
break;
case 2:
int sum = arrayAdd( arr);
System.out.println("您的数组和为:"+sum);
break;
case 3:
System.out.println("请输入您需要查找的元素;");
int n33 = sc.nextInt();
int index = getIndex( arr,n33);
if (index != -1) {
System.out.println("您要查找的元素;"+n33+"对应的下标是"+ index);
}else {
System.out.println("数组中没有你要查找的元素");
}
break;
case 4:
System.out.println("请输入您需要查找的元素的下标:0到"+(length-1)+"之间");
int n44 = sc.nextInt();
int element = getElement( arr,n44);
System.out.println("您要查找的元素是;"+element);
break;
case 5:
int max = getMax( arr);
System.out.println("您的数组中最大值为:"+max);
break;
case 6:
int min = getMin( arr);
System.out.println("您的数组中最小值为:"+min);
break;
case 7:
arrayReverse( arr);
arrayIterator( arr);
break;
case 8:
arrayPaiXuZheng(arr);
arrayIterator( arr);
break;
case 9:
arrayPaiXuFan(arr);
arrayIterator( arr);
break;
case 886 :
flag = false ;
break;
default:
System.out.println("输入的数值有误");
}
break;
case 886 :
flag = false ;
break;
default:
System.out.println("输入的数值有误");
}
}
}
//以下是定义的函数:
public static int add(int n){ //求1到n的和
int sum = 0;
for (int i = 1; i <= n; i ++) {
sum = sum + i;
}
return sum;
}
public static int addOuShu(int n){ //求1到n的偶数和
int sum = 0;
for (int i = 1; i <= n; i ++) {
if (i % 2 == 0) {
sum = sum + i;
}
}
return sum;
}
public static void printShui() { //打印水仙花数
for (int i = 100;i <= 999 ;i++ ) {
int ge = i % 10;
int shi = i /10 %10;
int bai = i /10 /10;
if (i == ge *ge * ge + shi * shi * shi + bai * bai * bai) {
System.out.println(i);
}
}
}
public static void countShui() { //统计水仙花数
int count = 0;
for (int i = 100;i <= 999 ;i++ ) {
int ge = i % 10;
int shi = i /10 %10;
int bai = i /10 /10;
if (i == ge *ge * ge + shi * shi * shi + bai * bai * bai) {
count ++;
}
}
System.out.println("100到999中的水仙花数有"+count+"个");
}
public static void printRectangle(int m ,int n){ //打印m行n列矩形
for (int i = 1;i <= m ;i++ ) {
for (int j = 1;j <= n ;j++ ) {
System.out.print("*");
}
System.out.println();
}
}
public static void printZhi(int n){ //打印高为n的等直角三角形
for (int i = 1;i <= n ;i++ ) {
for (int j = 1;j <= i ;j++ ) {
System.out.print("*");
}
System.out.println();
}
}
public static void printNNZheng(int n) { //打印正nn乘法表
for (int i = 1;i <= n ;i++ ) {
for (int j = 1; j <= i ;j++ ) {
int a = i * j;
System.out.print(j+"*"+i+"="+a+"\t");
}
System.out.println();
}
}
public static void printNNFan(int n) { //打印反nn乘法表
for (int i = n;i >= 1 ;i-- ) {
for (int j = 1; j <= i ;j++ ) {
int a = i * j;
System.out.print(j+"*"+i+"="+a+"\t");
}
System.out.println();
}
}
public static void arrayIterator(int[] arr) { //数组的遍历
for (int i = 0; i < arr.length;i++ ) {
System.out.println(arr);
}
}
public static int arrayAdd(int[] arr) { //数组求和
int sum = 0;
for (int i = 0; i < arr.length;i++ ) {
sum += arr;
}
return sum;
}
public static int getIndex(int[] arr,int a) { //查找您输入的数多对应的下标
int index = -1;
for (int i = 0;i < arr.length ;i++ ) {
if (arr == a) {
index = i;
}
}
return index;
}
public static int getElement(int[] arr,int a) { //根据输入的下标查找对应的元素
return arr[a];
}
public static int getMax(int[] arr) { //找大值
int max = arr[0];
for (int i = 0;i < arr.length ;i++ ) {
if (arr > max) {
max = arr;
}
}
return max;
}
public static int getMin(int[] arr) { //找小值
int min = arr[0];
for (int i = 0;i < arr.length ;i++ ) {
if (arr < min) {
min = arr;
}
}
return min;
}
/*public static void arrayPaiXuZheng(int[] arr) { //从小到大排序(冒泡排序)
for (int i = 0;i < arr.length-1 ;i++ ) {
for (int j = 0; j < arr.length-1;j++ ) {
if (arr[j] > arr[j+1]) {
int temp = arr;
arr = arr[j];
arr[j] = temp;
}
}
}
}*/
public static void arrayPaiXuZheng(int[] arr) { //从小到大排序(选择排序)
for (int i = 0;i < arr.length-1 ;i++ ) {
for (int j = i+1; j < arr.length;j++ ) {
if (arr > arr[j]) {
int temp = arr;
arr = arr[j];
arr[j] = temp;
}
}
}
}
public static void arrayPaiXuFan(int[] arr) { //从大到小排序
for (int i = 0;i < arr.length-1 ;i++ ) {
for (int j = i + 1; j < arr.length;j++ ) {
if (arr < arr[j]) {
int temp = arr;
arr = arr[j];
arr[j] = temp;
}
}
}
}
public static void arrayReverse(int[] arr) { //数组反转
for (int i = 0;i < arr.length/2 ;i++ ) {
int temp = arr;
arr = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
}
}
|
|