在 C++ 中获取百分比时遇到问题
Trouble getting percentages in C++
这里是 C++ 初学者。我正在尝试复制此输出,我快要完成了,但我在最后一部分(测验百分比)上遇到了问题。
Example output:
Welcome to our math quiz program! Please enter the answers to the
following questions:
5! = 5
(2^(2^(2^2))) = 65536
3 * (4 + 8) / ((4 * 2) / (5 – 1)) = 18
3 + 2 + 1 + 2 + 3 = 11
Number of correct answers: 3
Number of incorrect answers: 1
Quiz percentage: 75%
到目前为止,这是我的代码:
int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;
int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;
int guessOne;
int guessTwo;
int guessThree;
int guessFour;
cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;
switch (guessOne) {
case 120:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessTwo) {
case 65536:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessThree) {
case 18:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessFour) {
case 11:
numCorrect++;
break;
default:
numIncorrect++;
}
cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect / possibleCorrect) * 100 << "%" << endl;
cout << endl;
return 0;
}
似乎当我正确回答所有问题时,我得到了 100% 的分数,但是,当我得分以外的任何分数(只有 1、2 或 3 正确)时,我最终得到 0%,因为结果。我以为我正确地进行了百分比计算,但输出却另有建议。有人能指出我正确的方向吗?谢谢
编辑:为开关添加了中断(但是,不是问题的根源)。
切换时需要中断,先乘以 100.0 以隐式地将第一个表达式提升为双精度数,然后除法:
int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;
int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;
int guessOne;
int guessTwo;
int guessThree;
int guessFour;
cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;
switch (guessOne) {
case 120:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessTwo) {
case 65536:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessThree) {
case 18:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessFour) {
case 11:
numCorrect++;
break;
default:
numIncorrect++;
}
cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect * 100.0 / possibleCorrect) << "%" << endl;
cout << endl;
return 0;
}
虽然为了这个目的,一个简单的 if/else 会更容易阅读:
int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;
int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;
int guessOne;
int guessTwo;
int guessThree;
int guessFour;
cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;
if( guessOne == 120 ) ++numCorrect; else ++numIncorrect;
if( guessTwo == 65536 ) ++numCorrect; else ++numIncorrect;
if( guessThree == 18 ) ++numCorrect; else ++numIncorrect;
if( guessFour == 11 ) ++numCorrect; else ++numIncorrect;
cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect * 100.0 / possibleCorrect) << "%" << endl;
cout << endl;
return 0;
}
您的开关中缺少中断语句。
此外,您需要将结果类型转换为浮点型,因为所有变量都是整数。我建议你使用:
float perc;
perc = (numCorrect/possibleCorrect)*100;
这里是 C++ 初学者。我正在尝试复制此输出,我快要完成了,但我在最后一部分(测验百分比)上遇到了问题。
Example output:
Welcome to our math quiz program! Please enter the answers to the
following questions:
5! = 5
(2^(2^(2^2))) = 65536
3 * (4 + 8) / ((4 * 2) / (5 – 1)) = 18
3 + 2 + 1 + 2 + 3 = 11
Number of correct answers: 3
Number of incorrect answers: 1
Quiz percentage: 75%
到目前为止,这是我的代码:
int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;
int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;
int guessOne;
int guessTwo;
int guessThree;
int guessFour;
cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;
switch (guessOne) {
case 120:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessTwo) {
case 65536:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessThree) {
case 18:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessFour) {
case 11:
numCorrect++;
break;
default:
numIncorrect++;
}
cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect / possibleCorrect) * 100 << "%" << endl;
cout << endl;
return 0;
}
似乎当我正确回答所有问题时,我得到了 100% 的分数,但是,当我得分以外的任何分数(只有 1、2 或 3 正确)时,我最终得到 0%,因为结果。我以为我正确地进行了百分比计算,但输出却另有建议。有人能指出我正确的方向吗?谢谢
编辑:为开关添加了中断(但是,不是问题的根源)。
切换时需要中断,先乘以 100.0 以隐式地将第一个表达式提升为双精度数,然后除法:
int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;
int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;
int guessOne;
int guessTwo;
int guessThree;
int guessFour;
cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;
switch (guessOne) {
case 120:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessTwo) {
case 65536:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessThree) {
case 18:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessFour) {
case 11:
numCorrect++;
break;
default:
numIncorrect++;
}
cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect * 100.0 / possibleCorrect) << "%" << endl;
cout << endl;
return 0;
}
虽然为了这个目的,一个简单的 if/else 会更容易阅读:
int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;
int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;
int guessOne;
int guessTwo;
int guessThree;
int guessFour;
cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;
if( guessOne == 120 ) ++numCorrect; else ++numIncorrect;
if( guessTwo == 65536 ) ++numCorrect; else ++numIncorrect;
if( guessThree == 18 ) ++numCorrect; else ++numIncorrect;
if( guessFour == 11 ) ++numCorrect; else ++numIncorrect;
cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect * 100.0 / possibleCorrect) << "%" << endl;
cout << endl;
return 0;
}
您的开关中缺少中断语句。
此外,您需要将结果类型转换为浮点型,因为所有变量都是整数。我建议你使用:
float perc;
perc = (numCorrect/possibleCorrect)*100;