import javax.swing.*; public class Ch2Monogram { public static void main(String [] args){ // String name;//// name = JOptionPane.showInputDialog(null,// "Enter your full name(First, middle, last : ");//// JOptionPane.showMessageDialog(null, name); String name, first, middle, last, space, monogram; space = " "; //showInoutDialog -> window with textArea name = JOptionPane.showInputDialog(null, "Enter your full name (first, middle, last):" ); //Extract first, middle, and last names first = name.substring(0, name.indexOf(space)); name = name.substring(name.indexOf(space)+1, name.length()); middle = name.substring(0, name.indexOf(space)); last = name.substring(name.indexOf(space)+1, name.length()); //Compute the monogram monogram = first.substring(0, 1) + middle.substring(0, 1) + last.substring(0,1); //Output the result JOptionPane.showMessageDialog(null, "Your monogram is " + monogram); //showMessageDialog -> just message dialog } }
<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("*&
댓글
댓글 쓰기