找到你要的答案

Q:How to assign one value to multiple variables [closed]

Q:如何分配一个值到多个变量[闭]

I want to receive one value, which represents multiple variables. for example I receive 110200john This value goes directly without any code to multiple variables like

int x = 11
double y = 0200
string name = john

How can I do that ?

Can I use enum

enum data {
 int x ;
double y ;
string name ;

}

Also I am receiving the value in byte format.

Thank you for your help guys

I want to receive one value, which represents multiple variables. for example I receive 110200john This value goes directly without any code to multiple variables like

int x = 11
double y = 0200
string name = john

我该怎么做?

我可以使用枚举

enum data {
 int x ;
double y ;
string name ;

}

我也收到字节格式的值。

谢谢你们的帮助

answer1: 回答1:

You should almost certainly create a class to represent those three values together, if they're meaningful. I'd personally then write a static parse method. So something like:

public final class Person {
    private final int x;
    private final double y;
    private final String name;

    public Person(int x, double y, String name) {
        this.x = x;
        this.y = y;
        this.name = name;
    }

    public static Person parse(String text) {
        int x = Integer.parseInt(text.substring(0, 2));
        double y = Double.parseDouble(text.substring(2, 6));
        String name = text.substring(6);
        return Person(x, y, name);
    }

    // TODO: Getters or whatever is required
}

This assumes that the format of your string is always xxyyyyname - basically you should adjust the parse method to suit, using substring and the various other parse methods available.

你应该几乎可以创建一个类来代表这三个值在一起,如果他们是有意义的。然后我自己写一个静态解析方法。所以像:

public final class Person {
    private final int x;
    private final double y;
    private final String name;

    public Person(int x, double y, String name) {
        this.x = x;
        this.y = y;
        this.name = name;
    }

    public static Person parse(String text) {
        int x = Integer.parseInt(text.substring(0, 2));
        double y = Double.parseDouble(text.substring(2, 6));
        String name = text.substring(6);
        return Person(x, y, name);
    }

    // TODO: Getters or whatever is required
}

这是假设你的字符串的格式是xxyyyyname -基本上你应该调整分析方法相适应,使用字符串和其他各种解析方法。

java