Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 2044|回复: 3
打印 上一主题 下一主题

请大家帮帮忙

[复制链接]

33

主题

15

好友

964

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

跳转到指定楼层
1#
发表于 2011-9-8 09:50 PM |只看该作者 |倒序浏览
:Create a game in which let the user enter rock, paper or scissors, and  the computer randomly chooses rock, paper or scissors.  Then determine the winner.

Your games shall :
•Make sure the game works correctly whether the player enter a choice  in uppercase or lowercase letters or a combination of the two
•To allow for player misspellings, accept the player’s entry as long as the first two letters are correct. (In other words, if a player types “scixxrs”, you will accept it as “scissors”, because at least the first two letter are correct.
•When the player does not type at least the first two letters of the choice correctly, reprompt the player and continue to do so until the player’s entry contains at least the first two letters of one of the options
•Allow 10 complete rounds of the games. At the end, display counts of the number of times the player won, the number of times the computer won, and the number of tie games.
[Hints : Math.random() return a value of type double equal to 0.0 or less than 1.0 .  To find absolute value of num-guess, use the expression Math.abs(num-guess)]

不知道我做对吗,请大家改正我,如不对的话。


package javaapplication4;
import java.io.*;
public class JavaApplication4 {
public static void main(String[] args) {
  
  int userScore = 0;
int compScore = 0;
String userMove, compMove;
int winner;
System.out.println("ROCK - PAPER - SCISSORS");
System.out.println("This game plays 10 rounds.");
for (int rnd=1; rnd<=10; rnd++) {
userMove = getUserMove();
compMove = getComputerMove();
System.out.println("Computer's move: " + compMove);
winner = determineRoundWinner(compMove, userMove);
   if (winner == 1) {
System.out.println(compMove + " beats " +  userMove +   " the computer wins this round.");
compScore++;
  }
else if (winner == -1) {
System.out.println(userMove + " beats " +  compMove +  " the user wins this round.");
userScore++;
           }
          else {
System.out.println(userMove + " ties " + compMove + " nobody wins this round.");
            }
System.out.println("Score: User=" + userScore +  " Computer=" + compScore);
            System.out.println();
        }
displayGameWinner(userScore, compScore);
    }
  static String getUserMove() {
String userMove = "club";
while (!userMove.equals("ROCK") && !userMove.equals("PAPER") &&!userMove.equals("SCISSORS")) {
System.out.print("Enter your move " + "[ROCK, PAPER, SCISSORS]: ");
userMove =  Console.in.readLine();
userMove = userMove.toUpperCase();
        }
return userMove;
    }
  static String getComputerMove() {
int compMoveInt;
String compMove;
compMoveInt = randomInt(1,3);
if (compMoveInt == 1) {
compMove = "ROCK";
        }
else if (compMoveInt == 2) {
compMove = "PAPER";
}
else {
  compMove = "SCISSORS";
}
return compMove;
    }
    public static int randomInt(int lowEnd, int highEnd) {
        int theNum;
        theNum = (int)(Math.random() * (highEnd - lowEnd + 1)) + lowEnd;
return theNum;
    }
static int determineRoundWinner(String userMove,
String compMove) {
        int winner;
if (compMove.equals(userMove)) {
  winner = 0;
        }
else if (compMove.equals("ROCK") && userMove.equals("SCISSORS")) {
winner = 1;
}
else if (compMove.equals("PAPER") &&
userMove.equals("ROCK")) {
  winner = 1;
}
else if (compMove.equals("SCISSORS") &&
userMove.equals("PAPER")) {
winner = 1;
}
else {
winner = -1;
}

return winner;
  }

static void displayGameWinner(int userScore,
int compScore) {
System.out.println("\n\nFinal Score:");
System.out.println("       User=" + userScore +  "   Computer=" + compScore);
System.out.println();
if (userScore > compScore) {
System.out.println("The user wins!");}
else if (compScore > userScore) {
System.out.println("The computer wins!");
}
else {
  System.out.println("Its a tie, nobody wins.");
}  
        
}
    }




收藏收藏0

17

主题

0

好友

79

积分

中级会员

Rank: 2Rank: 2

2#
发表于 2011-9-9 02:29 AM |只看该作者
JAVA!!!这个嘛.....不懂
以前读书的时候看到这个就想到


回复

使用道具 举报

8

主题

0

好友

132

积分

高级会员

Rank: 3Rank: 3Rank: 3

3#
发表于 2011-9-28 11:15 PM |只看该作者
好乱哦@@。还没学到package的code statement.


回复

使用道具 举报

31

主题

0

好友

1228

积分

黄金长老

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

4#
发表于 2011-9-29 11:38 AM |只看该作者
想知道自己做的对不对,最容易的方法就是自己 compile 来 run 看。
没有人会喜欢看那么一大堆 code 帮你 “检查答案” 的。


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-12-26 12:41 PM , Processed in 0.122209 second(s), 27 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部