本帖最后由 HelloJav4 于 2018-2-14 13:59 编辑
这几天网上很火的,程序员租房贴出代码,就是这张图:
看起来挺有意思的,
其实很简单JAVA入门学过数组就知道电话是多少.
无聊之余写了个输入你的电话就自动产生JAVA代码的东西
具体看图:
(电话是我随便输的,大家不要当真哈)
源码在这,实现很简单,有别的语言的同学也可以贴出来看看效果
import java.util.*;
/**
* By Zjx on 2018-02-14 11:33.
*/
public class Tel {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的电话:");
String tel = sc.nextLine();
char[] arr = tel.toCharArray();
HashSet<Character> set = new HashSet<>();
for (char c : arr) {
set.add(c);
}
ArrayList<Integer> index = new ArrayList<>();
Object[] tels = set.toArray();
List<Object> list = Arrays.asList(tels);
Collections.shuffle(list);
for (char anArr : arr) {
for (int j = 0; j < list.size(); j++) {
if (anArr == (char) list.get(j)) {
index.add(j);
}
}
}
System.out.println("自动生成的代码:");
System.out.print("int[] arr = new int[] {");
for (int i = 0; i < list.size(); i++) {
if (i != list.size() - 1) {
System.out.print(list.get(i) + ",");
} else {
System.out.print(list.get(i) + "};");
}
}
System.out.println();
System.out.print("int[] index = new int[] {");
for (int i = 0; i < index.size(); i++) {
if (i != index.size() - 1) {
System.out.print(index.get(i) + ",");
} else {
System.out.print(index.get(i) + "};");
}
}
System.out.println();
System.out.println("String tel = \"\";");
System.out.println("for (int i : index) {");
System.out.println("tel += arr;");
System.out.println("}");
System.out.println("System.out.println(\"联系方式:\"+tel);");
System.out.println();
// print();
}
// public static void print(){
// int[] arr = new int[] {1,2,3,4,5,7,8,9};
// int[] index = new int[] {0,2,4,5,7,7,6,3,2,1,0};
// String tel = "";
// for (int i : index) {
// tel += arr;
// }
// System.out.println("联系方式:"+tel);
//
// }
}
|
|
|