所以我有一个包含锦标赛信息的课程。我有一个需要团队名称的arraylist。我只需要一个想法或功能,可以让我从其他类(机器人,TeamInfo)加载到ArrayLists内的每个指定成员的信息。
任何改进我的编码的批评都是非常被接受和赞赏的
测试是主类。锦标赛是包含锦标赛信息的类。
package test;
import java.util.Scanner;
/**
*
* @author
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String TheTournamentName, Date, Location;
int SoftwareStations , HardwareStations , FieldTesting;
Tournament UsingTheMain = new Tournament();
System.out.printf("Please Enter The Tournament Name\n");
TheTournamentName = input.nextLine();
UsingTheMain.setName(TheTournamentName);
//System.out.printf(UsingTheMain.getName());
System.out.printf("Please Enter the Date\n");
Date = input.nextLine();
UsingTheMain.setDate(Date);
System.out.printf("Please Enter The Location\n");
Location = input.nextLine();
UsingTheMain.setLocation(Location);
System.out.printf("Please Enter Number of Software Stations you want. Max Allowed is 2\n");
SoftwareStations = input.nextInt();
UsingTheMain.setSoftwareStations(SoftwareStations);
System.out.printf("Please Enter Number of Hardware Stations you want. Max allowed is 2\n");
HardwareStations = input.nextInt();
UsingTheMain.setHardwareStations(HardwareStations);
System.out.printf("Please Enter Number of Field Stations. Max Allowed is 2\n");
FieldTesting = input.nextInt();
UsingTheMain.setFieldTesting(FieldTesting);
System.out.printf("Please Enter 8 Teams");
// TODO code application logic here
}
}
第二课
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author
*/
public class Tournament {
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
*/
private String Name = "TournamentDEFULT";
private String Date = "22nd of Feb";
private String Location = "Texas";
private ArrayList<String> TeamNames = new ArrayList<String>();
private ArrayList<String> list = new ArrayList<String>();
private int HardwareStations = 2;
private int SoftwareStations = 2;
private int FieldTesting = 2;
Scanner input = new Scanner(System.in);
public String getName() {
return Name;
}
public void setName(String Name) {
//System.out.printf("Please Enter The Name Of The Tournament. It Could be a Number or Words but will be a String\n");
this.Name = Name;
}
public String getDate() {
return Date;
}
public void setDate(String Date) {
//System.out.printf("Please Enter the Date of The Tournament.\n");
this.Date = Date;
}
public ArrayList<String> getTeamNames() {
return TeamNames;
}
public void setTeamNames(int X) {
for (int i = 0; i >= X; i++)
{
int Q = 0;
String C;
System.out.printf("Please Enter the %n Team Names ;\n",Q);
C = input.nextLine();
TeamNames.add(C);
Q=1+Q;
}
}
public String getLocation() {
return Location;
}
public void setLocation(String Location) {
this.Location = Location;
}
public ArrayList<String> getList() {
return list;
}
public void setList(String X) {
list.add(X);
}
public int getHardwareStations() {
return HardwareStations;
}
public void setHardwareStations(int HardwareStations) {
this.HardwareStations = HardwareStations;
}
public int getSoftwareStations() {
return SoftwareStations;
}
public void setSoftwareStations(int SoftwareStations) {
this.SoftwareStations = SoftwareStations;
}
public int getFieldTesting() {
return FieldTesting;
}
public void setFieldTesting(int FieldTesting) {
this.FieldTesting = FieldTesting;
}
public Tournament()
{
}
}
答案 0 :(得分:0)
您的代码可能存在其他问题,但这段代码肯定是其中之一
public void setTeamNames(int X) {
for (int i = 0; i >= X; i++)
{
int Q = 0;
String C;
System.out.printf("Please Enter the %n Team Names ;\n",Q);
C = input.nextLine();
TeamNames.add(C);
Q=1+Q;
}
}
除非X为负或为零,否则将无法满足进入循环的条件。但是如果x为负或为null,我们将进入循环,但永远不会出现。而且我强烈怀疑你是想要这种行为。
同样@JB Nizet提到你应该尊重一些Java编码约定。例如变量局部变量和参数命名,以使您的代码易于阅读(请为此目的编辑它)。对本地变量类实例变量和参数名称使用camelCase约定
示例:建议您写public void setSoftwareStations(int SoftwareStations)
public void setSoftwareStations(int softwareStations)