




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
福建工程学院&信息科学与工程学院 实验报告课程名称面向对象程序设计任课教师姓名潘万丁年级15级专业网络工程班级网工1502实验编号实验2实验名称控制结构实验内容:1用户登录业务功能实现用户登录的业务功能:(1)用户名默认为“Java”(不忽略大小写),密码默认为“201606”(忽略大小写)。(2)当用户名或密码输入错误时,输出出错信息,并终止程序。(3)当用户名和密码都输入正确时,输出登录成功的提示信息。public class LoginDemo public static void main(String args) 源代码:package shiyan;import java.util.Scanner;public class shiyan1 public static void main(String args) / TODO 自动生成的方法存根System.out.println(请输入姓名:);Scanner scanner = new Scanner(System.in);String Name = scanner.nextLine();if(Name.equals(Java)System.out.println(请输入密码:);else System.out.println(姓名错误!);String Passwd = scanner.nextLine();if(Passwd.equals(201801)System.out.println(Name+登录成功!);else System.out.println(密码错误);2猜数字游戏实现“猜数字”游戏,其游戏规则为:出题者从0-9之间选取4个不重复的数字,由猜数者进行猜测。每一次猜测,出题者都要给出“nAmB”的提示信息:nA表示本次猜测中有n个与谜底的数字和位置完全相同;mB表示另有m个数字与谜底的数字相同,但位置不同。当得到“4A0B”的结果时,猜数者即获得胜利。功能需求:(1)随机生产4个不重复的0-9的整数。(2)每次猜测都要给出提示信息。(3)设置三个等级的难度供用户选择,“Easy”允许猜12次,“Common”允许猜9次,“Hard”允许猜7次。(4)超出限制次数,用户挑战失败,给出提示信息。(5)用户挑战成功,也应给出提示信息。public class LoginDemo public static void main(String args) 源代码:package shiyan;import java.util.Random;import java.util.Scanner;public class shiyan1 int Nums = new int4; int inputNumsArray = new int4; int difficultyLevel; int difficulty; int aA = 0; int bB = 0; String numberStr = ; String str = ; /* * 生成随机数 */ public int randNums(int n) for (int i = 0; i Nums.length; i+) Random ran = new Random(); int a = ran.nextInt(10); if (i - 1 != -1) for (int j = 0; j i; j+) if (a = Numsj) i-; break; else Numsi = a; else Numsi = a; return Nums; /* * 选择游戏难度 */ public int selectLevel() / 接受一个数字 / 1:Easy 可以猜12次 / 2:Common 可以猜9次 / 3:Hard 可以猜7次 Scanner scan = new Scanner(System.in); System.out .println(请选择难度系数(输入数字),1:Easy 可以猜12次;2:Common 可以猜9次;3:Hard 可以猜7次); difficulty = scan.nextInt(); switch (difficulty) case 1: difficultyLevel = 12; break; case 2: difficultyLevel = 9; break; case 3: difficultyLevel = 7; break; default: break; return difficultyLevel; /* * 接受用户输入的数字 */ public int inputNums(int n) Scanner scan = new Scanner(System.in); int b = scan.nextInt(); for (int i = 0; i inputNumsArray.length; i+) int c = (int) (int) b / Math.pow(10, 3 - i); inputNumsArrayi = c; b = (int) (b - c * Math.pow(10, (3 - i); return inputNumsArray; /* * 数字比对的方法 */ public String compare(int answer, int inputs) for (int i = 0; i answer.length; i+) if (inputsi = answeri) aA += 1; continue; else for (int j = 0; j answer.length; j+) if (inputsi = answerj) bB += 1; str = + aA + A + bB + B ; return str; /* * 整个游戏过程代码 */ public void play() randNums(4); for (int i = 0; i Nums.length; i+) numberStr = numberStr + Numsi; selectLevel(); System.out.println(你选择了难度系数: + difficulty + 共有: + difficultyLevel + 次机会。); for (int i = 0; i difficultyLevel; i+) inputNums(4); int chanceNums = difficultyLevel - i - 1; compare(Nums, inputNumsArray); if (aA != 4) if (chanceNums = 0) System.out.println(机会用完了,答案是: + numberStr); break; else System.out.println(str + 你还有 + chanceNums + 次机会); aA = 0
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论