java學習之猜數(shù)字小游戲
今天主要學習了一些初級的設計,完成了這個猜數(shù)字的小游戲,其是也算不上是什么游戲,因為我答案都給出來了。當然也是想對代碼更加熟練的操作,讓自己能夠得心應手。
這個小程序中讓我花了點時間的主要是那個如何去重的問題,當時也是思考良久,后來才考慮到使用死循環(huán)讓隨機數(shù)產(chǎn)生直到不重復為止,其他感覺也還好。
import java.util.Scanner;
public class GuessingGames {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
char[] chs=generate();
System.out.println(chs);
int score=500;
while(true) {
System.out.println("請猜猜看!");
String str=in.next();
char[] input=str.toCharArray(); //將用戶輸入的字符串轉(zhuǎn)換為數(shù)組
int[] result=compare(chs, input);
if (result[1]==chs.length) {
System.out.println("恭喜你,猜對了!!!你獲得的分數(shù)為"+score+"猜錯次數(shù)為:"+(500-score)/10);
break;
}else {
System.out.println("字符猜對個數(shù)為:"+result[0]+","+"位置猜對為:"+result[1]);
score-=10;
}
}
}
// 隨機生成字母
public static char[] generate() {
char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
char[] chs = new char[5];
char copy;
for (int i = 0; i < chs.length; i++) {
chs[i] = letters[(int) (Math.random() * 26)];
copy=letters[(int) (Math.random()*26)];
for (int j = 0; j < i; j++) {
if (chs[i]==chs[j]) {
for(;;) {
copy=letters[(int) (Math.random()*26)];
if (chs[i]!=copy) {
chs[i]=copy;
break;
}
}
}
}
}
return chs;
}
//完成兩個數(shù)組的對比
public static int[] compare(char[] chs,char[] input) {
int[] score=new int[2];
for (int i = 0; i < input.length; i++) {
for (int j = 0; j < chs.length; j++) {
if (input[i]==chs[j]) {
score[0]++;
if (i==j) {
score[1]++;
}
break;
}
}
}
return score;
}
}
小編再為大家分享一段代碼:Java控制臺猜數(shù)字小游戲:
import java.util.*;
/**
*控制臺猜數(shù)字小游戲,系統(tǒng)自動生成范圍為1-100的數(shù)字
*@version 1.0 2018-01-04
*@author jie1024(wechat:wxxueba)
*/
public class GuessX{
public static void main(String[] args){
Random random = new Random();//創(chuàng)建random
int x = random.nextInt(100)+1;//生成一個1-100之間的隨機數(shù) random.nextInt(100)的范圍為0-99,所以+1,范圍為1-100
System.out.println("系統(tǒng)已自動為您生成了一個隨機數(shù)(范圍為1-100),游戲開始!");
System.out.print("猜猜這個數(shù)字是多少吧:");
Scanner in = new Scanner(System.in);//創(chuàng)建scanner
int y = in.nextInt(); //輸入數(shù)字
int count = 1; //次數(shù)
while(y != x){
count ++;
if(y<1 || y>100){
System.out.print("Sorry,你猜的數(shù)字不在范圍之內(nèi)(范圍為1-100),請再重新猜一次吧:");
y = in.nextInt();
}
else if (y>x){
System.out.print("Sorry,你輸入的數(shù)字太大了,請再重新猜一次吧:");
y = in.nextInt();
}
else if (y<x){
System.out.print("Sorry,你輸入的數(shù)字太小了,請再重新猜一次吧:");
y = in.nextInt();
}
}
System.out.println("恭喜你,猜對了!你猜的數(shù)字是" + y + "你總共猜了" + count + "次!");
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
springboot解決Class path contains multiple 
這篇文章主要介紹了springboot解決Class path contains multiple SLF4J bindings問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
使用Jenkins Pipeline自動化構建發(fā)布Java項目的方法
這篇文章主要介紹了使用Jenkins Pipeline自動化構建發(fā)布Java項目的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
Java類的繼承實例詳解(動力節(jié)點Java學院整理)
在Java開發(fā)中,我們常常用到繼承這一概念,可以說繼承是Java這類面向?qū)ο缶幊陶Z言的基石,今天小編一起和大家一起學習java類的繼承2017-04-04
springboot整合springsecurity與mybatis-plus的簡單實現(xiàn)
Spring Security基于Spring開發(fā),項目中如果使用Spring作為基礎,配合Spring Security做權限更加方便,而Shiro需要和Spring進行整合開發(fā)。因此作為spring全家桶中的Spring Security在java領域很常用2021-10-10

