什么代码错误?我正在使程序输出最大数量为3

时间:2018-01-20 03:30:13

标签: c++ function

我试图调用一个函数将它作为变量名最大存储,然后输出它。感谢帮助。

我知道这是一个简单的问题,但我是新手,所以很难。

#include <iostream>

using namespace std;

int find_max(int x, int y, int z);
{


    if((x >= y) && (x>= z)){
        largest = x;
    }
    if((y >= x) && (y>= z)){
        largest = y;
    }
    if((z >= x) && (z>= y)){
        largest = z;
    }
        return largest;
}

int main()
{
int num1, num2, num3, largest;

cout << "Please enter three numbers: ";
cin >> num1 >> num2 >> num3;
find_max(num1,num2,num3);
cout << "The largest number is " << largest << ".\n";
return 0;

}

4 个答案:

答案 0 :(得分:1)

有一个应用程序。 : - )

int find_max(int x, int y, int z) {
     int ret = x;
     if(y>ret) ret = y;
     if(z>ret) ret = z;
     return ret;
}

或者,如果您愿意,

public float walkSpeed = 2;
public float runSpeed = 6;
public float turnSmoothTime = 0.2f;
float turnSmoothVelocity;
public float speedSmoothTime = 0.1f;
float speedSmoothVelocity;
float currentSpeed;
Animator animator;

void Start () {
    animator = GetComponent<Animator> ();
}

void Update () {
    Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
    Vector2 inputDir = input.normalized;

    if (inputDir != Vector2.zero) {
        float targetRotation = Mathf.Atan2 (inputDir.x, inputDir.y) * Mathf.Rad2Deg;

        transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
    }

    bool running = Input.GetKey (KeyCode.LeftShift);

    float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;

    currentSpeed = Mathf.SmoothDamp (currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);

    transform.Translate (transform.forward * currentSpeed * Time.deltaTime, Space.World);

    float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;

    animator.SetFloat ("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
}

答案 1 :(得分:0)

仔细阅读如何处理c ++中的函数。 因此,您会发现函数可以返回一些值。例如,您的函数返回给定三个值中最大的值。但你没有拿回值。

http://www.cplusplus.com/doc/tutorial/functions/

答案 2 :(得分:0)

在if语句之前定义更大的

int largest;
If ((x>=y.......

答案 3 :(得分:0)

这是正确的代码。你在第一行功能之后放了半个冒号。并且您没有使用函数find_max返回的值。在下面的代码中,我将值赋给变量maximum。

#include <iostream>

using namespace std;

int find_max(int x, int y, int z)
{

int largest;
if((x >= y) && (x>= z)){
largest = x;
}
if((y >= x) && (y>= z)){
largest = y;
}
if((z >= x) && (z>= y)){
    largest = z;
}
    return largest;
}

int main()
{
  int num1, num2, num3, largest;

  cout << "Please enter three numbers: ";
  cin >> num1 >> num2 >> num3;
  largest=find_max(num1,num2,num3);
  cout << "The largest number is " << largest << ".\n";
  return 0;

  }