循环中重置变量

时间:2017-06-20 19:46:17

标签: java loops

我有一个客户端类,其中值“number”应该总是相应地改变为用户输入但是在每个循环之后它保持与​​第一个条目相同,例如:用户输入F下一个循环它将打印F而不是任何东西但。我尝试使用私有方法创建它的新实例,但我不断得到枚举错误,我不知道如何处理。

public class Client {

public static void main(String args[]) throws UnknownHostException, IOException {
    boolean test = true;
    Socket s = new Socket("127.0.0.1", 6115);
    Scanner sc = new Scanner(System.in);
    Scanner sc1 = new Scanner(s.getInputStream());
    PrintStream p = new PrintStream(s.getOutputStream(), true);
    //String number;
    while (test) {
        String number;
        number = "";
        System.out.println("Enter any string");
        number = sc.nextLine();
        p.println(number);

        //flushing printstream not variable?
        number = sc1.nextLine();
        System.out.println("CALLED");
        System.out.println(number);
        p.flush();
        System.out.println("Would you like to enter another string? Y/N: ");
        String cont1 = sc.nextLine();
        if (cont1.equals("N")) {
            test = false;
            System.out.println("Goodbye! ");
        }
    }
    s.close();
    sc.close();
    sc1.close();
}
}
private void resetVariable() {
    Client = new Client();

}

错误是“错误:(43,17)java:class,interface或enum expected”将光标放在私有对象的void之后

然后“错误:(46,9)java:class,interface或enum expect”突出显示私有对象中的括号

1 个答案:

答案 0 :(得分:1)

将您的方法移到类中: -

    } // This closes main
    //} // This was closing the Class Client
    private void resetVariable() {
        Client = new Client();

    } // This closes resetVariable
  } // This should now close Class Client

更新:通过以下评论 - 您还应该考虑从成员函数Client实例化resetVariable