import java.io.BufferedReader; import java.io.InputStreamReader; class num_c { public num_c( int tal ) { this.num = tal; } // num_c public void addera( int tal ) { this.num = this.num + tal; } // addera public void dividera( int tal ) throws div_0_exc { if ( tal == 0 ) { throw new div_0_exc(); //////////////////////// } 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; num_c num = new num_c( 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 ( div_0_exc exc ) { System.out.println( "Ojdå! Division med 0" ); } System.out.println( "Nu är talet: " + num.varde() ); } } // try catch ( Exception exc ) { System.out.println( "Fel inmatning / tangentbordsfel" ); } // catch } // main } // class num_c