首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

告诉帮忙把以下代码写写,参考写, 感谢万分解决方案

2012-01-10 
告诉帮忙把以下代码写写,参考写, 感谢万分1.编写一个程序。要求定义一种比较直观的文本文件格式,用户可以按

告诉帮忙把以下代码写写,参考写, 感谢万分
1.编写一个程序。要求定义一种比较直观的文本文件格式,用户可以按这种格式在一个文本文件中输入学生姓名和成绩。然后,程序可以打开指定的文本文件,从中读取所有的学生姓名和成绩,计算并输出平均考试成绩、最高考试成绩,以及取得最高考试成绩的学生名单。这些输出结果要求同时能够输出到一个指定的文本文件中。

2、编程把一个数字表示的4285如何转成文字肆千贰百捌拾伍元。


3、请在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。
 
4、编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根据内容为[1][2][3]的数组形成内容为"1,2,3"的字符串。 

5、编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。 十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2,这次得到的余数就是次低位,如此循环,直到被除数为0为止。其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。 

6、请编写一个字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。 

7、编写一个程序,当用户输入一个目录时,信息后,该程序能列出该目录下的所有子目录和文件。

8、编程把一个数字表示的4285如何转成文字肆千贰百捌拾伍元。

[解决办法]
1.懒的写.
2.参考http://blog.csdn.net/lip009/archive/2006/10/19/1340814.aspx,带角分的
3.参考

Java code
public class Test{    public int searchChar(char chars[],char c){        if(chars==null){            throw new IllegalArgumentException("数组为null");        }        int result=-1;        for(int i=0;i<chars.length;i++){            if(chars[i]==c){                result=i;                break;            }        }        return result;    }    public static void main(String args[]){        Test test=new Test();        System.out.println(test.searchChar(new char[]{'a','b','c'}, 'b'));        System.out.println(test.searchChar(new char[]{'a','b','c'}, 'd'));        System.out.println(test.searchChar(null, 'b'));    }}
[解决办法]
4.
Java code
public class Test{    public static void main(String args[]){        String strs[]=new String[]{"1","2","3"};        String result="";        for(int i=0;i<strs.length;i++){            result=result+","+strs[i];        }        result=result.replaceAll("^,","");        System.out.println(result);    }}
[解决办法]
我把所有的题目都写了,楼主自己慢慢看吧,反正不是什么难题,应该没有注释也能看懂。代码太多,写注释就把我累死了。
Java code
package com.houlei;import java.io.BufferedReader;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.LinkedList;/** * 1.编写一个程序。要求定义一种比较直观的文本文件格式,用户可以按这种格式在一个文本文件中输入学生姓名和成绩。 * 然后,程序可以打开指定的文本文件,从中读取所有的学生姓名和成绩,计算并输出平均考试成绩、最高考试成绩,以及取得最高考试成绩的学生名单。 * 这些输出结果要求同时能够输出到一个指定的文本文件中。 *  * 2、编程把一个数字表示的4285如何转成文字肆千贰百捌拾伍元。 *  * 3、请在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。 * 要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。 * 在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。 *  * 4、编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根据内容为[1][2][3]的数组形成内容为"1,2,3"的字符串。 *  * 5、编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。 * 十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2,这次得到的余数就是次低位,如此循环,直到被除数为0为止。 * 其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。 *  * 6、请编写一个字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。 *  * 7、编写一个程序,当用户输入一个目录时,信息后,该程序能列出该目录下的所有子目录和文件。
[解决办法]
我把所有的题目都写了,楼主自己慢慢看吧,反正不是什么难题,应该没有注释也能看懂。代码太多,写注释就把我累死了。


Java code
package com.houlei; 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
/**
* @author 侯磊
* Questions 类用于测试
*/
public class Questions {
public static void main(String[] args) throws IOException {
try {
System.out.println("/*** 题目 1 :要求存在测试文件 **/");
Question1.proccess("d:/st1.txt", "d:/st2.txt"); // Q1
System.out.println("/*** 题目 2  **/");
System.out.println(Question2.change(4285)); // Q2
System.out.println("/*** 题目 3  **/");
char[] resouce = { '你', '好', '啊', '哈', '哈', '。' };
System.out.println(Question3.search(resouce, 'a')); // Q3
System.out.println(Question3.search(resouce, '哈')); // Q3
System.out.println(Question3.search(null, '哈')); // Q3
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("/*** 题目 4  **/");
System.out.println(Question4.jion(new int[] { 1, 2, 3 })); // Q4
System.out.println("/*** 题目 5 :要求输入待转换的整数数字 **/");
System.out.println(Question5.printInput()); //Q5
System.out.println("/*** 题目 6  **/");
Question6.test("d:/st1.txt"); // Q6
System.out.println("/*** 题目 7  **/");
Question7.listPath("d:/"); // Q7
}

}

class Question7 {
public static void listPath(String pathName) {
File path = new File(pathName);
if (path.isDirectory()) {
File[] subPaths = path.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}

});
System.out.println("所有子目录如下:");
for (int i = 0; i < subPaths.length; i++) {
System.out.println("\t" + subPaths[i].getName());
}
File[] subFiles = path.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
});
System.out.println("所有文件如下:");
for (int i = 0; i < subFiles.length; i++) {
System.out.println("\t" + subFiles[i].getName());
}
}
}
}

