实训一
阿超的课都是下午两点钟,这时班上不少的同学都昏昏欲睡,为了让大家兴奋起来,阿超让同学玩一个叫“黄金点”的游戏:
设计思路:进入循环输入玩家 输入数值计算出G值在运用 循环得出结果
码云地址:https://gitee.com/gs717/codes/yk98sm1zti42ud6wvcxhq64
部分代码如下:
private static void playGame() throws InterruptedException {
System.out.println("****游戏开始****");
for (int i = 0; i < persons.length; i++) {
Scanner scanner = new Scanner(System.in
System.out.println("****游戏开始****");
for (int i = 0; i < persons.length; i++) {
Scanner scanner = new Scanner(System.in
);
System.out.print("请" + persons[i].getName() + "输入数值(大于0小于100的有理数):");
persons[i].setNum(scanner.nextInt());
}
int numG = getG();
int recordMax = 0;
int max = Math.abs(persons[0].getNum() - numG);
for (int i = 0; i < persons.length; i++) {
if (max < (Math.abs(persons[i].getNum() - numG))) {
max = Math.abs(persons[i].getNum() - numG);
recordMax = i;
}
}
int min = Math.abs(persons[0].getNum() - numG);
int recordMin = 0;
for (int i = 0; i < persons.length; i++) {
if (min > Math.abs(persons[i].getNum() - numG)) {
min = Math.abs(persons[i].getNum() - numG);
recordMin = i;
}
}
System.out.println("******************************");
System.out.println("相信信息如下:");
persons[recordMin].setScore(persons[recordMin].getScore()+n);
persons[recordMax].setScore(persons[recordMax].getScore()-2);
showMessage();
System.out.println("G值为:" + numG);
System.out.println("gs" + persons[recordMin].getName());
System.out.println("玩家:" + persons[recordMax].getName() + "输了");
System.out.println("******************************");
}
private static int getG() {
int sum = 0;
for (int i = 0; i < persons.length; i++) {
sum += persons[i].getNum();
}
return (int) ((sum / persons.length) * 0.618);
}
private static void showMessage() {
System.out.println();
System.out.println("姓名" + "\t" + "输入的数"+"\t"+"成绩");
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i].getName() + "\t" + persons[i].getNum()+"\t"+persons[i].getScore());
}
System.out.println();
System.out.print("请" + persons[i].getName() + "输入数值(大于0小于100的有理数):");
persons[i].setNum(scanner.nextInt());
}
int numG = getG();
int recordMax = 0;
int max = Math.abs(persons[0].getNum() - numG);
for (int i = 0; i < persons.length; i++) {
if (max < (Math.abs(persons[i].getNum() - numG))) {
max = Math.abs(persons[i].getNum() - numG);
recordMax = i;
}
}
int min = Math.abs(persons[0].getNum() - numG);
int recordMin = 0;
for (int i = 0; i < persons.length; i++) {
if (min > Math.abs(persons[i].getNum() - numG)) {
min = Math.abs(persons[i].getNum() - numG);
recordMin = i;
}
}
System.out.println("******************************");
System.out.println("相信信息如下:");
persons[recordMin].setScore(persons[recordMin].getScore()+n);
persons[recordMax].setScore(persons[recordMax].getScore()-2);
showMessage();
System.out.println("G值为:" + numG);
System.out.println("gs" + persons[recordMin].getName());
System.out.println("玩家:" + persons[recordMax].getName() + "输了");
System.out.println("******************************");
}
private static int getG() {
int sum = 0;
for (int i = 0; i < persons.length; i++) {
sum += persons[i].getNum();
}
return (int) ((sum / persons.length) * 0.618);
}
private static void showMessage() {
System.out.println();
System.out.println("姓名" + "\t" + "输入的数"+"\t"+"成绩");
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i].getName() + "\t" + persons[i].getNum()+"\t"+persons[i].getScore());
}
System.out.println();
}
实验现象如下:
}
}
小结:
通过这段时间,我从学习过程中学到了很多,在我的组长的耐心帮助下,也成功的解决了很多问题,我的组长也真的是一个很优秀的人,并且在这次实训中我懂得了做事情要有耐心,要认真,不会的地方要虚心求教。
非常感谢我的组长啊!!!