Использование .toUpperCase () в сочетании с .include ()

Ваш код немного креативен, немного сумасшедший и с логикой, с которой очень сложно следовать. Самый необычный аспект заключается в том, что он имеет два JFrames, один называется «frame», и один сам объект GameFrame, оба из которых добавляют компоненты, но только один из них показывает. У вас также есть много методов, которые возвращают void (что при чрезмерном использовании увеличивает запах кода) и только добавляет, чтобы сделать код более запутанным.

Например,

public GameFrame(char x) {

  // here you set up the "frame" JFrame
  frame.setSize(1024, 768);
  frame.setTitle("Game");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  FlowLayout layout = new FlowLayout();
  createPanels(x);
  healthPanel.setLayout(layout);
  buttonPanel.setLayout(layout);
  mainPanel.setLayout(layout);

  // here you add content to the frame JFrame, and pack it
  frame.getContentPane().add(mainPanel);
  frame.pack();
  repaint();  // and then call repaint on the "this" JFrame?
}

public RectangleComponent getLife() {
  return life;
}

private void createHealth() {
  life = new RectangleComponent(green, healthPanel);
  death = new RectangleComponent(red, healthPanel);
}

private void createPanels(char x) {
  add(healthPanel); // now you add content to the "this" JFrame
  pack();  // and pack it
  createBar(x);
  createHealth();
  mainPanel.add(buttonPanel);
  mainPanel.add(healthPanel); // and then re-add a JPanel into a second JPanel?
  healthPanel.add(death);
  healthPanel.add(life);
  buttonPanel.add(bar.getSpell1());
  buttonPanel.add(bar.getSpell2());
  buttonPanel.add(bar.getSpell3());
  add(mainPanel); // and then re -add the mainPanel into the "this" JFrame???
}

Это все очень запутанное и вряд ли сработает.

Затем вы пытаетесь напрямую вызвать paintComponent и вызывать getGraphics на JComponent, оба из которых не должны выполняться.

Я рекомендую рассмотреть возможность повторного написания этого и, прежде всего, использовать только один JFrame и лучше организовать ваш код.

0
задан Jevon Cochran 18 January 2019 в 03:57
поделиться

3 ответа

Вы используете toUpperCase() в массиве euroTour, что недопустимо - это возможно только со строками. Сначала map массив в верхний регистр:

euroTour = euroTour.map(e => e.toUpperCase());

Затем вы можете использовать .includes() для этого:

euroTour.includes("FRANCE");

Демонстрация:

[ 1113]
var euroTour = ["France", "the Netherlands", "the UK", "Spain",
  "Portugal"
];

euroTour = euroTour.map(e => e.toUpperCase());

var findCountry = euroTour.includes("FRANCE");

0
ответ дан Jack Bashford 18 January 2019 в 03:57
поделиться

includes не обеспечивает регистронезависимый вариант. Вы можете использовать some совместно с регулярным выражением без учета регистра, чтобы выполнить то, что вы хотите:

var euroTour = ["France", "the Netherlands", "the UK", "Spain", "Portugal"]

console.log(euroTour.some(country => /^france$/i.test(country)))

0
ответ дан ic3b3rg 18 January 2019 в 03:57
поделиться

var euroTour = ["France", "the Netherlands", "the UK", "Spain", "Portugal"];


var findCountry = euroTour
  .map(country => country.toUpperCase())
  .includes('FRANCE');
 
console.log(findCountry);
 
 // A More General Solution
 
 function includesStrCaseInsensitive(arr, str){
  return arr
    .map(i => i.toUpperCase())
    .includes(str.toUpperCase());
}

console.log(includesStrCaseInsensitive(euroTour, 'FRANCE'));

console.log(includesStrCaseInsensitive(euroTour, 'FrANCE'));

0
ответ дан t.klein 18 January 2019 в 03:57
поделиться
Другие вопросы по тегам:

Похожие вопросы: