使用炭灰和串的Java岩石纸剪刀蜥蜴Spock

时间:2017-06-16 23:33:20

标签: java

您好,并提前感谢您的帮助。我是Java的新手,在我的第一堂课中已经有几天了。

正如你可以从标题中看出的那样,这是Rock,Paper,Scissors的扩展版本。

我使用string和int作为变量编写了一个很棒的工作程序,但后来被告知我必须将用户输入作为char。现在我遇到一个问题,让用户输入等于计算机输入。

更新:我重写了编写comp的程序。除了领带之外的一切现在都有效。问题是这样的。示例:userInput = r comp = R这应该是一个平局并输出“这是一个平局!”我一直在尝试Charactor.toUpperCase和charactor.toLowerCase,但我不认为我有正确的语法,所以如果userInput = comp,则忽略大小写。任何人都可以给我正确的语法吗?谢谢!

import java.util.Scanner;

public class Rocktest
{
public static void main(String[] args) throws Exception
{
char userInput;
char comp;   
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter R for Rock, P for Paper, S for Scissors, L
for Lizard and K for Spock: ");
userInput = (char)System.in.read();
    keyboard.nextLine();
    int comt=(int)(5*Math.random())+1;
    comp=(' ');

    if (comt==1){
           comp=('R');
    }
    else if (comt==2){
            comp='P';
    }
    else if (comt==3){
            comp='S';
    }

    else if (comt==4){
            comp='L';
    }

    else if (comt==5){
            comp='K';
    }           


    if((userInput == 'R')||( userInput == 'r'))  
         {
          System.out.println("You choose: Rock");

    }
    else if((userInput == 'P')||(userInput == 'p')) 
          {
            System.out.println("You choose: Paper");

    }
       if((userInput == 'S')||( userInput == 's'))  
         {
          System.out.println("You choose: Scissors");

    }      
        if((userInput == 'L')||( userInput == 'l'))  
         {
          System.out.println("You choose: Lizard");              
         }            

        if((userInput == 'K')||( userInput == 'k'))  
         {
          System.out.println("You choose: Spock"); 
        }                         

   System.out.println("I choose: " + comp); //This is where I'm having the                 
                                            //problem
                                           //need to ignore case
       if (userInput==comp)                             
       {
        System.out.println("A tie!");
        }

      else if((userInput == 'R')||( userInput == 'r')) {
             if (comp == 'S')
                    System.out.println("Rock crushes scissor - you win!");
            else if (comp == 'L')
                    System.out.println("Rock crushes Lizard - you win!");
            else if (comp == 'P')
                    System.out.println("Paper covers rock - you lose!");
            else if (comp == 'K')
                    System.out.println("Spock vaporizes rock - you lose!");
    }
    else if ((userInput == 'P')||(userInput == 'p')) {
            if (comp == 'S')
                    System.out.println("Scissors cut paper- you lose!");
            else if (comp == 'K')
                    System.out.println("Paper disproves Spock - you win!");
            else if (comp == 'R')
                    System.out.println("Paper covers rock - you win!");
            if (comp == 'L')
                    System.out.println("Lizard eats paper- you lose!");
    }        
    else if((userInput == 'S')||( userInput == 's')) {
            if (comp == 'P')
                    System.out.println("Scissors cut paper - you win!");
            else if (comp == 'L')
                    System.out.println("Scissors decapitates lizard - you 
            win!");
            else if (comp == 'R')
                    System.out.println("Rock crushes scissors - you lose!");
            else if (comp == 'K')
                    System.out.println("Spock smashes scissors - you lose!");
    }        
    else if((userInput == 'L')||( userInput == 'l'))  {
            if (comp == 'P')
                    System.out.println("Lizard eats paper - you win!");
            else if (comp == 'K')
                    System.out.println("Lizard poisons Spock - you win!");
            else if (comp == 'R')
                    System.out.println("Rock crushes lizard - you lose!");
            else if (comp == 'S')
                    System.out.println("Scissors decapatates lizard - you
            lose!");
    }    
    else if ((userInput == 'K')||( userInput == 'k')) {
            if (comp == 'P')
                    System.out.println("Paper disproves Spock - you lose!");
            else if (comp == 'L')
                    System.out.println("Lizard poisons Spock - you lose!");
            else if (comp == 'R')
                    System.out.println("Spock vaporizes rock - you win!");
            else if (comp == 'S')
                    System.out.println("Spock smashes scissors - you lose!");
    }  
    //System.out.println(+ comt);    used these lines for testing
    //System.out.println(+ comp);
    //System.out.println(+ userInput);

  }//end main
}//end class   

1 个答案:

答案 0 :(得分:4)

好的,所以我相信我找到了你的问题。

在您的代码中,假设变量if (comp == 'S')comp,您可以使用行char,它可以正常工作。但compString时,它永远不会等于char S或变量userInput,它也是char

简单修复:更改comp的变量类型。

那就是它。我希望这有帮助,祝你在java世界里好运。