当前在线人数13784
首页 - 分类讨论区 - 电脑网络 - 爪哇娇娃版 - 同主题阅读文章

此篇文章共收到打赏
0

  • 10
  • 20
  • 50
  • 100
您目前伪币余额:0
未名交友
[更多]
[更多]
rock paper scissor 求教
[版面:爪哇娇娃][首篇作者:newuxi] , 2015年06月15日14:45:32 ,692次阅读,3次回复
来APP回复,赚取更多伪币 关注本站公众号:
[分页:1 ]
newuxi
进入未名形象秀
我的博客
[回复] [回信给作者] [本篇全文] [本讨论区] [修改] [删除] [转寄] [转贴] [收藏] [举报] [ 1 ]

发信人: newuxi ( 无锡土人), 信区: Java
标  题: rock paper scissor 求教
发信站: BBS 未名空间站 (Mon Jun 15 14:45:32 2015, 美东)


主要是第一个comment那边,输入yes or no 然后取出第一个char赋值给tryAgain,我
用了tryAgain = scan.findInLine(".").charAt(0); 跑下来是出现一个框,根本就没
有赋值给tryAgain,求大神帮忙指点下,我哪里出错了。另外我用switch没问题吧



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

public class RockPaperScissors {
 
  private static String[] choices = { "Rock", "Paper", "Scissors" };
 
  public static void main (String[] args){
   
    Scanner scan = new Scanner(System.in);
    Random rGen = new Random();
   
    String name;
    int playerChoiceNum;
    String playerChoice;
   
    int computerChoiceNum;
    String computerChoice;
   
    char tryAgain;
    String playerMessage = null;
   
    System.out.print("What is your name? ");
    name = scan.nextLine();
    System.out.println();
   
    System.out.print("1 = Rockn2 = Papern3 = ScissorsnPlease choose 1, 2, or
3: ");
    playerChoiceNum = scan.nextInt(); scan.nextLine();
    playerChoice = choices[playerChoiceNum - 1];
    System.out.println();
   
    computerChoiceNum = Math.abs(rGen.nextInt()) % 3 + 1;
    computerChoice = choices[computerChoiceNum - 1];
   
    // If the player and computer are tied OR the
    // computer is beating the player, then...
    //   Ask the user if he is sure about his choice,
    //      as a yes/no
    //   Store the first character of the user's
    //      response in the variable tryAgain
    //   If the answer is no, then...
    //      Repeat the lines of code that got the
    //         user's choice the first time
   
    // YOUR CODE GOES HERE
    switch(computerChoiceNum - playerChoiceNum)
    {
      case -2:
        System.out.println(name +", are you Sure you want to choose " +
playerChoice + "?" + "(Yes or No)");
        break;
      case 0:
        System.out.println(name +", are you Sure you want to choose " +
playerChoice + "?" + "(Yes or No)");
     
        break;
      case 1:
        System.out.println(name +", are you Sure you want to choose " +
playerChoice + "?" + "(Yes or No)");
       
        break;
    }
   
     scan.nextLine();

  
    tryAgain = scan.findInLine(".").charAt(0);
    do {
     System.out.print("1 = Rockn2 = Papern3 = ScissorsnPlease choose 1, 2,
or 3: ");
     playerChoiceNum = scan.nextInt(); scan.nextLine();
     playerChoice = choices[playerChoiceNum - 1];
     System.out.println();
   
    computerChoiceNum = Math.abs(rGen.nextInt()) % 3 + 1;
    computerChoice = choices[computerChoiceNum - 1];
   }
    while (tryAgain =='N');
     
 
   
   
    System.out.printf("%s, you chose %s.%n", name, playerChoice);
    System.out.printf("The computer chose %s.%n", computerChoice);
    System.out.println();
   
    // If the player and computer made the same choice, then...
    //    Announce that it's a tie.
    // Otherwise, based upon the player's and computer's
    //    respective choices, announce what beats what and
    //    and tell the player whether he wins or loses.

    // YOUR CODE GOES HERE
    switch (playerChoiceNum - computerChoiceNum)
    { case -2 :
        System.out.println("Rock breaks Scissors...you win!");
        break;
      case -1 :
        if(playerChoiceNum == 2 )
          System.out.println("Scissors cut Paper...you lost!");
        else
          System.out.println("Paper covers Rock...you lost!");
        break;
     
      case 0 :
        System.out.println(name + " you and the computer both chose " +
playerChoiceNum + ", so it's a tie.");
        break;
      case 1 :
        if(playerChoiceNum == 3)
          System.out.println("Scissors cut Paper...you win!");
        else
          System.out.println("Paper covers Rock...you win!");
        break;
     
      case 2 :
        System.out.println("Roch breaks Scissors...you lost!");
        break;
    }






   
   
    System.out.println(playerMessage);
   
  }
 
}
--
※ 来源:·WWW 未名空间站 网址:mitbbs.com 移动:在应用商店搜索未名空间·[FROM: 73.]

 
briteguy
进入未名形象秀
我的博客
[回复] [回信给作者] [本篇全文] [本讨论区] [修改] [删除] [转寄] [转贴] [收藏] [举报] [ 2 ]

发信人: briteguy (FOCUS), 信区: Java
标  题: Re: rock paper scissor 求教
发信站: BBS 未名空间站 (Wed Jun 24 02:25:45 2015, 美东)

这么长的程序,要网上的人免费给回答的话,估计很难了。这种应该去找个tutor解决

--
※ 来源:·WWW 未名空间站 网址:mitbbs.com 移动:在应用商店搜索未名空间·[FROM: 50.]

 
meiyoutu
进入未名形象秀
我的博客
[回复] [回信给作者] [本篇全文] [本讨论区] [修改] [删除] [转寄] [转贴] [收藏] [举报] [ 3 ]

发信人: meiyoutu (没有图-牛一刀), 信区: Java
标  题: Re: rock paper scissor 求教
发信站: BBS 未名空间站 (Fri Jun 26 03:05:23 2015, 美东)

没有parse输入成int,是一个很明显的error。另外,“1=papern....",每个选项之后
都有个n,这不知是个typo(你贴到这里的typo),还是你运行程序就这样的。我觉得
这也应该是个error。
--
※ 来源:·WWW 未名空间站 网址:mitbbs.com 移动:在应用商店搜索未名空间·[FROM: 108.]

 
bobohu
进入未名形象秀
我的博客
[回复] [回信给作者] [本篇全文] [本讨论区] [修改] [删除] [转寄] [转贴] [收藏] [举报] [ 4 ]

发信人: bobohu (bobohu), 信区: Java
标  题: Re: rock paper scissor 求教
发信站: BBS 未名空间站 (Fri Sep  4 17:15:39 2015, 美东)

经典的解法是用double dispatch pattern.
--
※ 来源:·WWW 未名空间站 网址:mitbbs.com 移动:在应用商店搜索未名空间·[FROM: 95.]

[分页:1 ]
[快速返回] [ 进入爪哇娇娃讨论区] [返回顶部]
回复文章
标题:
内 容:

未名交友
将您的链接放在这儿

友情链接


 

Site Map - Contact Us - Terms and Conditions - Privacy Policy

版权所有,未名空间(mitbbs.com),since 1996