首页 > 开发 > JAVA > 正文

模拟注册--登录--幸运抽奖---全过程(一步步实现)

2016-05-20 14:39:12  来源:慕课网
  
  System.out.println("欢迎使用抽奖富翁系统");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.抽奖");
System.out.println("***");
Scanner in = new Scanner(System.in); System.out.print("请选择菜单:"); int sc = in.nextInt(); switch (sc) { case 1: System.out.println("抽奖富翁系统----->注册"); break; case 2: System.out.println("抽奖富翁系统----->登录"); break; case 3: System.out.println("抽奖富翁系统----->抽奖"); break; default: System.out.println("输入错误,请重新输入:"); break; }   
  public static void main(String[] args) {
System.out.println("欢迎使用抽奖富翁系统");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.抽奖");
System.out.println("***");
Scanner in = new Scanner(System.in); String answer=""; do { System.out.print("请选择菜单:"); int sc = in.nextInt(); switch (sc) { case 1: System.out.println("抽奖富翁系统----->注册"); System.out.println("是否选择继续:Y/N"); answer=in.next(); continue; case 2: System.out.println("抽奖富翁系统----->登录"); System.out.println("是否选择继续:Y/N"); answer=in.next(); continue; case 3: System.out.println("抽奖富翁系统----->抽奖"); System.out.println("是否选择继续:Y/N"); answer=in.next(); continue; default: System.out.println("输入错误,请重新输入:"); break; } } while ( answer.equals("Y")); System.out.println("程序结束,退出程序。"); }   
  System.out.println("欢迎使用抽奖富翁系统");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.抽奖");
System.out.println("***");
Scanner in = new Scanner(System.in); String answer=""; do { System.out.print("请选择菜单:"); int sc = in.nextInt(); switch (sc) { case 1: System.out.println("抽奖富翁系统----->注册"); System.out.println("输入用户名:"); String name=in.next(); System.out.println("输入密码:"); int password=in.nextInt(); int card= (int)(Math.random()*9000+1000); System.out.println("card:"+card); System.out.println("注册成功,请记住你的会员卡号,会员名,密码"); System.out.println("会员卡号\t"+"会员名\t"+"密码"); System.out.println(card+"\t"+name+"\t"+password); System.out.println("是否选择继续:Y/N"); answer=in.next(); continue; case 2: System.out.println("抽奖富翁系统----->登录"); System.out.println("是否选择继续:Y/N"); answer=in.next(); continue; case 3: System.out.println("抽奖富翁系统----->抽奖"); System.out.println("是否选择继续:Y/N"); answer=in.next(); continue; default: System.out.println("输入错误,请重新输入:"); break; } } while ( answer.equals("Y")); System.out.println("程序结束,退出程序。"); -----------------   
  System.out.println("欢迎使用抽奖富翁系统");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.抽奖");
System.out.println("***");
Scanner in = new Scanner(System.in); String answer = ""; int password = 0; String name = ""; int card = 0; do { System.out.print("请选择菜单:"); int sc = in.nextInt(); switch (sc) { case 1: System.out.println("抽奖富翁系统----->注册"); System.out.println("输入用户名:"); name = in.next(); System.out.println("输入密码:"); password = in.nextInt(); card = (int) (Math.random() * 9000 + 1000); System.out.println("card:" + card); System.out.println("注册成功,请记住你的会员卡号,会员名,密码"); System.out.println("会员卡号\t" + "会员名\t" + "密码"); System.out.println(card + "\t" + name + "\t" + password); System.out.println("是否选择继续:Y/N"); answer = in.next(); continue; case 2: System.out.println("抽奖富翁系统----->登录"); for (int i = 0; i < 3; i++) { System.out.println("请输入你的会员卡号"); int card1 = in.nextInt(); System.out.println("请输入你的会员会员名"); String name1 = in.next(); System.out.println("请输入你的会员名密码"); int password1 = in.nextInt(); if (card1 == card && name1.equals(name) && password1 == password) { System.out.println("登录成功"); break; } if (card1 != card || !name1.equals(name) || password1 != password) { System.err.println("输入错误,请重新输入,你还有:" + (2 - i) + "次机会"); continue; } } System.out.println("是否选择继续:Y/N"); answer = in.next(); continue; case 3: System.out.println("抽奖富翁系统----->抽奖"); System.out.println("是否选择继续:Y/N"); answer = in.next(); continue; default: System.out.println("输入错误,请重新输入:"); break; } } while (answer.equals("Y")); System.out.println("程序结束,退出程序。"); }   
System.out.println("欢迎使用抽奖富翁系统");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.抽奖");
System.out.println("***");
Scanner in = new Scanner(System.in); String answer = ""; int password = 0; String name = ""; int card = 0; do { System.out.print("请选择菜单:"); int sc = in.nextInt(); switch (sc) { case 1: System.out.println("抽奖富翁系统----->注册"); System.out.println("输入用户名:"); name = in.next(); System.out.println("输入密码:"); password = in.nextInt(); card = (int) (Math.random() * 9000 + 1000); System.out.println("card:" + card); System.out.println("注册成功,请记住你的会员卡号,会员名,密码"); System.out.println("会员卡号\t" + "会员名\t" + "密码"); System.out.println(card + "\t" + name + "\t" + password); System.out.println("是否选择继续:Y/N"); answer = in.next(); continue; case 2: System.out.println("抽奖富翁系统----->登录"); for (int i = 0; i < 3; i++) { System.out.println("请输入你的会员卡号"); int card1 = in.nextInt(); System.out.println("请输入你的会员会员名"); String name1 = in.next(); System.out.println("请输入你的会员名密码"); int password1 = in.nextInt(); if (card1 == card && name1.equals(name) && password1 == password) { System.out.println("登录成功"); break; } if (card1 != card || !name1.equals(name) || password1 != password) { System.err.println("输入错误,请重新输入,你还有:" + (2 - i) + "次机会"); continue; } } System.out.println("是否选择继续:Y/N"); answer = in.next(); continue; case 3: System.out.println("抽奖富翁系统----->抽奖"); for (int k = 0; k < 4; k++) { int q=(int)(Math.random()*9000+1000); System.out.println("本期的幸运数组是:"+q); if (q==card) { System.out.println("恭喜你是幸运客户,"); break; } else{ System.out.println("你不是幸运客户,"); continue; } } System.out.println("是否选择继续:Y/N"); answer = in.next(); continue; default: System.out.println("输入错误,请重新输入:"); break; } } while (answer.equals("Y")); System.out.println("程序结束,退出程序。");   到了这里本该结束的,但是有一个bug。抽奖功能的实现的一个前提是登陆成功,登录的前提是注册成功。不然直接选择菜单3.而没有经过1和2.就无法抽奖。弄了一个下午,代码大修,才算修改完善。
  但是里面还有很多重复,System.out.println("是否选择继续:Y/N");
answer = in.next();
if (answer.equals("Y")) {
continue;
}
else {
System.out.println("程序结束,退出程序。");
return;
这是一个问题。
  break;continue;return。用的总是感觉不对。这是第二个问题。
  第三个问题。while(true)无线循环。外面无法再写入代码?这个是个问题吗?要是不是的话?岂不是复杂代码无法应用无限循环了。
  希望大家能帮我解惑,谢谢。