2014-10-20 5 views
0

기본적으로이 설문 조사 프로그램이 있으며 "if"문에 의해 주어진 응답과 일치하는 응답 된 질문의 비율을 계산하는 방법을 알고 싶습니다. 이 백분율은 사용자 유형에 따라 다를 수 있습니다. 또 다른 점은 아래 코드에서 이미 설정 및 주석 처리 된 비 기본 메소드에 백분율을 계산하기위한 코드를 넣어야한다는 것입니다 (비 기본 메소드로 레이블 지정). 내가 알고 싶은 것은 가능한 모든 사용자 입력 (설문 조사가 5 개의 질문이므로 기본적으로 1/5, 2/5, 3/5, 4/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

백분율을 계산하는 방법을 알고 있습니까? 나는 일반적으로 (반드시 컴퓨터 프로그램에있는 것은 아닙니다) 의미합니다. – ajb

+0

예 (부품을 전체로 나눈 값) * 100 –

+0

가능한 입력 및 해당 백분율에 대해 배열을 만듭니다! 사용자가 대답을 입력 할 때 해당 대답을 증가시키고 마지막에는 백분율 계산을 수행하는 것이 쉬워야합니다. –

답변

1

마우스 오른쪽 단추로 나누어 해당 필드을 public static void main(String[] args) {의 내부 것은 같은 공용 변수를 선언 질문에 답하면이 숫자가 증가합니다. 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) 메서드의 매개 변수를 변경하여 질문과 전체 질문을 수정 한 다음 10 진수 형식으로 백분율을 반환하는 것이 현명한 방법이라고 생각합니다.

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.); 

희망.

+0

감사합니다. 나는 시험해 보았다. 그리고 그것은 실제로 일했다! 감사! –

+0

문제 없습니다. 행복한 코딩. –

0

, 당신은 사용자 권한 가지고 있다는 질문의 비율을 계산해야합니까?

0으로 초기화 된 올바른 질문 수를 저장하는 int 인 새로운 필드가 필요합니다. 그런 다음 질문이 생길 때마다이 합계를 1 씩 증가시킵니다. 우리가 얻을 때마다 지금

int questionsCorrect = 0; //this is where we will keep track of the questions 'correct' 

:

그런 다음 프로그램의 끝에서 당신은 질문

의 총 수
+0

저는 11 학년의 학생입니다. 그래서 기초적인 자바 키워드를 넘어서는 것은 아닙니다. 그래서 어떻게 사용자 입력을 저장하는 필드를 만들까요? 빠른 답장을 보내 주셔서 감사합니다. –

+0

아래 Aaron C의 의견을 참조하십시오! –

관련 문제