import java.util.Scanner; class PrimeChecker { public boolean isPrime(int input) { for (int i = 2; i < input; i++) { if (input % i == 0) { return false; } } return true; } } public class ex5 { public static void main(String [] args){ Scanner in = new Scanner(System.in); PrimeChecker pc = new PrimeChecker(); while(true){ try{ int input = Integer.parseInt(in.nextLine()); if(input < 0) break; else if(pc.isPrime(input) == false) throw new Exception("Not a prime"); System.out.println(input + " is a prime number"); } catch (Exception e) { System.out.println("Not a prime"); } } System.out.println("Terminated"); } }
<HTML> <body> </body> <script> function Calculator() { let methods = { "-" : (a, b) => a - b, "+" : (a, b) => a + b }; //methods is an object which keeps key for operators //and value to return the actual operation values //each returns the result of operation that key(operator) does this.calculate = function (str){ //calculate is one element in the function Calculator //it takes the string and returns the value //in the function element list is delimeted by , not ; let split = str.split(" "), a = +split[0], op = split[1], b = split [2] if(!methods[op] || isNaN(a) || isNaN(b)) { return NaN; // error handling } return methods[op](a,b); } this.addMethod = function(name, func){ methods[name] = func; //this is how to add new key and ele to object } } let powerCalc = new Calculator; powerCalc.addMethod("*&
댓글
댓글 쓰기