返回首页

我有代码,我需要有一个主体和多种方法重构。人在那里,可以帮助一个非常简单的答案吗?

public class Quiz {

 

	/**

	 * @param args

	 */

	public static void main(String[] args) {

		BufferedReader br = null;

		BufferedWriter bw = null;

		String inputString = null;

		String outputString = null;

 

		// accept string from user

		System.out.print("User input ");

		try {

			br = new BufferedReader(new InputStreamReader(System.in));

			inputString = br.readLine();

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {

				br.close();

			} catch (IOException e) {

				e.printStackTrace();

			}

		}

		System.out.println(inputString);

 

		// write string to disk file named data.dat

		try {

			bw = new BufferedWriter(new FileWriter("data.dat"));

			bw.write(inputString);

			bw.flush();

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {

				bw.close();

			} catch (IOException e) {

				e.printStackTrace();

			}

		}

 

		// read the string back in from the file

		try {

			br = new BufferedReader(new FileReader("data.dat"));

			inputString = br.readLine();

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {

				br.close();

			} catch (IOException e) {

				e.printStackTrace();

			}

		}

		// display to the screen

		System.out.println(inputString);

		

	}// main



}// class
:8171645 |会员

回答

评论会员:游客 时间:2012/02/04
如果你是专门为重构方法,你可以参考MartinFowler的"重构"一书。有用的东西
!BlackJack99
评论会员:游客 时间:2012/02/04
好,首先,我希望你不希望任何人在这里为你做它,但你必须做什么,基本上是这样的:{C}正如我以前说过,我不是要为你做它,但是这应该给你一个非常明确的想法,你需要做什么,那些都只是代码,你张贴,所以我不知道它甚至可以快速编辑被编译,它可以实现更好的,但你有什么要做的总体思路是:-一个方法,从键盘读取用户输入,并返回一个字符串-方法需要一个字符串作为参数,并把它写入一个文件-一个方法,从文件中读取输入并返回-最后请从你的主的方法PS:只是想知道,我了解之前你了解方法的异常?我看你是使用try/catch块漂亮的经常,应该是能够做到这一点容易..