if (s.endsWith("say $+$getnumber")){Random random = new Random();
randomNumber = random.nextInt(100);
dos.writeUTF("产生了一个100以内的数");
}

解决方案 »

  1.   

    一个猜字游戏:
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.util.*;import javax.swing.JTextArea;class ReadThread extends Thread{
    JTextArea ta;
    int randomNumber=-1;
    DataInputStream dis;
    DataOutputStream dos;public ReadThread(JTextArea t,DataInputStream d,DataOutputStream dos){
    this.ta = t;
    this.dis =d;
    this.dos = dos;
    }public void run(){
    int i=0;
    String s="";
    try{
    while(true){
    s =dis.readUTF();if (s.endsWith("say $+$getnumber")){Random random = new Random();
    randomNumber = random.nextInt(100);
    dos.writeUTF("产生了一个100以内的数");
    }
    if (!s.endsWith("太大了")&&
    !s.endsWith("太小了")&&
    !s.endsWith("产生了一个100以内的数")&&
    !s.endsWith("不是一个数字")&&
    !s.endsWith("say $+$getnumber")){
    System.out.println(s);
    try{
    int a = s.indexOf("say ");
    String ss = s.substring(a+4,s.length());
    i=Integer.parseInt(ss);
    }
    catch(NumberFormatException e){
    dos.writeUTF("不是一个数字");
    }if (i!=randomNumber){
    if(i>randomNumber){
    dos.writeUTF(i+"太大了");
    }else{
    dos.writeUTF(i+"太小了");
    }
    }else{
    dos.writeUTF(i+"对了");
    }}
    ta.append("I say: "+s); //accept the msg
    ta.append("\n");

    }
    catch(IOException e){
    System.out.println("conncttiong error");


    public int getRandomNumber(){
    return randomNumber;
    }}
      

  2.   

    String s = "test" + "say $+$getnumber";
    System.out.println(s.endsWith("say $+$getnumber"));以上代码输出为:true
    这不就是说测试s是否以say $+$getnumber结尾吗?