Reputation: 11
I am still new to Java and have an assignment that I am stuck on. I believe I got the boolean function correct, however I cannot figure out what to write in the main function.
This the assignment:
Write a public function (static method) winner(int points1,int points2) that takes two scores and returns a boolean. If points1 is greater than points2 the function returns true and if points1 is less than points2, the function returns false. Points should not be tied but return false if that ever happens. The function returns false if none of the conditions is true.
In your main function, create two arrays -the first array is team 1’s points and the second array is opponents’ points in corresponding games, i.e., the first items in the arrays are the scores for each team in the first game and so on. The arrays are the same length.
Using the function winner from above, calculate and print the win/loss record of team1. If all games were won, print out a message saying that team1 has a perfect record. Here are some examples:
Team1 Points {23,45,65,20}
Opponent Points {56,67,20,18}
This is what teamRecord should print:
Win 2 Loss 2
My code so far
import java.util.*;
public class TeamScore {
public static boolean createWinner(int points1, int points2)
{
if (points1 > points2)
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] team1Array = new int[4];
int[] team2Array = new int[4];
int result1 = 0;
int result2 = 0;
int team1Score;
int team2Score;
int win;
int loss;
System.out.println("Enter Team 1 points: ");
team1Score = Integer.parseInt(in.nextLine());
System.out.println("Enter Team 2 points: ");
team2Score = Integer.parseInt (in.nextLine());
for (int i=0; i <team1Array.length; i++)
{
createWinner(points1);
}
System.out.println("Win"+ team1Score);
//System.out.println(result2+"");
}
}
Upvotes: 0
Views: 6017
Reputation: 335
There are a lot of issues in your code :
1) You declared a function createWinner(int points1, int points2)
, but in your main you call createWinner(points1)
2) In your main, what's points1
? You didn't declare it.
3) Before computing, you should populate your arrays
After this explanation, you can modify your code with following :
import java.util.*;
public class TeamScore {
public static boolean createWinner(int points1, int points2)
{
if (points1 > points2)
{
return true;
}
else
{
return false;
}
return false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] team1Array = new int[4];
int[] team2Array = new int[4];
int team1Score = 0;
int team2Score = 0;
for (int i =0; i<4; i++) {
System.out.println("Enter Team 1 points: ");
team1Array[i]= Integer.parseInt(in.nextLine());
System.out.println("Enter Team 2 points: ");
team2Array[i] = Integer.parseInt(in.nextLine());
}
for (int i=0; i <4; i++)
{
if (createWinner(team1Array[i],team2Array[1])) {
team1Score+=1;
} else {
team2Score+=1;
}
}
if(team1Score>0 && team2Score == 0){
System.out.println("Team 1 has perfect record !");
}else{
System.out.println("Win "+ team1Score);
System.out.println("Loss "+ team2Score);
}
}
}
Less or more should work (I didn't try).
Upvotes: 1
Reputation: 37033
Take the input from user 4 times and store it in an array using for loop:
for (int i =0; i<4; i++) {
team1Array[i]= //take user input for team1
team2Array[i] = //take user input for team2
}
Then you could loop over array and increase individual score like:
for (int i=0; i <team1Array.length; i++) {
if (createWinner(points1)) {
//increment team1Score;
} else {
//increment team2Score;
}
}
Compare individual score using if and announce the winner by printing onto console.
Upvotes: 1