嗨,我正在尝试解决汽车拆除问题。我的错误是什么?因为我无法得到正确的答案。 [英] Hi, I'm trying to solve a problem for a car millage. What are my mistakes. Since I can't get the correct answer.

查看:100
本文介绍了嗨,我正在尝试解决汽车拆除问题。我的错误是什么?因为我无法得到正确的答案。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题。



HERE IS THE QUESTION.

Enter the miles used (-1 to quit): 287
Enter gallons: 13
MPG this tankful: 22.076923
Total MPG: 22.076923
Enter the miles used (-1 to quit): 200
Enter gallons: 10
MPG this tankful: 20.000000
Total MPG: 21.173913
Enter the miles used (-1 to quit): 120
Enter gallons: 5
MPG this tankful: 24.000000
Total MPG: 21.678571
Enter miles (-1 to quit): -1





我尝试过:





What I have tried:

#include <iostream>
#include "conio.h"
using namespace std;
void main()

{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(6);

	float miles = 0; float gallons = 0; float milage = 0; float TMG = 0; float TM = 0; float TG = 0;

	cout << "Enter Miles(-1 to quit): ";
	cin >> miles;
	while (miles != -1)
	{
		
		cout << "Enter gallons: ";
		cin >> gallons;
		milage = miles / gallons;
		cout << "Milage this tankful is: " << milage << endl;
		
		if (TMG == 0)
		{
			cout << "Total Milage is: " << milage << endl;
		}
		else
		{
			cout << "Total Milage is: " << TMG << endl;
		}
		
		TM = TM + miles;
		TG = TG + gallons;
		TMG = TM / TG;
		cout << "Enter Miles(-1 to quit): ";
		cin >> miles;
	}

	_getche();
}

推荐答案

仅存储里程和加仑:并将它们存储为整数 - 这就是用户输入的内容。

所以你需要四个变量:tripMiles,tripGallons,totalMiles,totalGallons。

每次用户输入旅行值时,从他们打印MPG:

Store your miles and gallons only: and store them as integers - that's what the user inputs them as.
So you need four variables: tripMiles, tripGallons, totalMiles, totalGallons.
Each time the user enters trip values, print the MPG from them:
cout << "MPG this tankful is: " << (float) tripMiles / (float(tripGallons) << endl;

并将行程值添加到总计中。以类似的方式输出总MPG:

And add the trip values to the totals. Output the total MPG in a similar way:

cout << "Total MPG: " << (float) totalMiles / (float(totalGallons) << endl;





除此之外,你需要每次围绕循环重新读取里程,而不仅仅是在它之外。



Other than that, you need to re-read the miles each time round the loop, not just outside it.


Quote:

我的错误是什么。因为我无法得到正确的答案。

What are my mistakes. Since I can't get the correct answer.



让我们猜测,你在第三次输入后获得第二个答案,第五次输入后获得第四个答案。

你的代码中放错了地方。使用调试器看看做了什么以及什么时候做。



有一个工具可以让你看到什么你的代码正在做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Lets guess, you get second answer after third input, fourth answer after fifth input.
Things are misplaced in your code. Use the debugger to see what is done and when.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于嗨,我正在尝试解决汽车拆除问题。我的错误是什么?因为我无法得到正确的答案。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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