0

저는 게임의 게임을 게임의 초기 단계로 만들고 있습니다. 프로그램을 실행하고 "만들고 싶습니까?"라는 메시지가 나타나고 "y"를 입력하면 else로 이동하여 test3의 테스트 문을 인쇄하고 프로그램을 종료합니다. 나는 무엇을 간과하고 있는가? 자바에서 문자열 비교를 들어이 if 문이 내가 원하는 방식으로 수행하지 않는 이유는 무엇입니까?

public static void main(String[] args) { 

    Scanner kb = new Scanner(System.in); 

    String userInput = ""; 
    char[][] initialGrid = new char[25][75]; 
    char[][] world = makeInitialGrid(kb, userInput, initialGrid); 
    printGrid(world); 
    userInput = "y"; 
    while (userInput == "y"){ 
     System.out.println("Do you want to make a new generation? (y) yes (n) no"); 
     userInput = kb.nextLine(); 
     System.out.println(userInput); 
     if (userInput == "y"){ 
      System.out.println("test1"); 
      int numOfNeighbors = findNeighbors(world, 6, 2); 
      System.out.println("test2"); 
      System.out.println(numOfNeighbors); 
      //makeNewGeneration(world); 
     } else { 
      System.out.println("test3"); 
      break; 

     } 
    } 
    kb.close(); 

답변

2

, 당신은 String#equals하지 == 사용해야합니다. 대신 if (userInput.equals("y")) { ...을 시도하십시오.

+0

오오 물론 ... 물론. 고맙습니다. –

+0

괜찮습니다! 귀하의 질문이 해결되면 답변을 수락하는 것을 잊지 마십시오. – Gian

관련 문제