import java.io.BufferedReader; import java.io.InputStreamReader; // Att kastas vid division med noll class DivZeroException extends Exception { } // class DivZeroException class Number { public Number(int tal) { this.num = tal; } // Number public void addera(int tal) { this.num = this.num + tal; } // addera public void dividera(int tal) throws DivZeroException { if (tal == 0) throw new DivZeroException(); this.num = this.num / tal; } // dividera public int varde() { return this.num; } private int num = 0; public static void main(String[] args) { BufferedReader kbd_reader = new BufferedReader( new InputStreamReader(System.in)); int tal = 1; String buf; Number num = new Number(14); System.out.println("Startar med: " + num.varde()); System.out.println("Adderar udda tal."); System.out.println("Dividerar jämna tal." ); try { while (tal != 99) { System.out.print("Ge ett tal (99 avslutar): "); buf = kbd_reader.readLine(); tal = Integer.parseInt(buf); if (tal % 2 == 1) num.addera(tal); else try { num.dividera(tal); } catch (DivZeroException e) { System.out.println("Ojdå! Division med 0."); } System.out.println("Nu är talet: " + num.varde()); } // while } // try catch (Exception e) { System.out.println("Fel inmatning / tangentbordsfel"); } // catch } // main } // class Number