我如何...使用if else有条件地更改此变量 [英] How do I... Make this variable change conditionally with if else

查看:164
本文介绍了我如何...使用if else有条件地更改此变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

double avgToFinalLetterGrade,totalKnownGradeWeight,finalOverallScore;

finalOverallScore = 0;

if(desiredGrade ==A|| desiredGrade ==a){

finalOverallScore = 90;}

else if(desiredGrade ==B|| desiredGrade ==b){

finalOverallScore = 80; }

else if(desiredGrade ==C|| desiredGrade ==c){

finalOverallScore = 70;}

else if(desiredGrade ==D|| desiredGrade ==d){

finalOverallScore = 60; }

else if(desiredGrade ==F|| desiredGrade ==f){

finalOverallScore = 0; }



totalKnownGradeWeight =(weightExamOne + weightExamTwo + weightFinalExam + weightLabs + weightProjects + weightAttendance + weightQuizzes);

avgToFinalLetterGrade =((100 * finalOverallScore ) - ((weightExamOne * examOneScore)+(weightExamTwo * examTwoScore)+(weightFinalExam * examFinalScore)+(weightLabs * avgLabGrade)+(weightQuizzes * avgQuizGrade)+(weightProjects * avgProjectGrade)+(weightAttendance * avgAttendanceGrade))

/(100 - (totalKnownGradeWeight)));



我尝试过:



每当我运行程序时(在此部分之前还有更多的if else和用户输入)finalOverallScore不会有条件地改变我的if else,只进入0.我将其初始化为零所以它有如果没有初始化的话,那个去掉了if else或变量的路径......什么是错的

double avgToFinalLetterGrade, totalKnownGradeWeight, finalOverallScore;
finalOverallScore = 0;
if (desiredGrade == "A" || desiredGrade == "a") {
finalOverallScore = 90;}
else if (desiredGrade == "B" || desiredGrade == "b") {
finalOverallScore = 80; }
else if (desiredGrade == "C" || desiredGrade == "c") {
finalOverallScore = 70;}
else if (desiredGrade == "D" || desiredGrade == "d") {
finalOverallScore = 60; }
else if (desiredGrade == "F" ||desiredGrade == "f") {
finalOverallScore = 0; }

totalKnownGradeWeight = (weightExamOne + weightExamTwo + weightFinalExam + weightLabs + weightProjects + weightAttendance + weightQuizzes);
avgToFinalLetterGrade = ((100 * finalOverallScore) - ((weightExamOne * examOneScore) + (weightExamTwo * examTwoScore) +(weightFinalExam * examFinalScore) + (weightLabs * avgLabGrade) + (weightQuizzes * avgQuizGrade) + (weightProjects * avgProjectGrade) + (weightAttendance * avgAttendanceGrade))
/ (100 - (totalKnownGradeWeight)));

What I have tried:

whenever i run the program (before this section theres a lot more if else and user input) the finalOverallScore doesnt change conditionally with my if else, only goes in as 0. i initialized it as zero so it had a path to go w out the if else or the variable showed up as uninitialized... whats wrong

推荐答案

我们分不清 - 太多取决于内容你的数据,以及当代码实际执行时:我们无法控制它,我们也不能在这里测试它。



所以,它会上升对你来说。

在函数的第一行放一个断点,然后通过调试器运行你的代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
We can't tell - too much depends on the content of your data, and when that code actually gets executed: and we have no control over that, nor can we test it here.

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


我会选择转换语句而不是一堆if ... else语句。

类似于:

I would go for a switch statement rather than a bunch of if...else statements.
Something like:
switch (desiredGrade.ToLower()) {
   case "a":
      finalOverallScore = 90;
      break;
   case "b":
      finalOverallScore = 80;
      break;
   case "c":
      finalOverallScore = 70;
      break;
   case "d":
      finalOverallScore = 60;
      break;
   case "e":
      finalOverallScore = 50;
      break;
   case "f":
      finalOverallScore = 0;
      break;
   default:
      throw new ArgumentException("Wrong grade", nameof(desiredGrade));
}





至于知道为什么你的变量没有得到正确的值,你将不得不在方法的第一行和调试,看看为什么。可能是 desiredGrade 不是A,a,B,b,C,c,D,d,F也不是f。



注意:我添加了你似乎没有考虑到的e情况。

注2:将值转换为小写允许你拥有更少的条件测试。



这里要理解的最重要的事情是你必须调试你的代码才能理解为什么它的行为不像你期望的那样它是。



亲切。



As for knowing why your variable does not get the correct value, you will have to put a breakpoint on the first line of the method and debug to see why. It could be that desiredGrade is not A, a, B, b, C, c, D, d, F nor f.

Note: I added the "e" case which you did not seem to have taken into account.
Note 2: transforming the value to lower case allows you to have fewer conditions to test.

Most important thing to understand here is that you have to debug your code to understand why it does not behave like you would expect it to.

Kindly.


这意味着 desiredGrade 值不是你的想法。

了解使用调试器是什么的唯一方法,设置断点并检查 desiredGrade 值和长度。



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

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

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



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

在Visual中调试C#代码工作室 - YouTube [ ^ ]

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

调试器中没有魔法,它不是'找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
That mean that desiredGrade value is not what you think.
the only way to know what is what is to use the debugger, set a breakpoint and inspect desiredGrade value and length.

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[^]
Debugging C# Code in Visual Studio - 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.


这篇关于我如何...使用if else有条件地更改此变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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