|登录 |注册

查看: 2235|回复: 3
打印 上一主题 下一主题

请大家帮帮忙

[复制链接]
seongchog
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.");
}  
        
}
    }
San28
2011-9-9 02:29 AM
JAVA!!!这个嘛.....不懂
以前读书的时候看到这个就想到
冥妖
2011-9-28 11:15 PM
好乱哦@@。还没学到package的code statement.
~Zero
2011-9-29 11:38 AM
想知道自己做的对不对,最容易的方法就是自己 compile 来 run 看。
没有人会喜欢看那么一大堆 code 帮你 “检查答案” 的。
您需要登录后才可以回帖 登录 | 注册

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

GMT+8, 2026-1-1 05:39 AM , Processed in 0.099286 second(s), 25 queries .

Powered by Discuz! X2.5 © 2001-2012 Comsenz Inc.

本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved

Dedicated Server powered by iCore Technology Sdn. Bhd.

合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部