测试代码
package first;
import java.util.Arrays;
import java.util.Scanner;
/*
//sdfsd
*/
//sdfsd
//
public class Test {
private static int pduan1(String x) {
if (x.equals("sort1"))
return 1;
else if (x.equals("sort2"))
return 2;
else
return 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] str = new String[100];
String t;
int d;
int[] b = new int[100];
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int i = 0; i < n; i++) {
str[i] = in.next();
}
for (int i = 0; i < n; i++) {
b[i] = Integer.parseInt(String.valueOf(str[i].substring(6, 14)));
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (b[j] > b[j + 1]) {
t = str[j];
str[j] = str[j + 1];
str[j + 1] = t;
d = b[j];
b[j] = b[j + 1];
b[j + 1] = b[j];
}
}
}
in.nextLine();
while (true) {
String x = in.next();
int a = pduan1(x);
switch (a) {
case 1:
for (int i = 0; i < n; i++) {
System.out.println(str[i].substring(6, 10) + "-"
+ str[i].substring(10, 12) + "-"
+ str[i].substring(12, 14));
}
break;
case 2:
for (int i = 0; i < n; i++) {
System.out.println(str[i]);
}
break;
default:
System.out.println("exit");
break;
}
if (a == 0) break;
}
}
}
源程序
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
int whiteLines = 0, commentLines = 0, normalLines = 0;
boolean isCommet = false;
Scanner scanner = new Scanner(new File(args[0]));//可在命令行里面输入
while (scanner.hasNextLine()) {
String string = scanner.nextLine();
if (string.trim().length() == 0) {
whiteLines++;
} else if (string.startsWith("//")) { //单行注释
commentLines++;
} else if (string.startsWith("/*")) { //多行注释
commentLines++; //注释行数先加一
if (!string.endsWith("*/")) { //如果字符串尾不是*/
isCommet = true; //置isComment为true,说明下面还有注释
}
} else if (isCommet) {
commentLines++; //isComment为true,则注释行数加一
if (string.endsWith("*/")) { //如果结尾是*/,则isComment为false
isCommet = false;
}
} else {
normalLines++;
}
}
System.out.println("The code has " + normalLines + " lines");
System.out.println("The whiteline has " + whiteLines + " lines");
System.out.println("The comment has " + commentLines + " lines");
}
}