[英]Properties for Each Team in ArrayLists
所以我有一個包含“比賽信息”的類。 我有一個采用團隊名稱的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()
{
}
}
您的代碼可能還有其他問題,但是這段代碼肯定是其中之一
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)
public void setSoftwareStations(int softwareStations)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.