我当前的代码接收来自控制台的输入并返回积分、净胜球和球队位置。然而,当用户继续游戏时,points
和变量会不断重置。goal_diff
我认为我的 setter 方法可能有问题。这是我的代码:
public class team {
public String name;
int points;
private int goal_diff;
public int score;
public team(String anyname)
{
name = anyname;
}
public void setName(String newname){
name = newname;
}
public void setscore(int newScore){
score = newScore;
}
public String getName(){
return name;
}
public void setgoal_diff (int newGD){
goal_diff = newGD;
}
public int getGD(){
return goal_diff;
}
public void setpoints(int oldpoints){
points = +oldpoints;
}
public void setpoints1(int newpoints, int oldpoints){
points = oldpoints + newpoints;
}
public int getpoints(){
return points;
}
public int getscore(){
return score;
}
public void printInfo()
{
System.out.print("Name: "+ getName());
System.out.print(" Points: "+ getpoints());
System.out.print(" GD: "+ getGD());
System.out.println("");
}
@Override
public String toString() {
return "Team: "+name + ": " +"Points: "+ points + " GD "+ goal_diff;}
}
这是主要课程:
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class mainTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner (System.in);
Scanner in = new Scanner (System.in);
//name team1
System.out.print("Please enter name for team 1 : ");
String in1 = sc.nextLine();
team team1 = new team(in1);
team1.setName(in1);
//name team2
System.out.print("Please enter name for team 2 : ");
String in2 = sc.nextLine();
team team2 = new team(in2);
team2.setName(in2);
//name team3
System.out.print("Please enter name for team 3 : ");
String in3 = sc.nextLine();
team team3 = new team(in3);
team3.setName(in3);
//name team 4
System.out.print("Please enter name for team 4 : ");
String in4 = sc.nextLine();
team team4 = new team(in4);
team4.setName(in4);
//teamnames array
String [] teamnames={in1,in2,in3,in4};
System.out.println("Do you want to play or exit? Please enter Y to continue playing or N to exit");
String in0 = sc.nextLine();
while(in0.equals("Y")||in0.equals("y")){
//choose first two teams playing
System.out.print("Please enter names and scores for playing teams 1 : ");
String resultA = sc.nextLine();
//String e = String.valueOf(resultA);
String[] myword= resultA.split(" ");
//List<String> playing1 = Arrays.asList(myword);
int score1 = Integer.parseInt(myword[1]);
int score2 = Integer.parseInt(myword[3]);
String p1 =myword[0];
//modify to make method
while (!(check(p1,teamnames))){
sc = new Scanner (System.in);
System.out.println("please enter team from list");
String newn = sc.nextLine();
p1=newn;
}
/*chkteam(p1,teamnames);*/
String p2 = myword[2];
//method to check fr same team
p2 = duplicate(p1,p2);
//for 1st team
if (p1.equals(team1.getName())){
setting(team1,score1,score2);
}
else if (p1.equals(team2.getName())){
setting(team2,score1,score2);
}
else if (p1.equals(team3.getName())){
setting(team3,score1,score2);
}
else if (p1.equals(team4.getName())){
setting(team4,score1,score2);
}
//for 2nd team
if (p2.equals(team1.getName())){
setting(team1,score2,score1);
}
else if (p2.equals(team2.getName())){
setting(team2,score2,score1);
}
else if (p2.equals(team3.getName())){
setting(team3,score2,score1);
}
else if (p2.equals(team4.getName())){
setting(team4,score2,score1);
}
//choose next two teams playing
System.out.print("Please enter next 2 teams playing : ");
String resultB = in.nextLine();
String f = String.valueOf(resultB);
String[] myword1= f.split(" ");
int score3 = Integer.parseInt(myword1[1]);
int score4 = Integer.parseInt(myword1[3]);
//:)
String p3 = myword1[0];
String p4 = myword1[2];
p3 = duplicate(p1,p3);
p3 = duplicate(p2,p3);
p4 = duplicate(p1,p4);
p4 = duplicate(p2,p4);
p4 = duplicate(p3,p4);
//for 1st team
if (p3.equals(team1.getName())){
setting(team1,score3,score4);
}
else if (p3.equals(team2.getName())){
setting(team2,score3,score4);
}
else if (p3.equals(team3.getName())){
setting(team3,score3,score4);
}
else if (p3.equals(team4.getName())){
setting(team4,score3,score4);
}
//for 2nd team
if (p4.equals(team1.getName())){
setting(team1,score4,score3);
}
else if (p4.equals(team2.getName())){
setting(team2,score4,score3);
}
else if (p4.equals(team3.getName())){
setting(team3,score4,score3);
}
else if (p4.equals(team4.getName())){
setting(team4,score4,score3);
}
team [] teams = {team1,team2,team3,team4};
sortPoints(teams);}
/*else {System.out.print("Thank you for playing the game");};*/
}
//METHODS start here
public static String duplicate(String name1, String name2){
while (name1.equals(name2)){
System.out.print("team " + name1 +" has already been entered. Please enter a different team :");
Scanner sc = new Scanner (System.in);
name2 = sc.nextLine();
}
return name2;
}
/*public static String chkteam(String play, String []teamnames){
while (!(check(play,teamnames))){
Scanner sc = new Scanner (System.in);
System.out.println("please enter team from list");
String newn = sc.nextLine();
play=newn;
}
return play;
}*/
public static void setting(team ipteam,int score1, int score2){
ipteam.setscore(score1);
ipteam.setgoal_diff(score1-score2);
if (score1 > score2){ipteam.setpoints(3);}
else if (score1 == score2){ipteam.setpoints(1);}
else {ipteam.setpoints(0);}
}
private static boolean isInteger(String s){
try{
Integer.parseInt(s);
return true;}
catch( Exception e ){
return false;
}}
private static boolean check(String teamname, String [] ar){
boolean res=false;
for (String x : ar){
if (x.equals(teamname)){
res=true;
}
}
return res;
}
private static void sortPoints(team[] teams) {
// TODO Auto-generated method stub
Arrays.sort(teams, new Comparator<team>() {
int res = 0;
@Override
public int compare(team o2, team o1) {
if (o1.getpoints()>o2.getpoints()){
res = 1;
}
else if (o1.getpoints()<o2.getpoints()){
res = -1;
}
else if (o1.getpoints()== o2.getpoints()){
if (o1.getGD()>o2.getGD()){
res = 1;
}
else {
res = -1;
}
}
return res;
}
});
printArr(teams);
}
private static void printArr(team[] teams) {
for(team i : teams) {
System.out.println(i);}
}
}