需要帮助,Java的新手 [英] Need help, new to Java

查看:71
本文介绍了需要帮助,Java的新手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码需要重构,以具有一种主要和多种方法.有没有人可以提供非常简单的答案?

I have code that I need to refactor to have one main and multiple methods. Anyone out there that can help with a very simple answer?

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

推荐答案

如果您正在寻找重构方法,可以参考Martin Fowler的"Refactoring"一书.有用的东西!
If you are specifically looking for Refactoring methodology, you can refer ''Refactoring'' book by Martin Fowler. Useful stuff!


好吧,首先,我希望您不要期望有人在这里为您做这件事,但是您要做的基本上是这样的事情:

well, first of all, i hope you are not expecting anyone here to do it for you, but what you have to do is basically something like this:

public class Quiz {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
			
			string readString = ReadInput();
			System.out.println(readString);
			
			StoreToFile(readString);
			
			string readFromFileString = ReadFromFile();
			System.out.println(readFromFileString);
			
	}// main
	
	private string ReadInput()
	{	BufferedReader br = null;
		String inputString = 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();
			}
		}
		return inputString;
	}
	
	private void StoreToFile(string inputString)
	{
		
		BufferedWriter bw = null;
		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();
			}
		}
	}
	private string ReadFromFile()
	{
		BufferedReader br = null;
		String inputString = null;
		// 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
		return inputString;
	}

}// class



正如我之前所说,我不会为您这样做,但这应该可以使您非常清楚地知道您需要做什么,这只是对您发布的代码进行的快速编辑,因此我不确定它是否可以进行编译,并且可以更好地实现,但是您必须做的一般想法是:

-一种从键盘读取用户输入并返回字符串的方法
-一种将字符串作为参数并将其写入文件的方法
-从文件读取输入并返回输入的方法
-并最终从主菜单中调用方法

附:只是想知道,在您了解方法之前,您是否了解过异常?我看到您经常使用try/catch块,您应该可以轻松地做到这一点.



and as i said before, im not going to do it for you, but this should give you a very clear idea what you need to do, those are just a quick edit of the code that you posted, so im not sure it can even be compiled, and it could be implemented better, but the general idea of what you have to do is:

- a method that read user input from keyboard and return a string
- a method that takes a string as a parameter and write it to a file
- a method that read input from a file and return it
- and finally call the methods from your main

p.s: just wondering, did u learnt about exception before you understand method? i see you are using the try/catch blocks pretty often, u should be able to do this easily..


这篇关于需要帮助,Java的新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