如何添加用于计算正确答案百分比的函数 [英] How do I have to add the function for calculating the the percentage of correct answer
问题描述
我编译并运行这个程序,它工作正常,但我想再添加1个功能,当总回答问题超过75%时,系统将打印祝贺,你准备好了下一个级别,否则它将打印你必须看到你的老师。所以我的问题是谁可以帮我计算正确的答案,因为我不知道如何计算正确答案的百分比:(
什么我试过了:
import java.util 。*;
class learnMultiplication
{
public static void main( String args [ ])
{
int correctAnswers = 0 ;
int actualAnswer,userAnswer;
int randomNum [] = new int [ 2 ];
字符串问题;
扫描仪sc = new Scanner(System.in);
// 循环直到用户回答10个正确的问题
< span class =code-keyword> while (correctAnswers< = 9 )
{
// 生成问题
question = newQuestion(randomNum);
// 计算实际答案
actualAnswer = randomNum [< span class =code-digit> 0 ] * randomNum [ 1 ];
// 向用户显示问题
System.out。 print( \ n + question + \ n);
// 从用户那里获得答案
userAnswer = sc。 nextInt();
// 如果两个答案都不匹配循环它们都匹配
而(userAnswer!= actualAnswer)
{
// 向用户显示错误消息
displayWrongMessage();
// 再次显示相同的问题
System.out .println( \ n + question + \ n);
// 从用户那里获得答案
userAnswer = sc。 nextInt();
}
// 一旦userCorrectMe正确回答显示正确的消息
displayCorrectMessage();
// 计算正确答案的总数
correctAnswers ++;
}
}
// 向用户显示新问题的方法
public static 字符串 newQuestion( int randomNum [])
{
字符串问题;
int 最小值= 1 ,Max = 4 跨度>;
// 随机生成两个1位整数
randomNum [ 0 ] = Min +( int )(Math.random()*((Max - Min)+ 1 ));
randomNum [ 1 ] = Min +( int )(Math.random()*( (最大 - 最小)+ 1 ));
// 形成问题
question = 多少是 + randomNum [ 0 ] + < span class =code-string> times + randomNum [ 1 ] + ?;
// 向用户返回问题
返回问题;
}
// 向用户显示错误讯息的方法
public static void displayWrongMessage ()
{
int index;
字符串 message = ;
// 为错误答案生成随机数
< span class =code-keyword> int 最小值= 0 ,Max = 3 ;
index = Min +( int )(Math.random()*((Max - Min)+ 1 跨度>));
// 选择消息
switch (index)
{
case 0 :message = 不。请再试一次。;
break ;
case 1 :message = 错误。再试一次。;
break ;
case 2 :message = 不。不要放弃!;
break ;
case 3 :message = 不。继续尝试。;
break ;
默认: break ;
}
// 打印生成的索引中的消息
System.out.println( \ n + message + \ n);
}
// 向用户显示正确消息的方法
public static void displayCorrectMessage ()
{
int index;
字符串 message = ;
// 生成一个随机数以获得正确答案
< span class =code-keyword> int 最小值= 0 ,Max = 3 ;
index = Min +( int )(Math.random()*((Max - Min)+ 1 跨度>));
// 选择消息
switch (index)
{
case 0 :message = 非常好!;
break ;
case 1 :message = 优秀!;
break ;
case 2 :message = 干得好!;
break ;
case 3 :message = 保持良好的工作!;
break ;
默认: break ;
}
// 打印生成的索引中的消息
System.out.println( \ n + message + \ n);
}
}
引用:我不知道如何计算正确答案的百分比
你需要学习百分比和分数。两者都是小学数学。
坏消息是,在编程时,你必须不断处理这类或类似的东西。
https://en.wikipedia.org/wiki/Percentage [ ^ ]
https://en.wikipedia.org/wiki/Fraction_%28mathematics%29 [ ^ ]
[更新]
引用:我不知道如何添加函数来计算已回答问题中正确答案的总数。
我担心你已经拥有变量
correctAnswers
。
建议:你应该先研究你在互联网上找到的代码帮助
i have compile and run this program and it works just fine, but i wanna add 1 more function into it which is when the total answered question is more than 75% the system will print "Congratulation, you are ready for the next level", or else it will print "You have to see your teacher". So my question is who can help me do the calculation of the correct answer because i dont know how to calculate the percentage of the correct answer :(
What I have tried:
import java.util.*;
class learnMultiplication
{
public static void main(String args[])
{
int correctAnswers = 0;
int actualAnswer, userAnswer;
int randomNum[] = new int[2];
String question;
Scanner sc = new Scanner(System.in);
//Loop till user answers 10 correct questions
while(correctAnswers <= 9)
{
//Generate a question
question = newQuestion(randomNum);
//Calculate actual answer
actualAnswer = randomNum[0] * randomNum[1];
//Display question to user
System.out.print("\n " + question+"\n");
//Get answer from user
userAnswer = sc.nextInt();
//If both answers doesn't match loop both of them matches
while(userAnswer != actualAnswer)
{
//Display a wrong message to user
displayWrongMessage();
//Displaying the same question again
System.out.println("\n " + question+"\n");
//Get answer from user
userAnswer = sc.nextInt();
}
//Once userCorrectMe answers correctly display correct message
displayCorrectMessage();
//Counting total number of correct answers
correctAnswers++;
}
}
//Method that displays a new question to user
public static String newQuestion(int randomNum[])
{
String question;
int Min = 1, Max = 4;
//Generate two 1 digit integers randomly
randomNum[0] = Min + (int)(Math.random() * ((Max - Min) + 1));
randomNum[1] = Min + (int)(Math.random() * ((Max - Min) + 1));
//Forming question
question = " How much is " + randomNum[0] + " times " + randomNum[1] + " ? ";
//Return question to user
return question;
}
//Method that displays wrong message to user
public static void displayWrongMessage()
{
int index;
String message = "";
//Generate a random number for wrong answer
int Min = 0, Max = 3;
index = Min + (int)(Math.random() * ((Max - Min) + 1));
//Selecting message
switch(index)
{
case 0: message = "No. Please try again.";
break;
case 1: message = "Wrong. Try once more.";
break;
case 2: message = "No. Don't give up!";
break;
case 3: message = "No. Keep trying.";
break;
default: break;
}
//Printing the message present at the generated index
System.out.println("\n " + message + " \n");
}
//Method that displays correct message to user
public static void displayCorrectMessage()
{
int index;
String message = "";
//Generate a random number for correct answer
int Min = 0, Max = 3;
index = Min + (int)(Math.random() * ((Max - Min) + 1));
//Selecting message
switch(index)
{
case 0: message = "Very good!";
break;
case 1: message = "Excellent!";
break;
case 2: message = "Nice work!";
break;
case 3: message = "Keep up the good work!";
break;
default: break;
}
//Printing the message present at the generated index
System.out.println("\n " + message + " \n");
}
}
Quote:i dont know how to calculate the percentage of the correct answer
You need to learn percentages and fractions. Both are elementary school mathemarics.
The bad news is that while programming, you will have to deal continuously with such things or similar ones.
https://en.wikipedia.org/wiki/Percentage[^]
https://en.wikipedia.org/wiki/Fraction_%28mathematics%29[^]
[Update]
Quote:i dont know how to add function to count the total number of correct answer from the answered question.
I fear you already have it with variable
correctAnswers
.
Advice: you should study the code you found on internet before asking for help.
这篇关于如何添加用于计算正确答案百分比的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!