Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以将数字数据类型转换为字符串以在Java中输出?_Java_String_Methods_Return_Numeric - Fatal编程技术网

是否可以将数字数据类型转换为字符串以在Java中输出?

是否可以将数字数据类型转换为字符串以在Java中输出?,java,string,methods,return,numeric,Java,String,Methods,Return,Numeric,我是一个初学者程序员,希望调整一个项目。我没有包括代码,因为我不知道从哪里开始。基本上,我正在捕获不同机票类别的输入。例如:1类为头等舱,2类为商务舱,3类为经济舱。然后,通过if-else语句运行输入,以根据类别确定每张票证的成本。在计算出价格并将其传递给另一个方法进行折扣率计算后,我希望在输出窗口中显示类类型 为了进一步澄清,它会这样说,(name+“您的类类型是:”+classType+“您的折扣价格是:”+discountPrice+“您的最终价格是:”+finalPrice)…加上所有

我是一个初学者程序员,希望调整一个项目。我没有包括代码,因为我不知道从哪里开始。基本上,我正在捕获不同机票类别的输入。例如:1类为头等舱,2类为商务舱,3类为经济舱。然后,通过if-else语句运行输入,以根据类别确定每张票证的成本。在计算出价格并将其传递给另一个方法进行折扣率计算后,我希望在输出窗口中显示类类型


为了进一步澄清,它会这样说,(name+“您的类类型是:”+classType+“您的折扣价格是:”+discountPrice+“您的最终价格是:”+finalPrice)…加上所有的格式优雅。我希望classType显示实际的单词,而不仅仅是“1”、“2”或“3”。我至少能够捕获输入并分配价格,然后进行计算。我只是希望这样做之后,我可以返回一个字符串值,而不是数字类型。对于那些提供帮助的人,非常感谢,请记住我是一个noob,没有机会学习数组和其他更复杂的东西。

由于这是一个足够简单的场景,您可以执行以下操作:

int typeNum; // This is what holds 1, 2, 3 etc for the type of ticket
             // 0 is used, so we'll just put a "NONE" as the string for it
String classType[] = {"NONE", "First Class", "Business", "Economy"};
...
System.out.println(name + 
                   "Your class type is: " + classType[typeNum] + 
                   "Your discount price is: " + discountPrice + 
                   "Your finalPrice is:" + finalPrice);
执行类型到字符串映射(通常只使用类型)的“正确方法”是使用
enum

更新):根据要求,无需阵列即可完成此操作:

int typeNum; // Still the int that should be either 1, 2, or 3 for type of ticket
...

String classType;

if ( typeNum == 1 ) {
    classType = "First Class";
} else if ( typeNum == 2 ) {
    classType = "Business";
} else if ( typeNum == 3 ) {
    classType = "Economy";
} else {
    classType = "(Unrecognized Ticket Type)";
}

System.out.println(name + 
                   "Your class type is: " + classType + 
                   "Your discount price is: " + discountPrice + 
                   "Your finalPrice is:" + finalPrice);

您的类类型似乎是
enum
的理想候选。例如:

public enum ClassType {

    FIRST_CLASS(1, "1st Class"),
    BUSINESS(2, "Business"),
    ECONOMY(3, "Economy");

    private final int code;
    private final String name;

    private ClassType(int code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public static ClassType getByCode(int code) {
        for (ClassType classType : ClassType.values()) {
            if (classType.code == code) {
                return classType;
            }
        }
        throw new IllegalArgumentException();
    }
}

如果您还没有了解enum,那么一个很好的起点是。

您希望1打印为1,100打印为100吗?这是你的问题吗?是的,1号被分配到头等舱。因此,我希望显示的不是classType的“1”,而是“first class”。+1对于一个低技术的解决方案,我的第一个想法是切换/分配,但数组更聪明。现在请耐心听我说。我不太理解分配给字符串的类类型[]……这是数组还是我真的那么新鲜?我只关注于使用多个方法,调用它们并返回一个值以显示在主方法中。我可以从用于为类类型分配价格的方法返回字符串和值吗?@BenignBaboon-Yup,
classType
是字符串数组。例如,
classType[1]
将是
“第一类”
classType[2]
将是“业务”,等等。@BenignBaboon您不能返回字符串和值-因为这将试图返回2个变量,而Java只允许您从函数返回1个变量。此外,该函数声明为
公共静态双类类型价格(…
,因此它必须返回一个
double
。但是,在返回值之后,您可以将其转换为字符串-因此这不是问题。我确信这会很好地工作,我感谢您花时间,但是,我想知道是否有一种方法可以在不使用数组的情况下执行此操作。我自己可能想得太远了。