7.35
这个题,挺好理解也挺好想出对应代码方法,就是太繁琐,需要有耐心。。
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
String[] words = {"write","that","arrays"};
guess(words);
}
public static void guess(String[] words){
Scanner input = new Scanner(System.in);
char c = 'y';
while(c=='y'){
play(words);
System.out.print("Do you want to guess another word? Enter y or n >: ");
c=input.next().charAt(0);
while(c!='y'&&c!='n'){
System.out.print("Do you want to guess another word? ENter y or n >:");
c=input.next().charAt(0);
}
}
}
public static void play(String[] words){
Scanner input = new Scanner(System.in);
int l = (int)(Math.random()*words.length);
String word = words[l];
int len = word.length();
char[] letters = new char[len];
for(int i=0;i<len;i++)
letters[i]='0';
int lettersPointer = 0;
int missCount = 0;
int notGuessed = len;
while(notGuessed>0)
{
System.out.print("(Guess) Enter a letter in word "+alter(word,letters)+" > ");
char guessLetter = input.next().charAt(0);
if(belongs(guessLetter,letters))
System.out.println(guessLetter+" is already in the word");
else if(!belongs(guessLetter,word))
{
System.out.println(guessLetter+" is not in the word");
missCount++;
}
else
{
letters[lettersPointer]=guessLetter;
lettersPointer++;
notGuessed-=occurTime(guessLetter,word);
}
}
System.out.println("The word is "+word+". You missed "+missCount+" time(s)");
}
public static String alter(String word , char[] letters){
String r = "";
int len = word.length();
for(int i=0;i<len;i++){
if(belongs(word.charAt(i),letters))
r += word.charAt(i);
else
r += '*';
}
return r;
}
public static boolean belongs(char key,char[] letters){
for (char letter : letters) {
if (letter == key)
return true;
}
return false;
}
public static boolean belongs(char key,String word){
for(int i=0;i<word.length();i++){
if(key==word.charAt(i))
return true;
}
return false;
}
public static int occurTime(char key,String word){
int count=0;
for(int i=0;i<word.length();i++) {
if (key == word.charAt(i))
count++;
}
return count;
}
}
7.36
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
int[] queens = {0,1,2,3,4,5,6,7};
while(conflict(queens))
shake(queens);
for(int i=0;i<8;i++){
for(int j=0;j<queens[i];j++)
System.out.print("| ");
System.out.print("|Q");
for(int j=queens[i]+1;j<8;j++)
System.out.print("| ");
System.out.println("|");
}
}
public static boolean conflict(int[] queens){
int[] plus = new int[8];
int[] minus = new int[8];
for(int i=0;i<8;i++){
plus[i] = i+queens[i];
minus[i] = i-queens[i];
}
for(int i=0;i<8;i++){
if(occurTime(plus[i],plus)>1)
return true;
}
for(int i=0;i<8;i++){
if(occurTime(minus[i],minus)>1)
return true;
}
return false;
}
public static int occurTime(int key,int[] set){
int count=0;
for(int value : set){
if(key == value)
count++;
}
return count;
}
public static void shake(int[] str){
int len = str.length;
for(int i=0;i<len;i++){
int d = (int)(Math.random()*len);
int tmp = str[i];
str[i] = str[d];
str[d] = tmp;
}
}
}
7.37
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of balls to drop: ");
int balls = input.nextInt();
System.out.print("Enter the number of slots in the bean machine: ");
int numSlot = input.nextInt();
int[] slots = new int[numSlot];
for(int i=0;i<balls;i++)
drop(numSlot,slots);
for(int i=max(slots);i>=1;i--){
for(int j=0;j<numSlot;j++){
if(slots[j]<i)
System.out.print(" ");
else
System.out.print("0");
}
System.out.println();
}
}
public static void drop(int numSlot , int[] slots){
double b = 0;
for(int i=0;i<numSlot-1;i++){
if(Math.random()>0.5){
b += 0.5;
System.out.print('R');
}else{
b -= 0.5;
System.out.print('L');
}
}
System.out.println();
if(numSlot%2==1)
slots[(int)(numSlot/2+b)]++;
else{
if(b>0)
slots[(int)(numSlot/2-0.5+b)]++;
else
slots[(int)(numSlot/2+b)]++;
}
}
public static int max(int[] v){
int max = v[0];
for(int value : v){
if(value > max)
max = value;
}
return max;
}
}