我正在创建一个计算课程分数的应用程序,然后根据分数输出一个字母等级。
总而言之,我不是数学上最伟大的,我在识别代码中的数学错误位置时遇到了问题,并且控制台因此而分割出过多的百分比值。
以下是控制台输出的内容:
My quiz percentage is 83.33333
my lab percentage is 72.916664
My Mid-Term percentage is 196.72131
my End-Term Percentage is 200.72131
My final percentage grade is 115.35263
Calculation error: possible percentage values between 1%-100%. Check your math and try again.
(最后一行只是一个if语句,表明存在计算错误。如果正确完成,它应该输出一个字母等级,由其他if语句处理。)
以下是我的代码:
//Quiz scores
float q1=20, q2=19, q3=14, q4=17, actualQuizPoints=70, actualPoints=80, totalMidTermPoints=120, actualMidTermPoints=61;
//Lab scores
float l1=20, l2=19, l3=14, l4=17;
void setup() {
noLoop();
size(100,100);
}
void draw() {
float totalQuizPoints=Addition(q1, q2, q3, q4);
float quizPercentage = Percentage(totalQuizPoints, actualQuizPoints);
float totalLabPoints =Addition(l1, l2, l3, l4);
float labPercentage=Percentage(totalLabPoints, actualPoints);
float midTermPercentage=Percentage(totalMidTermPoints, actualMidTermPoints);
float endTermPercentage=midTermPercentage+4;
float finalPercentage=Addition(quizPercentage, labPercentage, midTermPercentage, endTermPercentage)/4;
println("My quiz percentage is " + quizPercentage);
println("my lab percentage is " + labPercentage);
println("My Mid-Term percentage is " + midTermPercentage);
println("my End-Term Percentage is " + endTermPercentage);
println("My final percentage grade is " + finalPercentage);
if (finalPercentage <60) {
println ("My final letter grade is F.");
}
if (finalPercentage >=60 & finalPercentage <70) {
println ("My final letter grade is D.");
}
if (finalPercentage >=70 & finalPercentage <80) {
println ("My final letter grade is C.");
}
if (finalPercentage >=80 & finalPercentage <90) {
println ("My final letter grade is B.");
}
if (finalPercentage >=90 & finalPercentage <=100) {
println ("My final letter grade is A.");
}
if (finalPercentage >100) {
println ("Calculation error: possible percentage values between 1%-100%. Check your math and try again.");
}
}
float Addition(Float n1, float n2, float n3, float n4) {
float total=(n1+n2+n3+n4)/120*100;
return total;
}
float Percentage(float totalpoints, float actualpoints) {
float percentageIs=(totalpoints/actualpoints)*100;
return percentageIs;
}
答案 0 :(得分:3)
您与使用total
和actual
这两个词的方式不一致。
对于测验点和实验室积分,您使用total
表示所有获得积分的总和,actual
表示可以获得的积分。
对于期中,期末和最后一点,你可以转换它们。
您需要修改代码,以便与传入percentage()
函数的参数保持一致。
答案 1 :(得分:1)
不要在乎其他人的想法,但我只想尝试回答有趣的事情。没有理解你的所有代码。但我已将我的整个代码发布到我的&#34;调查结果&#34;在底部。
我的评论是可选的
如果编译,至少底部的代码会给出0-100%的实际百分比:)
结果:
My quiz percentage is 87.5
my lab percentage is 87.5
My Mid-Term percentage is 50.833332
my End-Term Percentage is 54.833332
My final percentage grade is 70.166664
My final letter grade is C.
不太确定这段代码是什么:
void setup() {
noLoop();
size(100,100);
}
我主要更改了添加方法以实际添加4个数字:
float Addition(Float n1, float n2, float n3, float n4) {
float total=(n1+n2+n3+n4);
return total;
}
我认为我改变了一些参数f。实例:
float quizPercentage = Percentage(totalQuizPoints, actualQuizPoints);
到actualPoints。否则它将计算70/70 * 100。这是100
我还互相切换参数:
float midTermPercentage=Percentage(totalMidTermPoints, actualMidTermPoints);
我不太确定这意味着什么,但我离开了它:
float endTermPercentage=midTermPercentage+4;
整个代码:
class Grading{
//Quiz scores
float q1=20, q2=19, q3=14, q4=17, actualQuizPoints=70, actualPoints=80, totalMidTermPoints=120, actualMidTermPoints=61;
//Lab scores
float l1=20, l2=19, l3=14, l4=17;
public static void main(String[] args){
Grading g=new Grading();
g.draw();
}
void draw() {
float totalQuizPoints=Addition(q1, q2, q3, q4);
float quizPercentage = Percentage(totalQuizPoints, actualPoints);
float totalLabPoints =Addition(l1, l2, l3, l4);
float labPercentage=Percentage(totalLabPoints, actualPoints);
float midTermPercentage=Percentage(actualMidTermPoints, totalMidTermPoints);
float endTermPercentage=midTermPercentage+4;
float finalPercentage=Addition(quizPercentage, labPercentage, midTermPercentage, endTermPercentage)/4;
System.out.println("My quiz percentage is " + quizPercentage);
System.out.println("my lab percentage is " + labPercentage);
System.out.println("My Mid-Term percentage is " + midTermPercentage);
System.out.println("my End-Term Percentage is " + endTermPercentage);
System.out.println("My final percentage grade is " + finalPercentage);
if (finalPercentage <60) {
System.out.println ("My final letter grade is F.");
}
if (finalPercentage >=60 & finalPercentage <70) {
System.out.println ("My final letter grade is D.");
}
if (finalPercentage >=70 & finalPercentage <80) {
System.out.println ("My final letter grade is C.");
}
if (finalPercentage >=80 & finalPercentage <90) {
System.out.println ("My final letter grade is B.");
}
if (finalPercentage >=90 & finalPercentage <=100) {
System.out.println ("My final letter grade is A.");
}
if (finalPercentage >100) {
System.out.println ("Calculation error: possible percentage values between 1%-100%. Check your math and try again.");
}
}
float Addition(Float n1, float n2, float n3, float n4) {
float total=(n1+n2+n3+n4);
return total;
}
float Percentage(float totalpoints, float actualpoints) {
float percentageIs=(totalpoints/actualpoints)*100;
return percentageIs;
}
}