Я выписал задание, в котором мне нужно напечатать семь разных стран, их «цвет» и «соседей», а затем раскрасить их четырьмя цветами, используя метод. Я понял, как печатать, но не могу понять, как раскрасить один из 4 цветов - синий, желтый, зеленый или красный. Он должен быть случайным, как метод, который проверяет цвет соседей и затем назначает один, так что в конце ни один из одинаковых цветов не касается каждого. Я понимаю, как сделать это в своей голове, но у меня недостаточно навыков и знаний о том, как соединить вещи, поэтому я понятия не имею, как это сделать. Пишу это в BlueJ. Буду очень признателен за помощь, пожалуйста. Спасибо)
import java.util.*;
public class Solve
{
public ArrayList<Country>countries;
public Solve()
{
countries = new ArrayList<>();
countries.add(new Country("Germany", "NONE", "Poland", "Czech Republic", "Austria", "Switzerland", "NONE", "NONE", "NONE"));
countries.add(new Country("Poland", "NONE", "Germany", "Czech Republic", "Slovakia", "NONE", "NONE", "NONE", "NONE"));
countries.add(new Country("Czech Republic", "NONE", "Poland", "Germany", "Austria", "Slovakia", "NONE", "NONE", "NONE"));
countries.add(new Country("Slovakia", "NONE", "Czech Republic", "Hungary", "Austria", "Poland", "NONE", "NONE", "NONE"));
countries.add(new Country("Hungary", "NONE", "Slovenia", "Slovakia", "Austria", "NONE", "NONE", "NONE", "NONE"));
countries.add(new Country("Slovenia", "NONE", "Italy", "Hungary", "Austria", "NONE", "NONE", "NONE", "NONE"));
countries.add(new Country("Italy", "NONE", "Slovenia", "Switzerland", "Austria", "NONE", "NONE", "NONE", "NONE"));
countries.add(new Country("Austria", "NONE", "Italy", "Slovenia", "Hungary", "Slovakia", "Czech Republic", "Germany", "Switzerland"));
countries.add(new Country("Switzerland", "NONE", "Italy", "Austria", "Germany", "NONE", "NONE", "NONE", "NONE"));
}
public void printData()
{
String first = "NONE";
String second = "NONE";
String third = "NONE";
String fourth = "NONE";
String fifth = "NONE";
String sixth = "NONE";
String seventh = "NONE";
String color = "NONE";
for(Country country:countries)
{
color = country.getColor().equals("NONE") ? "" : (country.getColor() + ", ");
first = country.getFirstNeighbor().equals("NONE") ? "" : (country.getFirstNeighbor() + ", ");
second = country.getSecondNeighbor().equals("NONE") ? "" : (country.getSecondNeighbor() + ", ");
third = country.getThirdNeighbor().equals("NONE") ? "" : (country.getThirdNeighbor() + ", ");
fourth= country.getFourthNeighbor().equals("NONE") ? "" : (country.getFourthNeighbor() + ", ");
fifth = country.getFifthNeighbor().equals("NONE") ? "" : (country.getFifthNeighbor() + ", ");
sixth = country.getSixthNeighbor().equals("NONE") ? "" : (country.getSixthNeighbor() + ", ");
seventh = country.getSeventhNeighbor().equals("NONE") ? "" : (country.getSeventhNeighbor() + ", ");
System.out.println("COUNTRY - " + country.getName() + " | COLOR - " + color + " | NEIGHBORS - " + first + second + third + fourth + fifth + sixth + seventh);
}
}
public void printNew()
{
......
}
}
import java.util.*;
public class Country
{
private String name;
private String currentColor;
private String country1;
private String country2;
private String country3;
private String country4;
private String country5;
private String country6;
private String country7;
public Country(String nameIn, String originalColor, String firstCountry, String secondCountry, String thirdCountry, String fourthCountry, String fifthCountry, String sixthCountry, String seventhCountry)
{
name = nameIn;
currentColor = originalColor;
country1 = firstCountry;
country2 = secondCountry;
country3 = thirdCountry;
country4 = fourthCountry;
country5 = fifthCountry;
country6 = sixthCountry;
country7 = seventhCountry;
}
public String getName()
{
return name;
}
public String getColor()
{
return currentColor;
}
public String getFirstNeighbor()
{
return country1;
}
public String getSecondNeighbor()
{
return country2;
}
public String getThirdNeighbor()
{
return country3;
}
public String getFourthNeighbor()
{
return country4;
}
public String getFifthNeighbor()
{
return country5;
}
public String getSixthNeighbor()
{
return country6;
}
public String getSeventhNeighbor()
{
return country7;
}
}