2013-10-24 11 views
0

문자열 초기화를 시도했지만 아무 소용이 없습니다. 나는 모든 솔루션을 시험해 보았습니다. 나는 그것이 부적절한 것인지, 아니면 새로운 솔루션이 필요한지 확신 할 수 없습니다. 이미 프로그램의 논리를 알아 냈기 때문에, 그저 초기화를 시도하는 데 도움이 필요합니다. 문자열 값. 누구든지 도울 수 있다면, 나는 많은 의무가있을 것이다!문자열 변수가 초기화되지 않았을 수 있습니다 (줄 34에 오류가 있음)

P.S 도전을 원하고 srings를 사용하여 나를 저주합니다. -_-

import java.util.Scanner; 
import java.util.Random; 

public class RockPaperScissors 
{ 
public static void main (String[] args) 
{ 
    String player, computer; 
    int answer; 
    Scanner scan = new Scanner (System.in); 

    Random generator = new Random(); 
    answer = generator.nextInt(3) + 1; 

    if (answer < 1 || answer > 3) 
      answer = generator.nextInt(3) + 1; 

    if (answer == 1) 
     computer = "rock"; 
    if (answer == 2) 
     computer = "paper"; 
    if (answer == 3) 
     computer = "scissors"; 

    System.out.println ("Please choose rock, paper, or scissors."); 
    player = scan.nextLine(); 

    if (!player.equalsIgnoreCase("rock") || !player.equalsIgnoreCase("paper") 
      || !player.equalsIgnoreCase("scissors")) 
    { 
     System.out.println ("Please correctly enter one of the three 
        choices: rock, paper, and scissors."); 
     player = scan.nextLine(); 
    } 

    if (player.compareTo(computer) == 0) 
     System.out.println ("It's a draw! You both chose " + player + 
        "!"); 

    if ((computer.equalsIgnoreCase("rock") && 
      player.equalsIgnoreCase("scissors")) && 
      (computer.equalsIgnoreCase("scissors") && player.equalsIgnoreCase("paper")) 
      && (computer.equalsIgnoreCase("paper") && player.equalsIgnoreCase("rock"))) 
     System.out.println ("You lost! The computer chose " + computer + " 
        and you chose " + player + "."); 

    if ((player.equalsIgnoreCase("rock") && 
      computer.equalsIgnoreCase("scissors")) && 
      (player.equalsIgnoreCase("scissors") && 
      computer.equalsIgnoreCase("paper")) && 
      (player.equalsIgnoreCase("paper") &&  
      computer.equalsIgnoreCase("rock"))) 
      System.out.println ("You won! CONGRATULATIONS! The computer chose    
        " + computer + " and you chose " + player + "."); 

} 

}

+1

그래서 선언 할 때 문자열 변수를 ""로 설정하십시오. – OldProgrammer

답변

1
String computer; // not initialized 

변경이 가변 컴퓨터가 조건에 기초하여 할당되므로

String computer = null; or ""// initialized 
if (player.compareTo(computer) == 0) 

한다. 위에서 언급 한 조건이 만족스럽지 않은 경우 값은 none이므로 오류 만 표시합니다.

0

나는 모든 질문을 이해한다면, 염두에 두어야보다 당신은 자바에서이 작업을 수행 할 수 있습니다 : 당신이 할 수있는 일

String str = "here is some 
    random text"; 

은 즉

String str = "here is some " + 
    "random text"; 

입니다 - 수 ' 줄 끝에서 문자열을 끊어야하고 닫고 + 부호를 사용하십시오.

0

두 가지 방법으로 Strings의 값을 초기화 할 수 있습니다 :

String abc = "I love basketball"; 

또는

String abc = new String("I love basketball"); 

은 당신이보고있는 것은 경고가 아닌 오류입니다. 당신이 String에 값을 넣어하지 주장, 그냥

String abc = null;  

또는 후자의 경우

String abc = "" 

, 당신의 문자열은 인용 사이의 문자로 초기화하면된다. 아무것도 아니지만 기본적으로는 null이 아닙니다.

"rock".equalsIgnoreCase(playerChoice); 
1

compilator은 문자열의 컴퓨터를보고 다음과 같이 잠재적 인 NPE 문제를 방지하기 위해

String의 비교. 그는 또한이 컴퓨터가 1 또는 2 또는 3의 응답으로 초기화되었음을 알 수 있습니다. 그러나 읽기 논리가 없기 때문에 대답이 낮거나 높을 수 있는지 여부를 알지 못하므로 최악의 가능성을 가정합니다. 초기화되지 않습니다.

그냥

String computer = ""; 

에 접속 라인을 변경하고 그것을 잘해야합니다.

관련 문제