Homework8
import java.io.*;
public class Counter
{
private int count;
public void readInput() throws IOException
{boolean tryAgain = true;
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System. in));
System.out.println("Enter a number.");
while (tryAgain)
{String count1 = keyboard.readLine();
count = Integer.parseInt(count1);
if (count >= 0)tryAgain = false;
elseSystem.out.println("Illegal date. Reenter input.");}}
public void reset(){count=0;}
public void inc(){count=count+1;
}public void dec(){count=count-1;
}public void output(){System.out.println(count);}}
====================================
public class Demo{
public static void main(String[] args) throws IOException {
Counter counter= new Counter();
counter.readInput();counter.inc();
counter.output();
counter.inc();counter.output();
counter.dec();
counter.output();
counter.reset();counter.output();}}
0 Comments:
張貼留言
<< Home