通过控制台测试输出的正确方法是什么。我正在托管服务器和客户端来测试Collatz猜想输出,并且不希望计算机浪费时间重复先前解决的方程式。
目前,控制台输出一组如下
1,0
2,1
3,7
4,2
5,5
6,8
7,16
8,3
(continuing on)
当它达到8,3时怎么能让它停止?
这是服务器的代码,希望不需要它。
import java.net.*;
import java.util.*;
import java.io.*;
public class Server {
public static void main(String[] args) {
new Server();
}
public Server() {
try {
ServerSocket sSocket = new ServerSocket(5000);
System.out.println("Server started at: " + new Date());
while(true) {
Socket socket = sSocket.accept();
ClientThread cT = new ClientThread(socket);
new Thread(cT).start();
}
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
}
class ClientThread implements Runnable {
Socket threadSocket;
public ClientThread(Socket socket) {
threadSocket = socket;
}
public void run() {
try {
PrintWriter output = new PrintWriter(threadSocket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(threadSocket.getInputStream()));
output.println("You have connected at: " + new Date());
while(true) {
String chatInput = input.readLine();
System.out.println(chatInput);
}
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
}
}
}
这是客户的代码
import java.io.*;
import java.math.BigInteger;
import java.net.*;
import java.util.Scanner;
public class Client
{
public static void main(String[] args)
{
new Client();
}
public Client()
{
Scanner scanner = new Scanner(System.in);
try {
Socket socket = new Socket("localhost",5000);
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputString = input.readLine();
System.out.println(inputString);
boolean start =true;
int startnumber = 1;
while(true) {
int k=0;
int stepnumber = 0;
BigInteger i = BigInteger.valueOf(startnumber);
while(i.compareTo(BigInteger.ONE) > 0){
if(i.testBit(0)){
stepnumber +=1;
i = i.multiply(BigInteger.valueOf(3));
i = i.add(BigInteger.ONE);
k++;
}
else {
stepnumber +=1;
i = i.divide(BigInteger.valueOf(2));
k++;
}
}
startnumber+=1;
int finalstartnumber = startnumber -1;
output.println(finalstartnumber + ","+ stepnumber + " ");
}
} catch (IOException exception) {
System.out.println("Error: " + exception);
}
}
}