📄 chapter4n2.java
字号:
import java.lancs.* ;/* * program to test the "Person" class */public class Chapter4n2 { /* * main */ public static void main(String[] args) throws Exception { Person myPerson = new Person() ; boolean continueLoop = true ; while (continueLoop) { // get the command (a character) BasicIo.prompt("type a single command character: ") ; char command = BasicIo.readCharacter() ; int age = 0 ; String response = null ; // call the appropriate method switch (command) { case 'a' : // set forename BasicIo.prompt("type a forename: ") ; response = BasicIo.readString() ; myPerson.setForename(response) ; break ; case 'b' : // set surname BasicIo.prompt("type a surname: ") ; response = BasicIo.readString() ; myPerson.setSurname(response) ; break ; case 'c' : // set age BasicIo.prompt("type an age: ") ; age = BasicIo.readInteger() ; myPerson.setAge(age) ; break ; case 'd' : // set gender BasicIo.prompt("type a gender: ") ; response = BasicIo.readString() ; if (response.equals("male")) myPerson.setGender(Person.MALE) ; else if (response.equals("female")) myPerson.setGender(Person.FEMALE) ; else myPerson.setGender(Person.UNKNOWN) ; break ; case 'e' : // get forename System.out.print("forename is ") ; System.out.println(myPerson.getForename()) ; break ; case 'f' : // get surname System.out.print("surname is ") ; System.out.println(myPerson.getSurname()) ; break ; case 'g' : // get age System.out.print("age is ") ; System.out.println(myPerson.getAge()) ; break ; case 'h' : // get gender System.out.print("gender is ") ; switch (myPerson.getGender()) { case Person.MALE : System.out.println("male") ; break ; case Person.FEMALE : System.out.println("female") ; break ; default : System.out.println("unknown") ; } break ; case '?' : // print 'help' information System.out.println("a : set forename") ; System.out.println("b : set surname") ; System.out.println("c : set age") ; System.out.println("d : set gender") ; System.out.println("e : get forename") ; System.out.println("f : get surname") ; System.out.println("g : get age") ; System.out.println("h : get gender") ; System.out.println("? : output this message") ; System.out.println("s : stop the program") ; break ; case 's' : // stop the program continueLoop = false ; break ; default : System.out.println("illegal command") ; } } } // end of method main } // end of class Chapter4n2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -