有人能告诉我这段代码是否正确?

有人能告诉我这段代码是否正确?

问题描述:

大家好吗

i想问你是否可以给我这个例子的解决方案?

因为我试过这些代码而我认为不是正确。

示例如下:编写一个完整的C程序,读取3个变量。

第一个变量是一个名为x的整数。第二个变量是名为y的浮点数,第三个变量是名为z的字符。

然后,打印变量x乘以10.变量y除以2,z之后的下一个字符(z + 1) )。



我的尝试:



hello everyone
i wanted to ask you if you can give me the solution to this example?
because i have tried these codes and i don't think it is right.
the example is this : Write a complete C program that reads 3 variables.
The first variable is an integer named x. The second variable is a float named y and the third variable is a character named z.
Then, print variable x multiplied by 10. variable y divided by 2 and the next character after z (z+1).

What I have tried:

// K.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdio.h>	
using namespace std;

int main()
{
	int x = 2;
	cout << "2*10=" << 2 * 10 << endl;
	float y = 1.5;
	cout << "1.5/2=" << 1.5 / 2 << endl;
	char z =5 ;
	cout << "z*(z+1)=" << z*(z+1)<< endl;
    return 0;
}

int x = 2;
cout << "2*10=" << 2 * 10 << endl;



错误1:要求您打印变量x乘以10 。如果突然x = 5怎么办?




Bug 1: you are requested to print variable x multiplied by 10. What if suddenly x= 5 ?

float y = 1.5;
cout << "1.5/2=" << 1.5 / 2 << endl;



错误2:要求你打印变量y除以2 。如果突然y = 3.5怎么办?




Bug 2: you are requested to print variable y divided by 2. What if suddenly y= 3.5 ?

char z =5 ;
cout << "z*(z+1)=" << z*(z+1)<< endl;



错误3:要求你打印z 之后的下一个字符(又名z + 1)。您不需要打印z *(z + 1)。

建议:z是char,z ='a'的可能性更大。



[更新]


Bug 3: you are requested to print the next character after z (aka z+1). You are not requested to print z*(z+1).
Advice: z being a char, z='a' would be more likely.

[Update]

int x  ;
float y  ;
char z = 'a';



您应该为x和y赋值

要知道程序是否正常,只需擦多次并更改值看看结果是否正常。


You should give values to x and y
To know if program is ok, just rub it many times and change the values to see if results are ok.


你错过了一个重要的要求:

You're missing an important requirement:
Write a complete C program that reads 3 variables.



您应该使用cin来获取3个变量的值,而不仅仅是分配常量值。喜欢:


You should use cin to get the values for the 3 variables, not just assign constant values. Like:

int main()
{
  int x;
  float y;
  char z;
  cout << "Enter x (int):";
  cin >> x;
  cout << "Enter y (float):";
  cin >> y;
  cout << "Enter z (char):";
  cin >> z;
  // etc...