class Question6 {
private BufferedReader reader;

private long lineNumber = 0;

public Question6(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
}

public String readLine() throws IOException {
String str = reader.readLine();
if (str != null) {
lineNumber++;
return lineNumber + ":" + str;
}
return str;
}

/**
* 该方法用于测试
*
* @throws IOException
*/
public static void test(String fileName) throws IOException {
Question6 reader = new Question6(new FileInputStream(fileName));
String str;
while ((str = reader.readLine()) != null) {
System.out.println(str);
}
}
}

class Question5 {
public static String printInput() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
br.close();
int num = Integer.parseInt(line);
StringBuffer sb = new StringBuffer();
while (num != 0) {
sb.insert(0, (num & 1));
num = num >>> 1;//左补0的右移运算,对应于题目要求的除2运算,要比题目要求的好些。


}
return sb.toString();
}
}

class Question4 {
public static String jion(int[] nums) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < nums.length; i++) {
sb.append(nums[i]);
if (i < nums.length - 1)
sb.append(",");
}
return sb.toString();
}
}

class Question3 {
public static int search(char[] resouce, char c)
throws IllegalAccessException {
if (resouce == null)
throw new IllegalAccessException("所传入的参数为null");
for (int i = 0; i < resouce.length; i++) {
if (c == resouce[i])
return i;
}
return -1;
}
}

/**
* 第2个与第8个相同
*/
class Question2 {
private static String[] nums = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒","捌", "玖" };
private static String[] units = { "元", "拾", "佰", "仟", "万" };
public static String change(int money) {
StringBuffer sb = new StringBuffer();
if (money < 0)
return null;
int tm;
for (int i = 1, j = 0; true; i *= 10, j++) {
tm = money % (i * 10);
tm = tm / i;
if (tm == 0)
break;
sb.insert(0, nums[tm] + units[j]);
}
return sb.toString();
}
}

/**
* 文件格式:每一行表示一个学生的一条记录信息。先是学生的姓名,然后是分号,最后是学生的成绩(也就是说,学生姓名和成绩之间以分号分隔)。
* Student类属于本题目
*/
class Question1 {
public static void proccess(String sourceFile, String destFile)
throws IOException {
// 读取所有的学生姓名和成绩
BufferedReader br = new BufferedReader(new FileReader(sourceFile));
LinkedList <Student> ss = new LinkedList <Student>();
String str;
double average = 0.0;
int count = 0;
Student highMarkStu = new Student(null, 0);
while ((str = br.readLine()) != null) {
String record[] = str.split(";|;");
float mark = Float.parseFloat(record[1]);
if (record[0].equals(""))
continue;
Student std = new Student(record[0], mark);
ss.add(std);
if (mark >= highMarkStu.getMark())
highMarkStu = std;
average += mark;
count++;
}
br.close();
average = average / count;
// 输出平均考试成绩、最高考试成绩,以及取得最高考试成绩的学生名单
StringBuffer sb = new StringBuffer();
sb.append("平均考试成绩:\t\t").append(average).append(" 分\r\n");
sb.append("最高考试成绩:\t\t").append(highMarkStu.getMark()).append(" 分\r\n");
sb.append("最高考试成绩的学生:\t").append(highMarkStu.getName()).append("\r\n");
FileWriter writer = new FileWriter(destFile);
writer.write(sb.toString());
writer.close();
}
}

