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%的结果。我以为我正在正确地进行百分比计算,但输出是另外建议的。任何人都能指出我正确的方向吗?感谢。
编辑:为交换机添加了中断(但不是问题的根源)。
答案 0 :(得分:1)
您的交换机缺少Break语句。
另外,您需要将结果类型转换为float,因为所有变量都是整数。我建议你使用:
float perc;
perc = (numCorrect/possibleCorrect)*100;
答案 1 :(得分:0)
如果你切换你需要一个休息时间,然后先乘以100.0来隐式地将第一个表达式提升为double然后除以:
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;
}