2016-09-28 68 views
1
public static char getChoice(char choice) { 
    System.out.println("\nTake or stop? (t/s): "); 
    choice = sc.next() 
     .charAt(0); 
    if (choice == 't') { 
     System.out.print("Player Took"); 
     choice = 't'; 

    } else if (choice == 's') { 
     System.out.println("Player didnt take"); 
     choice = 's'; 

    } 
    return choice; 
    //return answer; 
} 

这是主要的,多数民众赞成在调用它方法不返回正确的值(Java)的

 //Checking if players score is 21 when the game begins 
    if(ptotal == 21) 
    { 
     System.out.print("\nPlayer wins!"); 
    } 
    //Checking if the computers score is 21 when the game 
    else if(ctotal == 21) 
    { 
     System.out.print("\nComputer wins!"); 
    } 
    //Checking if both scores are 21 which would be a tie 
    else if(ctotal == 21 && ptotal == 21) 
    { 
     System.out.print("\nTie!"); 
    } 
    //If none above is true it will proceed to the loop 
    else 
    { 
     //getChoice(answer); 
     while(ptotal < 21 && answer == 't')//Loop conditions 
     { 
      getChoice(answer); 
      //Prompting user if they would like to take or stop 
      /*System.out.println(); 
      System.out.print("Take or stop? (t/s): "); 
      answer = sc.next().charAt(0); 
      System.out.println("");*/ 


      //Checking if the input is 't' 
      if(answer == 't') 
      { 
       //If true another card will be generated and added to the total value 
       System.out.print("\nPlayer draws: "); 
       pCard = getCard(); 
       showCard(pCard); 
       ptotal += cardValue(pCard); 

      } 
      //Checking if input is 's' 
      else if(answer == 's') 
      { 
       System.out.println("Player Stops"); 
      } 
     } 

这是应该要求用户输入一个字符输入,然后返回一个特定字符的部分取决于if语句,但每次它返回它一旦我从主要调用它不会显示这些字符串,但它不会选择正确的字符。请帮助。

+0

如果'choice'是't'或's',为什么你需要重新分配它到同一个东西?在这种情况下通过字符也是不切实际的。 – Li357

+0

我应该怎么做,因为我想要做的就是获得输入,然后在主要使用时输入相应的输入 – Noobgrammer

+0

我在这里看不到任何代码问题。发布调用'getChoice()'方法的代码 –

回答

1
//always post an MCVE 
//see http://stackoverflow.com/help/mcve 
import java.util.Scanner; 

public class Test{ 

    public static void main(String[] args){ 

     char c = getChoice(); 
     System.out.println("returned value is "+ c); 
    } 

    //why pass a value choise if you read it from user ? 
    public static char getChoice(/*char choice*/) 
    { 
     System.out.println("\nTake or stop? (t/s): "); 

     Scanner sc = new Scanner(System.in); 
     char choice = sc.next().charAt(0); 
     if(choice == 't') 
     { 
      System.out.println("Player Took"); 
      // choice is already equal to `t`. This is not needed 
      // choice = 't'; 

     } 

     else if(choice == 's') 
     { 
      System.out.println("Player didnt take"); 
      // choice is already equal to `s`. This is not needed 
      choice = 's'; 

     } 
     return choice; 
    } 
}