class Student implements java.lang.Comparable {
private float mark = 0.0f;
private String name;
public float getMark() {
return mark;
}
public String getName() {
return name;
}
public Student(String name, float mark) {
this.mark = mark;
this.name = name;
}
public int compareTo(Object obj) {
if (obj instanceof Student) {
Student std = (Student) obj;
if (this.mark == std.mark)
return 0;
if (this.mark > std.mark)
return 1;
}
return -1;
}
}


[解决办法]
Java code
 
import java.io.*;
import java.util.*;

public class ChengJi {
ArrayList chengJiAL = new ArrayList();      //在不考虑速度的情况,随便创建List,用Map 会好点。。。


String[] chengJiBiao;

//用于读成绩             
public void duChengJi(String weiZhi) {
try {
FileReader fr = new FileReader(weiZhi);  //用此是读取字符流 + Buffered  方便点可以 一读一行
BufferedReader br = new BufferedReader(fr); // 还方便可以解决 读中文的 出乱麻的情况
String chengJi;

while((chengJi=br.readLine()) != null) {  //读到文件为Null为止
chengJiAL.add(chengJi);          //象list中加元素
}
br.close();
} catch(Exception a) {
a.printStackTrace();
System.exit(0);
}
}

//在屏幕上打印 和 向 wenJianMing 中写入
public void daYingXieChengJi(String wenJianMing) {
this.zhuanHuanString();
this.paiXu();
try {

for(int i=0; i <chengJiBiao.length;i++) {
System.out.println(chengJiBiao[i]);
}

FileWriter fw = new FileWriter(wenJianMing);//写入字符流 ,可以写字符串
for(int i=0; i <chengJiBiao.length;i++) {
fw.write(chengJiBiao[i]);
fw.write("\n");      //忘记怎么写入换行了,不好意思
}
fw.close();
} catch(Exception a) {
a.printStackTrace();
System.exit(0);
}
}

//下面将 list 转成 String
public void zhuanHuanString(){
Object[] chengJiTemp = chengJiAL.toArray();  //将list转成Objcet
chengJiBiao = new String[chengJiTemp.length]; //转换成String
for(int i=0; i <chengJiTemp.length;i++) {    //现在有种 好像叫枚举的 可以很方便的用toArray()转换
chengJiBiao[i] = (String)chengJiTemp[i];
}
}

//最菜的排序
public void paiXu() {
for(int i=0; i <chengJiBiao.length;i++) {
String ssi = chengJiBiao[i].substring(panDuanDouHao(chengJiBiao[i])+1,chengJiBiao[i].length());
      //substring() 重新构建String
      //panDuanDouHao() 自己定义的一个 判断是中文 还是 英文 逗号的方法 ,并返回逗号所在值
     
Integer sii = new Integer(ssi);
for(int j=0; j <chengJiBiao.length;j++) {
String ssj = chengJiBiao[j].substring(panDuanDouHao(chengJiBiao[j])+1,chengJiBiao[j].length());
Integer sjj = new Integer(ssj);
if(sii.compareTo(sjj) < 0) {    //compareTo()比较Integer 对象
String temp = chengJiBiao[i];
chengJiBiao[i] = chengJiBiao[j];
chengJiBiao[j] = temp;
}
}
}
}

//判断中文或英文逗号并返回逗号所在值
public static int panDuanDouHao(String s) {
int index = 0;
if(s.indexOf(',') >=0) {
index = s.indexOf(',');
}else if(s.indexOf(',') >=0) {
index = s.indexOf(',');
}else {
index = -1;
}
return index;
}

public static void main(String[] args) {

ChengJi cj = new ChengJi();
cj.duChengJi("ChengJi.txt");//在文本中 加入
//王五,100
//张三,110
//历史,10

cj.daYingXieChengJi("ChengJiBiao.txt");


}
}
//本人很菜,刚自学不久,写的不好,不好意思 ,显丑了~~!!!

热点排行