所以基本上我有这个调查计划,我想知道如何计算回答的问题百分比,这些问题与" if"声明。但是这个百分比可以根据用户输入的内容而有所不同。另一件事是我需要将用于计算百分比的代码放入我已经设置并在下面的代码中注释的非主方法(标记为非主方法)。我想知道的是如何制作一个能够解释所有可能的用户输入的公式(基本上是1 / 5,2 / 5,3 / 5,4 / 5和5/5,因为调查是5个问题)。
import java.util.Scanner;
public class VideogameSurveyFINAL {
//non-main method
private static void gamerpercentagelikeme (String[] args) {
int correct
}
//main method below
public static void main(String[] args) {
boolean run = true;
while(run){
System.out.println ("WELCOME TO THE GRAND SURVEY OF VIDEOGAMES!");
System.out.println ("");
Scanner keyboard = new Scanner(System.in);
//Q1
System.out.println("Question 1: Do you like Videogames? (Please answer Yes or No)");
String input = keyboard.nextLine();
if (input.equalsIgnoreCase("Yes")||input.equalsIgnoreCase("Yeah")||input.equalsIgnoreCase("Y")||input.equalsIgnoreCase("YES!")) {
System.out.println("Awesome!");
}
else {
System.out.println("Wow. You have no sense of fun.");
}
//Q2
System.out.println ("");
System.out.println("Question 2: What types of videogames do you play?(Please use abbreviated froms of games such as FPS, MMO, RPG etc).");
String input2 = keyboard.nextLine();
if (input2.equalsIgnoreCase("Fps")) {
System.out.println("Fantastic! Me too!");
}
else {
System.out.println("OK, thats cool!My favorite is FPS.");
}
//Q3
System.out.println ("");
System.out.println("Question 3:Do you like Singleplayer or Multiplayer games? ");
String input3 = keyboard.nextLine();
if (input3.equalsIgnoreCase("Singleplayer")||input.equalsIgnoreCase("S")||input.equalsIgnoreCase("SP")) {
System.out.println("Me Too!!");
}
else {
System.out.println("Rad! My favorite is Singleplayer. ");
}
//Q4
System.out.println ("");
System.out.println("Question 4: What do you like in a videogame");
String input4 = keyboard.nextLine();
if (input4.equalsIgnoreCase("I dont play games")||input.equalsIgnoreCase("None")||input.equalsIgnoreCase("N")) {
System.out.println("You are sooooo boring.");
}
else {
System.out.println("Awesome! What I like in a videogame is solid gameplay, great graphics and immersive audio...but most of all, IT HAS TO BE FUN!!");
}
//Q5
System.out.println ("");
System.out.println("Question 5: What is your ALL-TIME favorite videogame?");
String input5 = keyboard.nextLine();
if (input5.equalsIgnoreCase("Skyrim")) {
System.out.println("Really?! ME TOO!");
}
else {
System.out.println("Great! My personal favorite is SKYRIM!");
}
System.out.println ("");
System.out.println("THANK YOU FOR TAKING THE GRAND SURVEY OF VIDEOGAMES!");
System.out.println ("");
System.out.println ("Would you like to take the survey again? (Please answer with Yes or No: Case sensitive)");
String input6 = keyboard.nextLine();
if (input6.equalsIgnoreCase("No")) {
break;
}
else
{
System.out.println("Okay! The Survey will restart shortly.");
System.out.println ("");
System.out.println ("");
}
}
} }
答案 0 :(得分:1)
public static void main(String[] args) {
内部声明一个公共变量,如:
int questionsCorrect = 0; //this is where we will keep track of the questions 'correct'
现在每次我们得到一个正确的问题,我们都会增加这个数。 在if(condition ...)语句中,我们将执行此增量。
例如:
if (input.equalsIgnoreCase("Yes")||input.equalsIgnoreCase("Yeah")||input.equalsIgnoreCase("Y")||input.equalsIgnoreCase("YES!")) {
System.out.println("Awesome!");
questionsCorrect++; //increment number of questions correct by 1
}
为调查中的每个问题执行此操作。
现在对于百分比......我认为更改private static void gamerpercentagelikeme (String[] args)
方法的参数以正确处理问题和总问题,然后以十进制形式返回百分比是明智的。
private static double gamerpercentagelikeme (int correct, int total){
return correct/total;
}
现在,当我们想要显示答案时,我们可以致电:
double results = gamerpercentagelikeme(questionsCorrect, TOTAL_QUESTIONS); //note: declare TOTAL_QUESTIONS before using it.
double percentageLikeMe = results * 100;
System.out.println(percentageLikeME + "% like me.);
希望这有帮助。
答案 1 :(得分:0)
如果我理解正确,你需要计算用户正确的问题百分比吗?
你需要有一个新的字段,它是一个int,它存储了他们正确的问题数量,初始化为零。然后,每当他们得到一个问题,你就把这个总数加一。
然后在程序结束时,您将该字段除以问题总数