我正在研究使用网络概念实现的牛和公牛游戏。服务器和客户端程序都是用java编写的
我的服务器程序是 import java.io。*;
import java.net.*;
import java.util.Random;
import java .util.Scanner;
import java.lang.*;
class Server3{
public static void main(String args[])
throws Exception
{
ServerSocket ss=new ServerSocket(888);
Socket s=ss.accept();
System.out.println("connection established");
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader kb=new BufferedReader(new InputStreamReader(System.in));
String str="Welcome to COWS and BULLS Game";
ps.println(str);
String str1="Rules are :You need to guess a 3 digit number without duplicates";
String str2="There are 2 variables namely Cows and Bulls";
String str3="Cows will give you the count of no of correct int in ur guess and bulls gives the no of correct positions";
String str4="When the count in bulls is 3 you need to enter null";
String str5="now enter a 3 digit number";
ps.println(str1);
ps.println(str2);
ps.println(str3);
ps.println(str4);
ps.println(str5);
Random gen=new Random();
int target=0;
while(hasDupes(target= (gen.nextInt(900) + 100)));
String targetStr = target +"";
boolean guessed = false;
Scanner input = new Scanner(System.in);
int guesses = 0;
do{
int bulls = 0;
int cows = 0;
String n;
n=br.readLine();
guesses++;
String guessStr=n+"";
for(int i= 0;i < 3;i++) {
if(guessStr.charAt(i) == targetStr.charAt(i)) {
bulls++;
}
}
for(int i= 0;i < 3;i++) {
if(targetStr.contains (guessStr.charAt(i)+"")) {
cows++;
}
}
if(bulls == 3) {
guessed = true;
dos.writeBoolean(guessed);
}
else
{
String Cows=cows+"";
String Bulls=bulls+"";
ps.println(Cows);
ps.println(Bulls);
dos.writeBoolean(guessed);
}
} while(!guessed);
String Guesses=guesses+"";
ps.println(Guesses);
}
public static boolean hasDupes(int num){
boolean[] digs = new boolean[10];
while(num > 0){
if(digs[num%10]) return true;
digs[num%10] = true;
num/= 10;
}
return false;
}
}
我的客户端程序的一部分是:
import java.io.*;
import java.net.*;
class Client3
{
public static void main(String args[])
throws Exception
{
Socket s=new Socket("192.168.1.36",888);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(s.getInputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader kb=new BufferedReader(new InputStreamReader(System.in));
String str,str1,str2,str3,str4,str5;
str=br.readLine();
str1=br.readLine();
str2=br.readLine();
str3=br.readLine();
str4=br.readLine();
str5=br.readLine();
System.out.println(str);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
System.out.println(str5);
String Cows,Bulls;
String n;
boolean guessed=false;
do
{
n=kb.readLine();
dos.writeBytes(n+"\n");
Cows=br.readLine();
Bulls=br.readLine();
System.out.println(Cows+"cows and"+Bulls+"bulls");
guessed=br.readBoolean();
} while(!guessed);
String Guesses;
Guesses=br.readLine();
System.out.println("you won after"+Guesses+"guesses");
dos.close();
br.close();
kb.close();
s.close();
}
}
我的服务器需要向我的客户端发送一个布尔值,客户端需要读取服务器发送的布尔值。我不清楚如何从客户端和服务器发送和读取布尔值的概念。
我的客户端错误是
Client3.java:36: error: method readBoolean in class DataInputStream cannot be ap
plied to given types;
dis.readBoolean(guessed);
^
required: no arguments
found: boolean
reason: actual and formal argument lists differ in length
1 error
我不熟悉如何发送和读取布尔值的方法。请帮助我
答案 0 :(得分:0)
可能的原因:
<强> 1 即可。尝试使用变量而不声明它。 &LT; ---
<强> 2 即可。拼写错误的类或方法名称(请记住,Java区分大小写)。
第3 即可。使用的参数与方法的签名不匹配。
<强> 4 即可。使用导入声明未正确引用打包的类。
由于我无法在声明变量br和输出的任何地方找到,因此您的错误是因为未在代码中声明变量br
和output