【问题标题】:How to calculate percentage base on user input in java如何根据java中的用户输入计算百分比
【发布时间】:2014-10-20 22:46:20
【问题描述】:

所以基本上我有这个调查程序,我想知道如何计算与“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 ("");




     }
  }

} }

【问题讨论】:

  • 你知道如何计算百分比吗?我的意思是一般(不一定在计算机程序中)。
  • 是的,它只是(部分除以整体)*100
  • 为每个可能的输入及其百分比创建数组!应该很容易,当用户输入他们的答案时,只需增加相应的答案,最后进行百分比计算。
  • 太棒了。弄清楚哪些变量(或其他数据)包含“部分”和“整体”,然后您应该能够计算百分比。

标签: java math logic


【解决方案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.);

希望这会有所帮助。

【讨论】:

  • 没问题。编码愉快。
【解决方案2】:

如果我理解正确,您需要计算用户答对问题的百分比?

您需要有一个新字段,它是一个 int,用于存储他们正确的问题数,初始化为零。然后,每次他们答对了一个问题,你就将这个总数加一。

然后在程序结束时,您将该字段除以问题总数

【讨论】:

  • 我是 llth 年级的学生,所以除了基本的 java 关键字之外我真的没有太多东西,所以我将如何制作存储用户输入的字段?感谢您的快速回复。
  • 请参阅下方 Aaron C 的评论!
猜你喜欢
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多