Java 如何从使用客户端数组的服务器调用字符串?

Java 如何从使用客户端数组的服务器调用字符串?,java,arrays,string,Java,Arrays,String,嘿,我正试图从我的服务器类调用一个字符串,以检查我的客户端中的数组数是否有序,但我很难在我的类中获取字符串返回的值,这是我到目前为止得到的 import java.util.Scanner; public class Main { public static void main(String[] args){ System.out.println("Insert 10 numbers in order from least to greatest");

嘿,我正试图从我的服务器类调用一个字符串,以检查我的客户端中的数组数是否有序,但我很难在我的类中获取字符串返回的值,这是我到目前为止得到的

import java.util.Scanner;


public class Main {
    public static void main(String[] args){
        System.out.println("Insert 10 numbers in order from least to greatest");
        Scanner input = new Scanner(System.in);
        int[] array = new int[10];
        for(int i=0; i<array.length; i++){
            int x = input.nextInt();
        }
    }
}
import java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
System.out.println(“按从最小到最大的顺序插入10个数字”);
扫描仪输入=新扫描仪(System.in);
int[]数组=新的int[10];

对于(inti=0;i您只需从
Main
类创建并调用
Server

import java.util.Scanner;

public class Main {
public static void main(String[] args){
    System.out.println("Insert 10 numbers in order from least to greatest");
    Scanner input = new Scanner(System.in);
    int[] array = new int[10];
    for(int i=0; i<array.length; i++){
        int x = input.nextInt();
        array[i] = x;
    }

    Server server = new Server();
    String response = server.checkFalse(array);
    System.out.println(response);
}
(请注意,我们不需要显式创建
Server
的实例来调用
checkFalse()

公共类服务器{
公共静态字符串checkFalse(int[]数组){
String checker=“错误,数字不符合顺序”;
整数=0;

对于(int i=0;i要使用不同的线程执行这些操作,可以使用DatagramSocket和DatagramPacket发送和接收数据。将integer从客户端发送到服务器的choosen端口,并从服务器侦听端口以接收数据。

客户端和服务器是在同一个VM内的单独线程中运行,还是在同一台机器上的不同VM中运行?如何不同的机器?如果我们知道您需要使用的常量,那么提供一些帮助会更容易。在Eclpiseth上有两个不同的类并排运行。这很有效,非常感谢,但是有没有一种方法可以在不创建新服务器的情况下实现这一点?我的老师肯定会对使用这种方法表示赞赏,因为我们还没有学习过但是。是的,您可以将
checkFalse()
设置为静态方法。我将用一个示例更新答案。因为这看起来像是一个家庭作业,您可能需要阅读:)嗯,这是学校项目的一部分,我只是无法修复这一方面,我在课堂上写的代码忘了把它放在USB上,只是在家里制造了一堆乱七八糟的东西。我了解静态是如何工作的。我只是不停地在两个课堂上捣乱,复制没有静态的版本,因为我的静态版本有太多的错误。哈哈
import java.util.Scanner;

public class Main {
public static void main(String[] args){
    System.out.println("Insert 10 numbers in order from least to greatest");
    Scanner input = new Scanner(System.in);
    int[] array = new int[10];
    for(int i=0; i<array.length; i++){
        int x = input.nextInt();
        array[i] = x;
    }

    Server server = new Server();
    String response = server.checkFalse(array);
    System.out.println(response);
}
public class Main {
    public static void main(String[] args){
        System.out.println("Insert 10 numbers in order from least to greatest");
        Scanner input = new Scanner(System.in);
        int[] array = new int[10];
        for(int i=0; i<array.length; i++){
            int x = input.nextInt();
            array[i] = x;
        }

        String response = Server.checkFalse(array);
        System.out.println(response);
    }
}
public class Server {
    public static String checkFalse(int[] array){
        String checker = "Error, the numbers are not in order";
        int number = 0;

        for(int i =0; i<array.length-1; i++){
            number = array[i];
            if(array[i+1] < number){
                return checker;
            }
        }
        return "The numbers are in order!";  
    }
}