我有一个客户端类,其中值“number”应该始终相应地更改为用户输入,但在每次循环之后它保持与第一个条目相同,例如:用户输入 F 下一个循环它将打印 F 而不是任何内容但。我尝试使用私有(private)方法创建它的新实例,但我不断收到枚举错误,我不知道如何处理。
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();
}
错误是“Error:(43, 17) java: class,interface,or enum Expected”,它将光标放在私有(private)对象中的 void 之后
然后“错误:(46, 9) java:需要类、接口(interface)或枚举”,突出显示私有(private)对象中的括号
最佳答案
将你的方法移到类中:-
} // This closes main
//} // This was closing the Class Client
private void resetVariable() {
Client = new Client();
} // This closes resetVariable
} // This should now close Class Client
更新:来自下面的评论 - 您还应该考虑从成员函数 resetVariable
实例化类 Client
>
关于java - 重置循环中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44661801/