当前位置:天才代写 > tutorial > JAVA 教程 > Java编程那些事儿89——读取节制台输入

Java编程那些事儿89——读取节制台输入

2017-11-12 08:00 星期日 所属: JAVA 教程 浏览:377

副标题#e#

11.3.2 读取节制台输入

前面先容了利用IO类实现文件读写的示例,其实在许多处所还需要利用到IO类,这里再以读取节制台输入为例子来先容IO类的利用。

节制台(Console)指无图形界面的措施,运行时显示或输入数据的位置,前面的先容中可以利用System.out.println将需要输出的内容显示到节制台,本部门将先容如何接管用户在节制台中的输入。

利用节制台输入是用户在措施运行时和措施举办交互的一种基本手段,这种手段是Windows操纵系统呈现以前,操纵系统位于DOS时代时,用户和措施交互的主要手段。虽然,此刻这种交互的方法已经被图形界面(GUI)措施代替了。

在读取节制台操纵中,操纵系统在用户在节制台输入内容,并按回车键提交今后,将用户提交的内容通报给Java运行时系统,Java运行时系统将用户输入的信息结构成一个输入流工具——System.in,在措施员读取节制台输入时,只需要从该流中读取数据即可。至于结构流System.in的进程对付措施员来说是透明的。

查阅JDK API可以发明,System类中的静态属性in是InputStream范例的工具,可以凭据输入流的读取要领读取即可。

下面的示例代码实现了输入“回显”的成果,即将用户输入的内容从头显示到节制台,示例代码如下:

/**

* 读取节制台输入,并将输入的内容显示到节制台

*/

public class ReadConsole1 {

 public static void main(String[] args) {

  try{

   //提示信息

   System.out.println("请输入:");

   //数组缓冲

   byte[] b = new byte[1024];

   //读取数据

   int n = System.in.read(b);

   //转换为字符串

   String s = new String(b,0,n);

   //回显内容

   System.out.println("输入内容为:" + s);

  }catch(Exception e){}

 }

}

在该示例代码中,从System.in中读取出用户的输入,然后将用户输入的内容转换为字符串s,然后输出该字符串的内容即可。

下面实现一个简朴的逻辑,成果为:回显用户在节制台输入的内容,当用户输入quit时措施运行竣事。实现的代码如下:

/**

* 读取节制台输入

* 轮回回显内容,当输入quit时退出措施

*/

public class ReadConsole2 {

 public static void main(String[] args) {

  //数组缓冲

  byte[] b = new byte[1024];

  //有效数据个数

  int n = 0;

  try{

   while(true){

    //提示信息

    System.out.println("请输入:");

    //读取数据

    n = System.in.read(b);

    //转换为字符串

    String s = new String(b,0,n - 2);

    //判定是否是quit

    if(s.equalsIgnoreCase("quit")){

     break; //竣事轮回

    }

    //回显内容

    System.out.println("输入内容为:" + s);

   }

  }catch(Exception e){}

 }

}


#p#副标题#e#

在该示例代码中,插手了一个while轮回,使得用户的输入可以举办多次,在用户输入时,送入输入流的内容除了用户输入的内容以外,还包括”\r\n”这两个字符,所以在将输入的内容和quit较量时,去掉读出的最后2个字符,将剩余的内容转换为字符串。

最后是一个简朴的《掷骰子》的节制台小游戏,在该游戏中,玩家初始拥有1000的款子,每次输入押大照旧押小,以及下注金额,随机3个骰子的点数,假如3个骰子的总点数小于便是9,则开小,不然开大,然后判定玩家是否押对,假如未押对则扣除下注金额,假如押对则嘉奖和玩家下注金额沟通的款子。该措施的示例代码如下:

/**

* 掷骰子游戏实现

*/

public class DiceGame {

 public static void main(String[] args) {

  int money = 1000; //初始款子数量

  int diceNum = 0; // 掷出的骰子数值和

  int type = 0; // 玩家押的巨细

  int cMoney = 0; // 当前下注金额

  boolean success; // 胜负

  // 游戏进程

  while (true) {

   // 输入巨细

   System.out.println("请押巨细(1代表大,2代表小):");

   type = readKeyboard();

   // 校验

   if (!checkType(type)) {

    System.out.println("输入犯科,请从头输入!");

    continue;

   }

   // 输入下注金额

   while(true){

    System.out.println("你当前的款子数量是"

    + money + "请下注:");

    cMoney = readKeyboard();

    // 校验

    if (!checkCMoney(money,cMoney)) {

     System.out.println("输入犯科,请从头输入!");

     continue;

    }else{

     break;

    }

   }

   // 掷骰子

   diceNum = doDice();

   // 判定胜负

   success = isSuccess(type,diceNum);

   // 款子变革

   money = changeMoney(money,success,cMoney);

   // 游戏竣事

   if(isEnd(money)){

    System.out.println("你输了,bye!");

    break;

   }

  }

 }

 /**

 * 读取用户输入

 * @return 玩家输入的整数,假如名目犯科例返回0

 */

 public static int readKeyboard() {

  try {

   // 缓冲区数组

   byte[] b = new byte[1024];

   // 读取用户输入到数组b中,

   // 读取的字节数量为n

   int n = System.in.read(b);

   // 转换为整数

   String s = new String(b, 0, n - 2);

   int num = Integer.parseInt(s);

   return num;

  } catch (Exception e) {}

  return 0;

 }

 /**

 * 押的范例校验

 * @param type 范例

 * @return true代表切合要求,false代表不切合

 */

 public static boolean checkType(int type) {

  if (type == 1 || type == 2) {

   return true;

  } else {

   return false;

  }

 }

 /**

 * 校验下注金额是否正当

 * @param money   玩家款子数

 * @param cMoney 下注金额

 * @return true代表切合要求,false代表不切合要求

 */

 public static boolean checkCMoney(int money, int cMoney) {

  if (cMoney <= 0) {

    return false;

  } else if (cMoney <= money) {

    return true;

  } else {

    return false;

  }

 }

 /**

 * 掷骰子

 * @return 骰子的数值之和

 */

 public static int doDice() {

  int n = (int) (Math.random() * 6) + 1;

  int n1 = (int) (Math.random() * 6) + 1;

  int n2 = (int) (Math.random() * 6) + 1;

  // 输出随机功效

  System.out.println("庄家开:" + n + " " + n1 + " " + n2);

  return n + n1 + n2;

 }

 /**

 * 胜负判定

 * @param type 用户输入范例

 * @param diceNum   骰子点数

 * @return true代表赢,false代表输

 */

 public static boolean isSuccess(int type, int diceNum) {

  // 计较庄家范例

  int bankerType = 0;

  if (diceNum <= 9) {

   bankerType = 2;

   System.out.println("庄家开小!");

  } else {

   bankerType = 1;

   System.out.println("庄家开大!");

  }

  if (bankerType == type) { // 赢

   return true;

  } else { // 输

   return false;

  }

 }

 /**

 * 款子变革

 * @param money 用户钱数

 * @param success 胜负

 * @param cMoney 下注金额

 * @return 变革今后的款子

 */

 public static int changeMoney(int money, boolean success, int cMoney) {

  if (success) {

   money += cMoney;

  } else {

   money -= cMoney;

  }

  System.out.println("剩余金额:" + money);

  return money;

 }

 /**

 * 判定游戏是否竣事

 * @param money 玩家款子

 * @return true代表竣事

 */

 public static boolean isEnd(int money) {

  return money <= 0;

 }

}

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式