2014년 3월 11일 화요일

JAVA - 자바 문자수 구하기

           public class Pack06 {
              public static void main(String[] args) {
                 if(args.length < 1) {
                    System.err.print("문자열을 입력해주세요!");
                    System.exit(-1);
                 }
                 int numLines = 1;
                 int numWords = 1;
                 int numChars = 0;
                 String s = args[0];
                 for(int i = 0, len = s.length(); i < len; ++i) {
                    switch (s.charAt(i)) {
                       case '/':
                       ++numLines;
.
                       case ' ':
                       ++numWords;
                       break;
                       default:
                       ++numChars;
                       break;
                    }
                 }
                 System.out.println("행수: " + numLines);
                 System.out.println("단어수: " + numWords);
                 System.out.println("문자수: " + numChars);
              }
           }
           //Pack07.java
           public class Pack07 {
              public static void main(String[] args) {
                 if(args.length % 2 != 0) {
                    System.err.println("학생의 수와 점수의 수가 일치하지 않습니다!");
                    System.exit(-1);
                 }
                 int numStudents = args.length / 2;
                 String[] names = new String[numStudents];
                 int[] grades = new int[numStudents];
                 //이름과 점수를 저장 
                 for(int i = 0; i < numStudents; ++i) {
                    names[i] = args[i * 2];
                    grades[i] = Integer.parseInt(args[i * 2 + 1]);
                 }
                 printNameOrder(names, grades);
                 printScoreOrder(names, grades);

              }

              public static void printNameOrder(String[] names, int[] grades) {
                 String[] tempNames = names.clone();
                 int[] tempGrades = grades.clone();
                 for(int i = 0, len = tempNames.length ; i < len; ++i ){
                    for(int j = i + 1; j < len ; ++j ) {
                       if(tempNames[j].compareTo(tempNames[i]) < 0) {
                          String stemp = tempNames[i];
                          tempNames[i] = tempNames[j];
                          tempNames[j] = stemp;
                          int iTemp = tempGrades[i];
                          tempGrades[i] = tempGrades[j];
                          tempGrades[j] = iTemp;
                       }
                    }
                 }
                 System.out.println("-------이름순-------");
                 for(int i = 0, len = names.length; i < len; ++i) {
                    System.out.println(tempNames[i] + "\t" + tempGrades[i]);
                 }
              }
              public static void printScoreOrder(String[] names, int[] grades) {
                 String[] tempNames = names.clone();
                 int[] tempGrades = grades.clone();
                 for(int i = 0, len = tempNames.length ; i < len; ++i ){
                    for(int j = i + 1; j < len ; ++j ) {
                       if(tempGrades[j] > tempGrades[i]) {
                          String stemp = tempNames[i];
                          tempNames[i] = tempNames[j];
                          tempNames[j] = stemp;
                          int iTemp = tempGrades[i];
                          tempGrades[i] = tempGrades[j];
                          tempGrades[j] = iTemp;
                       }
                    }
                 }
                 System.out.println("-------성적-------");
                 for(int i = 0, len = names.length; i < len; ++i) {
                    System.out.println(tempNames[i] + "\t" + tempGrades[i]);
                 }
              }
           }





댓글 없음:

댓글 쓰기