算法说明:
房间号可理解为8进制,只不过是0,1,2,3,5,6,7,8
所以转化为八进制后将4--7的数字递增1。
复制内容到剪贴板
代码:
package test;
public class CreatingRoomNO {
/**
* This Class is to format the hospital room No.
* @param args
*/
public static void main(String[] args) {
int roomNum;
try {
//get input
System.out.println("Please input the quantity of rooms:\n");
byte[] buff = new byte[10];
System.in.read(buff);
//format to number
roomNum = Integer.valueOf(new String(buff).trim());
//decimal to octal
String roomNumString = Integer.toOctalString(roomNum);
//check if overbrim
roomNum = Integer.valueOf(roomNumString);
if(roomNum>500){
System.out.println("Too many!!");
}
//format the special octal of hospital
for (int i = 7; i >=4; i--) {
roomNumString = roomNumString.replace(String.valueOf(i), String.valueOf(i+1));
}
//print the result
System.out.println("No. is " +roomNumString);
}catch (NumberFormatException e){
System.out.println("Check the input unless you want to be fired!");
}catch (Exception e) {
System.out.println("Oh my god ,no way");
}
}
}