|
发表于 2015-2-3 23:53:37
|
显示全部楼层
11题:
public class Test11 {
public static void main(String[] args) {
System.out.println(calcAge(1, 10, 8));
}
static int calcAge(int n, int age, int end){
if (n >= end)
return age;
return calcAge(n + 1, age + 2, end);
}
}
12题:
public class Test12 {
public static void main(String[] args) {
final long PER_DAY_US = 24L * 60 * 60 * 1000 * 1000;// ΢��
final long PER_DAY_MS = 24L * 60 * 60 * 1000; // ����
System.out.println(PER_DAY_US / PER_DAY_MS);
}
}
13题:
public class Test13 {
public static void main(String[] args) {
int j = 0;
for (int i = 0; i < 100; i++) {
j++;
}
System.out.println("j = " + j);
}
}
14:
public class Test14 {
private final static int end = Integer.MAX_VALUE - 1;
private final static int start = Integer.MAX_VALUE - 101;
public static void main(String[] args) {
int count = 0;
for (int i = start; i <= end; i++) {
count++;
}
System.out.println(" count = " + count);
}
}
15:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test15 {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(format.format(new Date(new Date().getTime()-1000*24*3600)));
}
}
16:
import java.io.File;
public class Test16 {
public static void main(String[] args) {
File dir = new File("c:\\");
// System.out.println(dir.isDirectory());
for(String s : dir.list())
System.out.println(s);
}
}
17:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class Test17 {
public static void main(String[] args) {
File dirSrc = new File("d:\\java\\");
File dirDest = new File("d:\\jad\\");
if(!dirDest.exists()){
dirDest.mkdirs();
}
if(!dirDest.isDirectory()){
System.out.println("there exists a file named jad");
return;
}
FileOutputStream fos = null;
FileInputStream fis = null;
for(File f : dirSrc.listFiles()){
if (f.isFile() && f.getName().endsWith(".java")) {
String path = dirDest.getAbsolutePath() + File.separator
+ f.getName().replaceAll("\\.java", "\\.jad");
File destFile = new File(path);
try {
if (!destFile.createNewFile())
System.out.println(path+" has been exist. nothing was done.");
else
System.out.println(path+" has been copied.");
fos = new FileOutputStream(destFile);
fis = new FileInputStream(f);
byte[] content = read(fis);
fos.write(content);
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
System.out.println("over");
}
public static byte[] read(InputStream in) throws IOException {
byte[] buf = new byte[1024];
int length = 0;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
while ((length = in.read(buf, 0, buf.length)) > 0) {
bout.write(buf, 0, length);
}
bout.flush();
return bout.toByteArray();
}
}
18:
import java.util.Random;
public class Test18 {
private static Random rdm = new Random();
public static void main(String[] args) {
StringBuffer word = null;
switch (rdm.nextInt(3)) {
case 1:
word = new StringBuffer("P");
break;
case 2:
word = new StringBuffer("G");
break;
default:
word = new StringBuffer("M");
}
word.append('a');
word.append('i');
word.append('n');
System.out.println("word = "+word);
}
}
19:
import java.io.UnsupportedEncodingException;
public class Test19 {
public static void main(String[] args) {
try {
System.out.println(subStr("我ABCDEF", 4, "utf8"));
System.out.println(subStr("我AB汉CDEF", 6, "utf8"));
System.out.println(subStr("我AB汉CDEF", 7, "utf8"));
System.out.println(subStr("我AB汉CDEF", 8, "utf8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
static String subStr(String orig, int num, String charset) throws UnsupportedEncodingException{
int count = 0;
int i=0;
for (; i<orig.length(); i++){
Character c = orig.charAt(i);
count = count + (c.toString().getBytes(charset).length);
if (count > num) break;
}
return orig.substring(0, i);
}
}
20:
不会(‘;ω;´)。
容我翻课本复习一下。
能不能直接AtomicInteger啊。
老师你好,我是来拿阳光普照奖的。
|
|