代码:
//import java.util.Scanner;
public class practice{public static void main(String[] args) {int arr [] = {1,2,3,4};String str = arrToString(arr);System.out.println(str);}public static String arrToString(int [] arr){if(arr==null){return "";}if(arr.length==0){return "[]";}String result ="[";String result1 = "]";for (int i = 0; i < arr.length; i++) {if(i==arr.length-1){result = result + arr[i];}else{result = result + arr[i]+", ";}}result=result+result1;return result;}
}
代码结果:
自己写的代码:
import java.util.Scanner;
public class practice{public static void main(String[] args) {//键盘录入一个字符串,并进行遍历;Scanner input = new Scanner(System.in);System.out.println("输入一个字符串:");String a = input.next();print(a);printReserve(a);print(a);}public static void printReserve(String a){for(int i=a.length()-1;i>-1;i--) {char c = a.charAt(i);System.out.print(c+"\t");}System.out.println();}public static void print(String a){for(int i=0;i<a.length();i++) {char c = a.charAt(i);System.out.print(c+"\t");}System.out.println();}
}
代码运行结果截图:
视频代码:
听了以上步骤,自己写的代码:
import java.util.Scanner;
public class practice{static int count = 0;public static void main(String[] args) {Scanner input = new Scanner(System.in);int money;while (true) {System.out.println("请输入金额:");money = input.nextInt();if (money >= 0 && money <= 9999999) {break;} else {System.out.println("输入错误,请重新输入 ");}}//System.out.println(money);//获取各个位String da = "";while (money != 0) {int ge = money % 10;money = money / 10;String number = func(ge);da =number+ func3(count)+da;count++;}System.out.println(da);System.out.println(func1(da));}public static String func1(String da) {for (int i = count; i < 7; i++) {da = func(0) +func3(i)+ da;}return da;}public static String func(int ge) {String [] nums = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};return nums[ge];}public static String func3(int ge){String [] nums = {"元", "拾", "佰", "仟", "万", "拾", "佰"};return nums[ge];}
}
代码结果:
视频代码: