我对 Java 还很陌生,并且有一项任务被困住了。我相信我的 boolean 函数是正确的,但是我不知道在主函数中要写什么。
这是作业:
编写一个公共(public)函数(静态方法)winner(intpoints1,intpoint2),它接受两个分数并返回一个 boolean 值。如果points1大于points2,则函数返回true;如果point1小于points2,则函数返回false。分数不应相等,但如果发生这种情况,则返回 false。如果没有一个条件为 true,则该函数返回 false。
在你的主函数中,创建两个数组 - 第一个数组是球队 1 的得分,第二个数组是对手在相应比赛中的得分,即数组中的第一项是每个球队在第一场比赛中的得分,很快。数组的长度相同。
使用上面的函数 Winner,计算并打印 team1 的输赢记录。如果所有比赛都赢了,则打印一条消息,说明 team1 拥有完美的记录。以下是一些示例:
Team1 积分 {23,45,65,20}
对手得分 {56,67,20,18}
这是 teamRecord 应该打印的内容:
Team1 记录
赢2 损失2
到目前为止我的代码
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+"");
}
}
最佳答案
您的代码中有很多问题:
1) 您声明了一个函数 createWinner(intpoints1, intpoints2)
,但在 main 中您调用了 createWinner(points1)
2) 在你的 main 中,points1
是什么?你没有声明它。
3)在计算之前,您应该填充数组
在此解释之后,您可以使用以下方式修改您的代码:
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);
}
}
}
更少或更多应该有效(我没有尝试)。
关于java - 如何使用 boolean 函数和数组计算和显示 Java 中两个不同团队的赢/输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28194420/