从方法返回多个值 [英] Returning multiple values from a method

查看:85
本文介绍了从方法返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法 drive 像这样:

public double drive(double milesTraveled, double gasUsed)
{
    gasInTank -= gasUsed;
    return totalMiles += milesTraveled;
}

我知道我不能从一个方法返回多个值,但这是我需要做的,因为我需要在我的main方法中同时使用这两个值,因为现在显然只返回一个值.我想不出任何可行的方法.抱歉,这是一个超级初学者的问题.我该怎么做才能从方法中返回两个值?

I know I can't return multiple values from a method, but that's kind of what I need to do because I need both of these values in my main method, and as it is now it's obviously only returning the one. I can't think of anything that would work. Sorry if this is a super beginner question. What can I do to get both values to return from the method?

推荐答案

在大多数语言中,您只能从方法中返回单个值.该单个值可以是复杂类型,例如结构,数组或对象. 某些语言还允许您定义输出参数或将指针或引用传递到外部存储位置.这些类型的参数还允许您从方法中返回其他值.

In most languages you can only return a single value from a method. That single value could be a complex type, such as a struct, array or object. Some languages also allow you to define output parameters or pass in pointers or references to outside storage locations. These kinds of parameters also allow you to return additional values from your method.

这篇关于从方法返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