標籤:ann 計數 接收 16px generate 一段 開關 設計 實現
day05-java-(方法,猜字元小遊戲)
1.方法:
1)用於封裝一段特定的邏輯功能
2)方法應儘可能的獨立,只幹一件事
3)方法可以被反覆的調用多次
4)避免代碼重複,有利於代碼的維護,有利於團隊的協作開發
2.方法的定義:
修飾詞 傳回值類型 方法名(參數列表){
方法體
}
3.方法的調用:
1)無傳回值: 方法名(有參傳參);
2)有傳回值: 資料類型 變數 = 方法名(有參傳參);
方法名(有參傳參);------不建議
4.return的用法:
1)return 值; //1.結束方法的執行 2.返回結果給調用方
2)return; //1.結束方法的執行
猜字元小遊戲:
一.設計資料結構:資料
1)char[] chs; //隨機字元數組
2)char[] input; //使用者輸入的字元數組
3)int[] result; //對比的結果
4)int score; //得分
int count; //猜錯的次數
二.設計程式結構:方法
1)主方法:
public static void main(String[] args){
//...
}
2)產生隨機字元數組:
public static char[] generate(){
char[] chs = new char[5];
//...
return chs;
}
3)對比:隨機字元數組與使用者輸入的字元數組
public static int[] check(char[] chs,char[] input){
int[] result = new int[2];
//...
return result;
}
三.設計演算法:方法體
String str = "abcde";
1)char[] a = str.toCharArray(); //轉換為字元數組
2)str = str.toUpperCase(); //轉為大寫字母
str = str.toLowerCase(); //轉為小寫字母
3)if(str.equals("ABCDE")){ //判斷str內容是否是ABCDE
}
實現代碼三塊功能:
1.擷取隨機產生的字元數組chs
2.讓使用者猜吧,接收使用者輸入的字元數組input
3.對比chs與input,擷取結果並顯示結果
若沒猜對,則重複第2步開始
方法可以有傳回值也可以沒有傳回值
無傳回值----傳回值類型void
有傳回值----傳回值類型寫成確切的資料類型
System.out.println("HelloWorld");
System.arraycopy(a,1,a1,0,4);
Arrays.sort(arr); //無傳回值
int a = scan.nextInt();
double b = scan.nextDouble();
double c = Math.random();
double d = Math.sqrt(25);
int[] a1 = Arrays.copyOf(a,6); //有傳回值
質數=素數:只能被1和它本身整除的數
是質數?-------取餘所有都不得0
不是質數?-----只要有得0的
5是質數
5%2/3/4--------------都不得0
7是質數
7%2/3/4/5/6----------都不得0
8不是質數
8%2/3/4/5/6/7--------有得0
9不是質數
9%2/3/4/5/6/7/8------有得0
平方根:
sqrt(35)------想求誰的平方根就求誰的平方根
100的平方根為10
25的平方根為5
81的平方根為9
方法的示範:
package day06;//方法的示範public class MethodDemo { public static void main(String[] args) { //say(); //sayHi(); //編譯錯誤,有參則必須傳參 //sayHi(25); //編譯錯誤,參數類型不符 //sayHi("zhangsan"); //String name="zhangsan" //sayHi("lisi"); //String name="lisi" //sayHi("wangwu"); //String name="wangwu" //int a = getNum(); //getNum()的值就是return後的那個值 //System.out.println(a); //double b = plus(5.0,6.0); //double num1=5.5,double num2=6.0 //System.out.println(b); //11.5 double c=5.5,d=6.0; double e = plus(c,d); //double num1=5.5,double num2=6.0 //a(); //方法的嵌套調用 System.out.println("over"); } //有參有傳回值 public static double plus(double num1,double num2){ double num = num1+num2; return num; //返回的是num中的那個數 //return num1+num2; } //無參有傳回值 public static int getNum(){ //return; //編譯錯誤,必須返回一個值 //return 8.88; //編譯錯誤,傳回值類型必須匹配 return 88; //1.結束方法的執行 2.返回結果給調用方 } //有參無傳回值 public static void sayHi(String name){ System.out.println("大家好,我叫"+name); return; //1.結束方法的執行 } //無參無傳回值 public static void say(){ System.out.println("大家好,我叫Freddy"); } public static void a(){ System.out.println(111); b(); System.out.println(222); } public static void b(){ System.out.println(333); }}
猜字元小遊戲:
package day06;import java.util.Scanner;//猜字元遊戲public class Guessing { //主方法 public static void main(String[] args) { Scanner scan = new Scanner(System.in); char[] chs = generate(); //擷取隨機字元數組 System.out.println(chs); //作弊 int count = 0; //猜錯的次數 while(true){ //自造死迴圈 System.out.println("猜吧!"); String str = scan.next().toUpperCase(); //擷取使用者輸入的字串並轉換為大寫字母 if(str.equals("EXIT")){ //判斷str的內容是否是EXIT System.out.println("下次再來吧!"); break; } char[] input = str.toCharArray(); //將字串轉換為字元數組 int[] result = check(chs,input); //對比:隨機字元數組與使用者輸入的字元數組 if(result[0]==chs.length){ //位置對個數為5,意味著猜對了 int score = 100*chs.length-10*count; //一個字元100分,猜錯一次扣10分 System.out.println("恭喜你,猜對了! 總得分為:"+score); break; }else{ //猜錯了 count++; //猜錯次數增1 System.out.println("字元對個數為:"+result[1]+",位置對個數為:"+result[0]); } } } //產生隨機字元數組 public static char[] generate(){ char[] chs = new char[5]; 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‘ }; //隨機字元範圍數組 boolean[] flags = new boolean[letters.length]; //開關數組(與letters數組相對應) for(int i=0;i<chs.length;i++){ //遍曆隨機字元數組 int index; do{ index = (int)(Math.random()*letters.length); //產生0到25之間的隨機數 }while(flags[index]==true); //當index下標對應的開關為true時,表示已存過,則重建index下標 //當index下標對應的開頭為false時,表示未存過,則index下標可用,迴圈結束 chs[i] = letters[index]; //基於index下標到letters中擷取字元,並賦值給chs中的每一個元素 flags[index] = true; //將index下標對應的開關修改為true,表示已存過 } return chs; } //對比:隨機字元數組與使用者輸入的字元數組 public static int[] check(char[] chs,char[] input){ int[] result = new int[2]; //0,0---result[0]為位置對,result[1]為字元對 for(int i=0;i<chs.length;i++){ //遍曆隨機字元數組 for(int j=0;j<input.length;j++){ //遍曆使用者輸入的字元數組 if(chs[i]==input[j]){ //字元對 result[1]++; //字元對個數增1 if(i==j){ //位置對 result[0]++; //位置對個數增1 } break; //input中剩餘字元不再比較了 } } } return result; }}
day05-java-(方法,猜字元小遊戲)