1.第一题(观察打印效果)
public class StringDemo {
public static void main (String[] args) {
String s1 = new String("java" );
StringBuffer s2 = new StringBuffer("java" );
String text1 = function1(s1);
StringBuffer text2 = function2(s2);
System.out.println(text1 + text2);
}
public static String function1 (String text) {
return text.replaceAll("j" ,"1" );
}
/**
* StringBuffer append(char c)
* 将 char 参数的字符串表示形式追加到此序列
*/
public static StringBuffer function2 (StringBuffer text) {
return text.append("c" );
}
}
打印结果:
1avajavac
2.第二题
public class NumberDemo {
public static void main (String[] args) {
/**
* 2.任取一个四位数字组成的四位正整数,
* 把其每位上的数字排成最大值和最小值(也是四位数)相减刚好还是这个数,
* 求这个四位正整数
*/
for (int number=1000 ;number<10000 ;number++) {
int a = number%10 ;
int b = (number%100 -a)/10 ;
int d = number/1000 ;
int c = number/100 -d*10 ;
int [] arraySort = sortNumber(a,b,c,d);
int minNumber = arraySort[3 ]*1 + arraySort[2 ]*10 + arraySort[1 ]*100 + arraySort[0 ]*1000 ;
int maxNumber = arraySort[0 ]*1 + arraySort[1 ]*10 + arraySort[2 ]*100 + arraySort[3 ]*1000 ;
if ((minNumber/1000 )!=0 &&((maxNumber-minNumber)==number)) {
System.out.println("满足条件的四位数为:" + number);
}
}
}
public static int [] sortNumber (int a,int b,int c,int d) {
int [] arrayNumber = {a,b,c,d};
int temp;
for (int j=1 ;j<arrayNumber.length;j++) {
for (int i=0 ;i<arrayNumber.length;i++) {
if (arrayNumber[i]>arrayNumber[j]) {
temp = arrayNumber[i];
arrayNumber[i] = arrayNumber[j];
arrayNumber[j] = temp;
}
}
}
return arrayNumber;
}
}
打印结果:
满足条件的四位数为:6174
3.第三题
package BydDemo;
/**
* 3.利用Java写一个方法用于保存Txt文件
*
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class BydDemo01 {
public static void main (String[] args) {
String fileName = "/1.txt" ;
String content = "利用Java写一个方法用于保存Txt文件" ;
if (saveFile(fileName,content)) {
System.out.println("写入保存成功!" );
} else {
System.out.println("写入保存失败!" );
}
}
public static boolean saveFile (String fileName,String content) {
String path = "E:/testFolder" ;
boolean success = true ;
File file = new File(path + fileName);
FileOutputStream fos = null ;
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
fos = new FileOutputStream(file);
byte [] b = content.getBytes();
fos.write(b);
} catch (Exception e) {
success = false ;
e.printStackTrace();
} finally {
if (fos!=null ) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}
}
打印结果:
写入保存成功!
4.第四题
package BydDemo;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class BydDemo02 {
public static void main (String[] args) {
printFile("/a.txt" );
}
public static void printFile (String fileName) {
String path = "E:/testFolder" ;
String file = path + fileName;
FileReader fr = null ;
BufferedReader buff = null ;
try {
fr = new FileReader(file);
buff = new BufferedReader(fr);
String s = null ;
while ((s = buff.readLine())!=null ) {
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr!=null ) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (buff!=null ) {
try {
buff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
打印结果:
111111
2222222
33333
5.第五题
package BydDemo;
public class BydDemo06 {
/**
* 5.传入一个数字数组,返回数字之和,如果数字是5则跳过不累加,
* 如果数字大于8就停止累加,返回结果;传入[1,3,5,9,1] = 4;
* [1,3,5,2,9,1] = 6;
*/
public static void main (String[] args) {
int [] array1 = {1 ,3 ,5 ,9 ,1 };
int [] array2 = {1 ,3 ,5 ,2 ,9 ,1 };
System.out.println("第一个数组之和:" + addAll(array1));
System.out.println("第二个数组之和:" + addAll(array2));
}
public static int addAll (int [] arrayNumber) {
int sum = 0 ;
for (int i=0 ;i<arrayNumber.length;i++) {
if (arrayNumber[i]==5 ) {
continue ;
}
if (arrayNumber[i]>8 ) {
break ;
}
sum += arrayNumber[i];
}
return sum;
}
}
打印结果:
第一个数组之和:4
第二个数组之和:6
6.第六题
package BydDemo;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class BydDemo07 {
/**
* 6.传入一个List,返回List中是数字的集合:
* List中包含"a23","1","2","c";
* 返回"1","2"
*/
public static void main (String[] args) {
List<String> arrayList1 = new ArrayList<String>();
arrayList1.add("a23" );
arrayList1.add("1" );
arrayList1.add("2" );
arrayList1.add("c" );
List<String> arrayList2 = getNumberList(arrayList1);
printArrayList(arrayList2);
}
public static List<String> getNumberList (List<String> list) {
List<String> arrayList2 = new ArrayList<String>();
for (int i=0 ;i<list.size();i++) {
if (isNumeric(list.get(i))) {
arrayList2.add(list.get(i));
}
}
return arrayList2;
}
public static void printArrayList (List<String> list) {
ListIterator< String> listIterator = list.listIterator();
System.out.println("打印集合中的数字:" );
while (listIterator.hasNext()) {
System.out.print(listIterator.next() + " " );
}
}
public static boolean isNumeric (String str) {
for (int i=0 ;i<str.length();i++) {
char ch = str.charAt(i);
if (!Character.isDigit(ch)) {
return false ;
}
}
return true ;
}
}
打印结果:
打印集合中的数字:
1 2
7.第七题
package BydDemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
public class BydDemo08 {
/**
* 7.传入一个Map,返回Map中值是数字的key集合,传入
* [a:1],[b:"ad"],[c:"3"]返回“a”,“c”
*/
public static void main (String[] args) {
Map<String, String> hashMap1 = new HashMap<String,String>();
hashMap1.put("a" , "1" );
hashMap1.put("b" , "ad" );
hashMap1.put("c" , "3" );
List<String> arrayList = getNumberMap(hashMap1);
printArrayList(arrayList);
}
public static void printArrayList (List<String> arrayList) {
ListIterator<String> listIterator = arrayList.listIterator();
System.out.println("输出符合的结果:" );
while (listIterator.hasNext()) {
System.out.print(listIterator.next() + " " );
}
}
public static List<String> getNumberMap (Map<String, String> hashMap) {
List<String> arrayList = new ArrayList<String>();
Set<String> set = hashMap.keySet();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = hashMap.get(key);
if (isNumeric(value)) {
arrayList.add(key);
}
}
return arrayList;
}
public static boolean isNumeric (String str) {
for (int i=0 ;i<str.length();i++) {
char ch = str.charAt(i);
if (!Character.isDigit(ch)) {
return false ;
}
}
return true ;
}
}
打印结果:
输出符合的结果:
a c
8.第八题
package BydDemo;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class BydDemo09 {
/**
* 8.传入一个日期格式,返回当前的日期;传入"yyyy-MM-dd"
* 返回"2014-09-05",传入"yy-MM-dd hh:mm:ss",
* 返回"14-09-05 09:30:19"等
*
*/
public static void main (String[] args) {
System.out.println("请输出日期格式:" );
Scanner sc = new Scanner(System.in);
String dataFormat = sc.nextLine();
getNowDataTime(dataFormat);
if (sc!=null ) {
sc.close();
}
}
public static void getNowDataTime (String dataFormat) {
Date currentTime = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dataFormat);
String dataString = simpleDateFormat.format(currentTime);
System.out.println("日期格式为:" + dataFormat + ";打印效果为:" + dataString);
}
}
打印结果:
请输出日期格式:
yyyy-MM -dd
日期格式为:yyyy-MM -dd ;打印效果为:2017 - 03 - 02
请输出日期格式:
yy-MM -dd hh:mm:ss
日期格式为:yy-MM -dd hh:mm:ss;打印效果为:17 - 03 - 02 06 :09 :58
9.第九题
package BydDemo;
import java.util.HashMap;
import java.util.Map;
public class BydDemo10 {
/**
* 9.写一个递归方法,传入一个Map,返回这个Map对象中一共
* 有多少个Map类型的对象
* Map中是可以添加Map,被添加的这个Map也可以添加Map,
* 所以需要判断传入的Map对象的所有子节点。如果是Map就 +1,
* 同时对这个子Map也需要循环它的子节点,如果它子节点是Map,
* 也需要+1;同时还需要继续循环这个子Map;
*/
/**
* 实现思路:遍历Map中的键值对,若键值对是Map类型,sum开始累加且递归。
* (递归边界条件: 键值是否有map),有map就继续调用递归,没有就处理下一个键值,直到没有为止
*/
public static int sum = -1 ;
public static void main (String[] args) {
Map<Object,Object> hashMap3 = new HashMap<Object,Object>();
hashMap3.put("10" , "i" );
hashMap3.put("11" , "j" );
hashMap3.put("12" , "k" );
Map<Object,Object> hashMap2 = new HashMap<Object,Object>();
hashMap2.put("7" , "f" );
hashMap2.put("8" , hashMap3);
hashMap2.put("9" , "h" );
Map<Object,Object> hashMap1 = new HashMap<Object,Object>();
hashMap1.put("4" , "c" );
hashMap1.put(hashMap2, "d" );
hashMap1.put("6" , hashMap2);
Map<Object,Object> hashMap = new HashMap<Object,Object>();
hashMap.put(hashMap1, "a" );
hashMap.put(2 , "b" );
hashMap.put("3" , hashMap1);
int number = isGoneCycle(hashMap);
System.out.println("总共的Map对象:" + number);
}
public static int isGoneCycle (Object hashMap1) {
if (hashMap1 instanceof Map) {
sum ++;
Map<Object,Object> hashMap = (Map<Object, Object>) hashMap1;
for (Object key : hashMap.keySet()) {
System.out.println("Key = " + key);
isGoneCycle(key);
}
System.out.println("****分割线****" );
for (Object value : hashMap.values()) {
System.out.println("Value = " + value);
isGoneCycle(value);
}
return sum;
} else {
return 0 ;
}
}
}
打印结果:
Key = 2
Key = 3
Key = {4=c, {7=f, 8={11=j, 12=k, 10=i}, 9=h}=d, 6={7=f, 8={11=j, 12=k, 10=i}, 9=h}}
Key = 4
Key = {7=f, 8={11=j, 12=k, 10=i}, 9=h}
Key = 7
Key = 8
Key = 9
****分割线****
Value = f
Value = {11=j, 12=k, 10=i}
Key = 11
Key = 12
Key = 10
****分割线****
Value = j
Value = k
Value = i
Value = h
Key = 6
****分割线****
Value = c
Value = d
Value = {7=f, 8={11=j, 12=k, 10=i}, 9=h}
Key = 7
Key = 8
Key = 9
****分割线****
Value = f
Value = {11=j, 12=k, 10=i}
Key = 11
Key = 12
Key = 10
****分割线****
Value = j
Value = k
Value = i
Value = h
****分割线****
Value = b
Value = {4=c, {7=f, 8={11=j, 12=k, 10=i}, 9=h}=d, 6={7=f, 8={11=j, 12=k, 10=i}, 9=h}}
Key = 4
Key = {7=f, 8={11=j, 12=k, 10=i}, 9=h}
Key = 7
Key = 8
Key = 9
****分割线****
Value = f
Value = {11=j, 12=k, 10=i}
Key = 11
Key = 12
Key = 10
****分割线****
Value = j
Value = k
Value = i
Value = h
Key = 6
****分割线****
Value = c
Value = d
Value = {7=f, 8={11=j, 12=k, 10=i}, 9=h}
Key = 7
Key = 8
Key = 9
****分割线****
Value = f
Value = {11=j, 12=k, 10=i}
Key = 11
Key = 12
Key = 10
****分割线****
Value = j
Value = k
Value = i
Value = h
Value = a
总共的Map对象:10