2016-09-30 40 views
-2

我對上述問題持續發生錯誤。請幫助..... 我必須在下週五之前將此代碼作爲我項目的一部分提交。我在網上看過其他程序,但都使用緩衝區讀取器,所以我不能理解。在bluej中獲得「非法表達式開始」錯誤

import java.util.*; 
//HANGMAN 
//ANSH DAWDA XA 
class hangman 
{ 
Scanner S=new Scanner(System.in); 
String A,W,word,clue; 
void h1() 
{ 
    System.out.println("_____________"); 
    System.out.println(" |   |"); 
    System.out.println(" |"); 
    System.out.println(" |"); 
    System.out.println(" |"); 
    System.out.println(" |"); 
    System.out.println(" |"); 
    System.out.println(" |"); 
    System.out.println("---"); 
} 

void h2() 
{ 
    System.out.println("_____________"); 
    System.out.println(" |   |"); 
    System.out.println(" |   ()"); 
    System.out.println(" |"); 
    System.out.println(" |"); 
    System.out.println(" |"); 
    System.out.println(" |"); 
    System.out.println(" |"); 
    System.out.println("---"); 
} 

void h3() 
{ 
    System.out.println("_____________"); 
    System.out.println(" |   |"); 
    System.out.println(" |   ()"); 
    System.out.println(" |   |"); 
    System.out.println(" |   |"); 
    System.out.println(" |   |"); 
    System.out.println(" |"); 
    System.out.println(" |"); 
    System.out.println("---"); 
} 

void h4() 
{ 
    System.out.println("_____________"); 
    System.out.println(" |   |"); 
    System.out.println(" |   ()"); 
    System.out.println(" |   |"); 
    System.out.println(" |   |"); 
    System.out.println(" |   |"); 
    System.out.println(" |   /"); 
    System.out.println(" |   |"); 
    System.out.println("---"); 
} 

void h5() 
{ 
    System.out.println("_____________"); 
    System.out.println(" |   |"); 
    System.out.println(" |   ()"); 
    System.out.println(" |   |"); 
    System.out.println(" |   |"); 
    System.out.println(" |   |"); 
    System.out.println(" |  /\\"); 
    System.out.println(" |   | |"); 
    System.out.println("---"); 
} 

void h6() 
{ 
    System.out.println("_____________"); 
    System.out.println(" |   |"); 
    System.out.println(" |   ()"); 
    System.out.println(" |   |"); 
    System.out.println(" |   /|"); 
    System.out.println(" |  /|"); 
    System.out.println(" |  /\\"); 
    System.out.println(" |   | |"); 
    System.out.println("---"); 
} 

void h7() 
{ 
    System.out.println("_____________"); 
    System.out.println(" |   |"); 
    System.out.println(" |   ()"); 
    System.out.println(" |   |"); 
    System.out.println(" |   /|\\"); 
    System.out.println(" |  /| \\"); 
    System.out.println(" |  /\\"); 
    System.out.println(" |   | |"); 
    System.out.println("---"); 
} 
int option; 
void sports() 
{ 
    option=(int)(Math.random()*10); 
    switch(option) 
    { case 0: 
     { 
      clue = "He is an Argentine footballer who plays for La Liga club FC Barcelona and is the captain of the Argentina national team, playing mainly as a forward."; 
      word = "LIONEL MESSI"; 
     } 
     break; 
     case 1: 
     { 
      clue = "First person to score 200 in ODIs"; 
      word = "SACHIN TENDULKAR"; 
      break; 
     } 
     case 2: 
     { 
      clue = "It is a South Asian team sport."; 
      word = "KABADDI"; 
     } 
     break; 
     case 3: 
     { 
      clue = "It is a twoplayer board game"; 
      word = "CHESS"; 
     } 
     break; 
     case 4: 
     { 
      clue = "It is a team sport in which two teams of six players are separated by a net."; 
      word = "VOLLEYBALL"; 
     } 
     break; 
     case 5: 
     { 
      clue = "Known as 'The wall'"; 
      word = "RAHUL DRAVID"; 
     } 
     break; 
     case 6: 
     { 
      clue = "World Table Tennis Champion"; 
      word = "ZHANG JIKE"; 
     } 
     break; 
     case 7: 
     { 
      clue = "A famous sport"; 
      word = "FOOTBALL"; 
     } 
     break; 
     case 8: 
     { 
      clue = "Played on a hard board divided by a net"; 
      word = "TABLE TENNIS"; 
     } 
     break; 
     case 9: 
     { 
      clue = "Grandmaster of chess"; 
      word = "VISHWANATAN ANAND"; 
     } 
     break; 
    } 
    return String {word,clue}; 
} 

}

+1

@Simze爲什麼?這很好。問題是最後一個方法最後的return語句。 – f1sh

+0

@Ansh你的方法''體育''有返回類型的void,這意味着你不能返回任何東西。然而,這就是你在這裏嘗試的:''返回String {word,clue};''。這個陳述也不是一個有效的表達。 – f1sh

+0

對不起我的壞!已刪除評論 –

回答

0

請更改功能

void sports() { 

    // Code 

     return String {word,clue}; 
    } 

要:

String[] sports() { 

     // Code 
     return new String[]{word, clue}; 
    } 

這意味着你只需要改變的功能運動的返回類型,並返回一個有效的對象。