The code is working correctly I just need to have three trys for each team to ha
ID: 3576879 • Letter: T
Question
The code is working correctly I just need to have three trys for each team to have.Team A and team B only have 3 chances to answer each question.
import java.util.Scanner;
public class FamilyFeud
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int TeamAScore = 0, TeamBScore = 0;
int currentQuestionTeam = 0;
String[] questions = new String[6];
String[] answers = new String[6];
System.out.println("Welcome Team A and Team B I'm your host Steve Harvey");
System.out.println("I will be asking you some question and you will receive 1 point for each answer ");
System.out.println("you must type your answer in lower case form other wise you won't get the answer");
System.out.println("right. So lets get started!");
questions[0] = "First president of the United States?:";
answers[0] ="george washington";
questions[1] ="First food people learn to cook?:";
answers[1] ="eggs";
questions[2] ="Place where you see lots of joggers?:";
answers[2] ="park";
questions[3]="Something people save?:";
answers[3]="money";
questions[4]="Why a baby might be cranky?:";
answers[4]="tired";
questions[5]="Something small that scares childern and adults?:";
answers[5]="spiders";
for(int i=0;i< 6;i++)
{
if(currentQuestionTeam == 0)
{
System.out.println("Question to Team A: ");
}
else
{
System.out.println("Question to Team B: ");
}
System.out.println(questions[i]);
String ans = sc.nextLine();
if(ans.equals(answers[i]))
{
System.out.println("correct");
if(currentQuestionTeam == 0)
{
TeamAScore++;
currentQuestionTeam = 1;
}
else
{
TeamBScore++;
currentQuestionTeam = 0;
}
}
else
{
if(currentQuestionTeam == 0)
{
System.out.println("Question Pass to Team B:");
}
else
{
System.out.println("Question Pass to Team A:");
}
ans = sc.nextLine();
if(ans.equals(answers[i]))
{
System.out.println("correct");
if(currentQuestionTeam == 0)
{
TeamAScore++;
currentQuestionTeam = 1;
}
else
{
TeamBScore++;
currentQuestionTeam = 0;
}
}
}
}
System.out.println("Team A score: "+TeamAScore);
System.out.println("Team B score: "+TeamBScore);
if(TeamAScore > TeamBScore){
System.out.println("Team A wins");
}
else if(TeamAScore < TeamBScore){
System.out.println("Team B wins");
}
else{
System.out.println("Both teams same score");
}
}
}
Explanation / Answer
import java.util.Scanner;
public class FamilyFeud2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int TeamAScore = 0, TeamBScore = 0;
int currentQuestionTeam = 0;
String[] questions = new String[6];
String[] answers = new String[6];
System.out.println("Welcome Team A and Team B I'm your host Steve Harvey");
System.out.println("I will be asking you some question and you will receive 1 point for each answer ");
System.out.println("you must type your answer in lower case form other wise you won't get the answer");
System.out.println("right. So lets get started!");
questions[0] = "First president of the United States?:";
answers[0] ="george washington";
questions[1] ="First food people learn to cook?:";
answers[1] ="eggs";
questions[2] ="Place where you see lots of joggers?:";
answers[2] ="park";
questions[3]="Something people save?:";
answers[3]="money";
questions[4]="Why a baby might be cranky?:";
answers[4]="tired";
questions[5]="Something small that scares childern and adults?:";
answers[5]="spiders";
for(int i=0;i< 6;i++)
{
///////////////////////////////////////////////////////////////////////////////////////
for(int k=1;k<=3;k++)
{
if(currentQuestionTeam == 0)
{
System.out.println("Attempt "+k+" : Question to Team A: ");
System.out.println(questions[i]);
String ans = sc.nextLine();
if(ans.equals(answers[i]))
{
System.out.println("correct");
if(currentQuestionTeam == 0)
{
TeamAScore++;
currentQuestionTeam = 1;
i++;
break;
}
else
{
TeamBScore++;
currentQuestionTeam = 0;
i++;
break;
}
}
}
else
{
System.out.println("Attempt "+k+" : Question to Team B: ");
System.out.println(questions[i]);
String ans = sc.nextLine();
if(ans.equals(answers[i]))
{
System.out.println("correct");
if(currentQuestionTeam == 0)
{
TeamAScore++;
currentQuestionTeam = 1;
i++;
break;
}
else
{
TeamBScore++;
currentQuestionTeam = 0;
i++;
break;
}
}
}
}
currentQuestionTeam = 1;
for(int k=1;k<=3;k++)
{
if(currentQuestionTeam == 0)
{
System.out.println("Attempt "+k+" : Question to Team A: ");
System.out.println(questions[i]);
String ans = sc.nextLine();
if(ans.equals(answers[i]))
{
System.out.println("correct");
if(currentQuestionTeam == 0)
{
TeamAScore++;
currentQuestionTeam = 1;
i++;
break;
}
else
{
TeamBScore++;
currentQuestionTeam = 0;
i++;
break;
}
}
}
else
{
System.out.println("Attempt "+k+" : Question to Team B: ");
System.out.println(questions[i]);
String ans = sc.nextLine();
if(ans.equals(answers[i]))
{
System.out.println("correct");
if(currentQuestionTeam == 0)
{
TeamAScore++;
currentQuestionTeam = 1;
i++;
break;
}
else
{
TeamBScore++;
currentQuestionTeam = 0;
i++;
break;
}
}
}
}
currentQuestionTeam = 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
System.out.println("Team A score: "+TeamAScore);
System.out.println("Team B score: "+TeamBScore);
if(TeamAScore > TeamBScore){
System.out.println("Team A wins");
}
else if(TeamAScore < TeamBScore){
System.out.println("Team B wins");
}
else{
System.out.println("Both teams same score");
}
}
}
Output :
Welcome Team A and Team B I'm your host Steve Harvey
I will be asking you some question and you will receive 1 point for each answer
you must type your answer in lower case form other wise you won't get the answer
right. So lets get started!
Attempt 1 : Question to Team A:
First president of the United States?:
f
Attempt 2 : Question to Team A:
First president of the United States?:
f
Attempt 3 : Question to Team A:
First president of the United States?:
f
Attempt 1 : Question to Team B:
First president of the United States?:
f
Attempt 2 : Question to Team B:
First president of the United States?:
f
Attempt 3 : Question to Team B:
First president of the United States?:
f
Attempt 1 : Question to Team A:
First food people learn to cook?:
f
Attempt 2 : Question to Team A:
First food people learn to cook?:
eggs
correct
Attempt 1 : Question to Team B:
Place where you see lots of joggers?:
g
Attempt 2 : Question to Team B:
Place where you see lots of joggers?:
g
Attempt 3 : Question to Team B:
Place where you see lots of joggers?:
g
Attempt 1 : Question to Team A:
Something people save?:
d
Attempt 2 : Question to Team A:
Something people save?:
d
Attempt 3 : Question to Team A:
Something people save?:
d
Attempt 1 : Question to Team B:
Something people save?:
v
Attempt 2 : Question to Team B:
Something people save?:
v
Attempt 3 : Question to Team B:
Something people save?:
v
Attempt 1 : Question to Team A:
Why a baby might be cranky?:
v
Attempt 2 : Question to Team A:
Why a baby might be cranky?:
v
Attempt 3 : Question to Team A:
Why a baby might be cranky?:
v
Attempt 1 : Question to Team B:
Why a baby might be cranky?:
t
Attempt 2 : Question to Team B:
Why a baby might be cranky?:
t
Attempt 3 : Question to Team B:
Why a baby might be cranky?:
t
Attempt 1 : Question to Team A:
Something small that scares childern and adults?:
t
Attempt 2 : Question to Team A:
Something small that scares childern and adults?:
tt
Attempt 3 : Question to Team A:
Something small that scares childern and adults?:
t
Attempt 1 : Question to Team B:
Something small that scares childern and adults?:
t
Attempt 2 : Question to Team B:
Something small that scares childern and adults?:
t
Attempt 3 : Question to Team B:
Something small that scares childern and adults?:
t
Team A score: 1
Team B score: 0
Team A wins