import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException
import java.util.ArrayList
import java.util.Random
import java.util.Scanner
public class Words {
private static char[] consonant={'B','C','D','F','G','H','J','K','L','M','N','P','Q',
'R','S','T','V','W','X','Y','Z'}
private static char[] vowel={'A','E','I','O','U'}
private static ArrayList<String>dict = new ArrayList<String>()
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
getResource()
int score1 = 0
int score2 = 0
Scanner userInput = new Scanner(System.in)
String A= null
int count = 0
boolean flag = false//true 为Player1, FALSE为Player2
do{
String random = randomString()
System.out.println(random)
if(flag)
{
System.out.println("Player1 input a word: (or ‘@’ to pass or ‘!’ to quit)")
A=userInput.nextLine()
if(A.equals("@") )
{
flag =!flag
System.out.println("Player2 input a word: (or ‘@’ to pass or ‘!’ to quit)")
A =userInput.nextLine()
}
}
else
{
System.out.println("Player2 input a word: (or ‘@’ to pass or ‘!’ to quit)")
A=userInput.nextLine()
if(A.equals("@") )
{
flag = !flag
System.out.println("Player1 input a word: (or ‘@’ to pass or ‘!’ to quit)")
A =userInput.nextLine()
}
}
if(A.equals("@"))
{
System.out.println("反复切换,系统退出")
return
}
else
{
String temp = null
if(flag)
{
//String random = randomString()
temp = match(random,A)
if(find(temp,dict))
score1+=2
System.out.println(" Player1 get "+score1+" points")
}
else
{
//String random = randomString()
temp = match(random,A)
if(find(temp,dict))
score2+=2
System.out.println(" Player2 get "+score2+" points")
}
}
count++
}while(count <10&&(!A.equals("!")))
System.out.println("游戏结束!")
System.out.println("Player1 get"+score1+"points")
System.out.println("Player2 get"+score2+"points")
if(A.equals("!") )
System.out.println("exit")
}
public static String randomString()//产生随机字符串
{
Random generator = new Random()
String temp = new String()
for(int i = 0i<8i++)
temp += consonant[generator.nextInt(20)+1]
temp+=vowel[generator.nextInt(4)+1]
temp+=vowel[generator.nextInt(4)+1]
return temp
}
public static String match(String str1,String str2)//
{
if(str1.contains(str2))
{
System.out.println("Bingo,you get it!")
return str2
}
else
{
String temp = new String()
L1: for(int i = 0i<str2.length()i++)
{
for(int j = 0j<str1.length()j++)
if(str2.charAt(i)==str1.charAt(j))
{
temp += str2.charAt(i)
continue L1
}
}
return temp
}
}
public static boolean find(String str,ArrayList<String>soure)//从读取的信息中查找输入的字符串
{
for(int i = 0i <soure.size()i++)
{
if(soure.get(i).equals(str))
{
System.out.println("Find It ! You can get two points")
return true
}
}
return false
}
public static void getResource() throws IOException//读取words.txt文件
{
FileReader fin = new FileReader("words.txt")
BufferedReader bin = new BufferedReader(fin)
String str = null
do{
str = bin.readLine()
if(str!=null)
dict.add(str)
}while(str!=null)
bin.close()
fin.close()
}
}
/*题目:*
* java程序编写:有100匹马,驮100担货,大马驮3担,中马驮2担,两匹小马驮1担,问有大、中、小马各多少?
*
* 编程思路:
*
* 假设: 大马 x个 中马 y 个 小马 (100-x-y) 个
* 那么 3*x + 2*y + (100-x-y)/2 = 100
*
* 并且 x>=0 y>=0 (100-x-y)>=0[即 x+y<=100]
* 另外 两匹小马驮1担, 说明 小马必须是双数
*
* 列出所有可能性并输出 多有满足条件的情况即可
* */
public class Demo
{
public static void main(String [] args)
{
getResult()
}
private static void getResult()
{
int count = 0
System.out.println("满足条件的情况有:")
for (int x = 0x<=100x++)
{
for(int y = 0y<=100y++)
{
int z = 100-x-y
if( z%2==0&&3*x + 2*y + z/2 == 100)
{
count++
System.out.println("情况"+count+":大马"+x+"个,"+"中马"+y+"个"+",小马"+z+"个")
}
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)