有这样一个题目:首先自定义一个异常,然后用户输入10个数整型,若不为整数则抛出自定义异常,并且将此数置0。然后将这10个数相加
如果总数>1000,则抛出自定义异常,并且将总和写入磁盘文件(在自定义异常中做)。
不会做的 哪位高手帮忙一下 多谢拉
如果总数>1000,则抛出自定义异常,并且将总和写入磁盘文件(在自定义异常中做)。
不会做的 哪位高手帮忙一下 多谢拉
解决方案 »
- int和intger的区别可不可以理解成String和char的区别?
- 关于窗体动态的定位到屏幕的中间处
- 汉字为何不能直接输入到JTable?
- 请问怎样才能查找一个已知数据并定位到jtable或jtree中的某行或某个节点上,且要是被选中状态?
- eclipse插件:openexplorer(直接用资源管理器打开选择文件、文件夹、JAR包)跨平台
- windows 界面移植问题
- 怎样使你的java项目生成在windows下可执行文件呢?(给分)
- 怎样调用windows的API?
- 我不知道这个问题是不是该在这问:关于JC!
- xml获取标签里面的内容(也包含标签)
- 关于rcp首选项的问题,请大大们关注一下
- 关于javac encoding 的问题
import java.util.regex.Pattern;public class Q5 {
private static final int MAX = 1000;
@Deprecated
public static void main(String[] args) {
args = new String[]{"1", "2200"};
Q5 q5 = new Q5();
try {
q5.check(args);
} catch (OutOf1000Excetion e) {
e.printStackTrace();
} catch (NotIntegerException e) {
e.printStackTrace();
}
}
/**
*
*
* @param strs
* @throws NotIntegerException
* @throws OutOf1000Excetion
*/
public void check(String... strs) throws NotIntegerException, OutOf1000Excetion {
Pattern pattern = Pattern.compile("\\d+");
int count = 0;
for (int i = 0; i < strs.length; i ++) {
Matcher matcher = pattern.matcher(strs[i]);
if (!matcher.matches()) {
throw new NotIntegerException("Not Integer");
}
count = count + Integer.parseInt(strs[i]);
if (count > MAX) {
throw new OutOf1000Excetion("Too big, large 1000");
}
}
}
/**
* 不为整数则抛出自定义异常
*
* @author cong_px
*/
class NotIntegerException extends Exception {
public NotIntegerException() {
super();
}
/**
*
* @param message
*/
public NotIntegerException(String message) {
super(message);
}
}
/**
* 总数>1000,则抛出自定义异常
*
*/
class OutOf1000Excetion extends Exception {
/**
*
*/
public OutOf1000Excetion() {
super();
}
/**
*
* @param message
*/
public OutOf1000Excetion(String message) {
super(message);
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class ExceptionTest {
private static final int MAX = 1000; public static void main(String[] args) throws OutOf1000Excetion {
int[] inputData = getInputData();
int sum = 0;
for(int i = 0; i < inputData.length; i++)
sum += inputData[i];
if(sum > 1000)
throw new OutOf1000Excetion(sum, "Out of 1000");
System.out.println("Sum=" + sum);
} private static int[] getInputData() {
int size = 10;
int ret[] = new int[size];
DataInputStream dis = new DataInputStream(System.in);
String temp = null;
for (int i = 0; i < 10; i++) {
System.out.println("please enter integer[" + i + 1 + "]:");
try {
temp = dis.readLine();
if (!temp.matches("[+-]?\\d+")) {
temp = "0";
throw new NotIntegerException("Not Integer.");
}
} catch (Exception e) {
}
ret[i] = Integer.parseInt(temp);
}
return ret;
}
}class NotIntegerException extends Exception {
public NotIntegerException() {
super();
} public NotIntegerException(String message) {
super(message);
}
}class OutOf1000Excetion extends Exception { public OutOf1000Excetion(int sum, String message) {
super(message);
writeToDisk(sum);
}
private void writeToDisk(int sum)
{
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("D:\\input.txt"));
dos.write(String.valueOf(sum).getBytes());
dos.flush();
dos.close();
} catch (Exception e) {
e.printStackTrace();
if(dos!=null)
try {
dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return;
}
}
}