2016-02-27 43 views
1

所以我今天的任務是使用方法做一個石頭紙剪刀遊戲。我的代碼的第一個問題是,我需要它來詢問用戶是否要再次玩。 (y或n)另外,我需要實現得分,如果用戶獲勝,則得1分,如果失敗,則爲-1,並且每次他們再次玩都將該得分添加到總得分。關於如何實現這個的任何想法?或者我需要更改爲我的代碼。另外,我是一個新手,對於醜陋的格式感到抱歉,並隨時批評每一個小細節,因此我不能吸收所有可能的信息。巖紙剪刀簡單的java使用方法

public static void main(String[] args){ 
boolean tie = true; 
    do{ 
     String computer = computerChoice(); 
     String user = userChoice(); 
     tie = (computer.compareTo(user) == 0); 
     determineWinner(computer, user); 
    }while(tie); 

    } 

    public static String computerChoice(){ 
    Random rand = new Random(); 
int cinput = rand.nextInt(3)+ 1; 
    String computer = "thing"; 
    if (cinput == 1) 
    computer = "Rock"; 
    if (cinput == 2) 
    computer = "Paper"; 
if (cinput == 3) 
    computer = "Scissors"; 
    return computer; 
    } 
    public static String userChoice(){ 
    Scanner sc = new Scanner (System.in); 
    String user = "default"; 
    do{ 
    System.out.println("choose your weapon(Paper,Scissors or Rock)"); 
    user = sc.nextLine(); 
    } 
    while (isValidChoice (user) == false); 
    return user; 
    } 
    public static boolean isValidChoice(String choice){ 
    boolean status; 
    if (choice.compareTo("Rock")== 0) 
    status = true; 
    else if (choice.compareTo("Paper")== 0) 
    status = true; 
    else if (choice.compareTo("Scissors")== 0) 
    status = true; 
    else{ 
    status = false; 
    System.out.println("Error! Make sure you are capitalizing your     choices"); 
} 

return status; 
    } 
    public static boolean determineWinner(String computer, String user){ 
    System.out.println (" Computer Choice: " + computer); 
    System.out.println ("Your Choice : " + user); 
    if (computer.compareTo("Rock") == 0 && user.compareTo ("Scissors") == 0) 
    System.out.println (" Computer wins! Better luck next time!"); 
    if (computer.compareTo("Scissors")== 0 && user.compareTo("Paper") == 0) 
    System.out.println (" Computer wins! Better luck next time!"); 
    if (computer.compareTo("Paper") == 0 && user.compareTo("Rock") == 0) 
    System.out.println (" Computer wins! Better luck next time!"); 
    if (computer.compareTo("Rock") == 0 && user.compareTo("Paper") == 0) 
    System.out.println (" You win!!"); 
    if (computer.compareTo("Scissors") == 0 && user.compareTo("Rock") == 0) 
    System.out.println (" You win!!"); 
    if (computer.compareTo("Paper") == 0 && user.compareTo("Scissors") == 0) 
    System.out.println (" You win!!"); 
    else if (computer.compareTo(user) == 0){ 
    System.out.println(" Tie! the game must be played again."); 
    return false; 
    } 
    return true; 
} 
} 

,我的教授給我們作爲一個例子輸出爲:

Choose your weapon! 
1. Paper 
2. Scissors 
3. Rock 
5 
Choose your weapon! 
1. Paper 
2. Scissors 
3. Rock 
    1 
You chose paper! 
I choose rock! 
I have been vanquished! 
We have matched wits 1 times, and your score is 1 
Do you want to play again (y or n)? y 
Choose your weapon! 
1. Paper 
2. Scissors 
3. Rock 
1 
You chose paper! 
    I choose paper! 
    We are equally matched. You are a worthy adversary. 
    We have matched wits 2 times, and your score is 1 
    Do you want to play again (y or n)? n 
+2

你知道怎麼反覆要求一個「武器」,但不知道怎麼反覆要求再玩一次? –

+1

使用.equals()可以更好地檢查響應 – WIR3D

+0

@ScottHunter,所以我只需製作另一種方法並讓它們再次播放? – Progamminnoob

回答

2

這裏是最終的程序代碼。我添加了兩個函數,一個用於調用實際遊戲,另一個用於檢查玩家是否想再次玩。另外,最後還有最後一句話

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

public class RPC 
{ 
    public static Scanner sc   = new Scanner(System.in); 
    public static int  score  = 0; 
    public static int  gameCount = 0; 

    public static void main(String[] args) 
    { 
     play(); 
     while (playAgain()) 
     { 
      play(); 
     } 
    } 

    public static void play() 
    { 
     String computer = computerChoice(); 
     String user = userChoice(); 
     determineWinner(computer, user); 
    } 

    public static String computerChoice() 
    { 
     Random rand = new Random(); 
     int cinput = rand.nextInt(3) + 1; 
     String computer = "thing"; 
     if (cinput == 1) 
      computer = "Rock"; 
     if (cinput == 2) 
      computer = "Paper"; 
     if (cinput == 3) 
      computer = "Scissors"; 
     return computer; 
    } 

    public static boolean playAgain() 
    { 
     System.out.println("Play again?(y/n)"); 
     String input = sc.nextLine(); 
     if (input.toLowerCase().equals("y")) 
     { 
      return true; 
     } else if (input.toLowerCase().equals("n")) 
     { 
      return false; 
     } else 
     { 
      System.out.println("Invalid Input"); 
      return playAgain(); 
     } 

    } 

    public static String userChoice() 
    { 

     String user = "default"; 
     do 
     { 
      System.out.println("choose your weapon(Paper,Scissors or Rock)"); 
      user = sc.nextLine(); 
     } while (!isValidChoice(user)); 
     return user; 
    } 

    public static boolean isValidChoice(String choice) 
    { 
     boolean status; 
     if (choice.equals("Rock")) 
      status = true; 
     else if (choice.equals("Paper")) 
      status = true; 
     else if (choice.equals("Scissors")) 
      status = true; 
     else 
     { 
      status = false; 
      System.out.println("Error! Make sure you are capitalizing your choices"); 
     } 

     return status; 
    } 

    public static void determineWinner(String computer, String user) 
    { 
     gameCount++; 
     System.out.println(" Computer Choice: " + computer); 
     System.out.println("Your Choice : " + user); 
     if (computer.equals("Rock") && user.equals("Scissors")) 
     { 
      score--; 
      System.out.println(" Computer wins! Better luck next time!"); 
     } 
     if (computer.equals("Scissors") && user.equals("Paper")) 
     { 
      score--; 
      System.out.println(" Computer wins! Better luck next time!"); 
     } 
     if (computer.equals("Paper") && user.equals("Rock")) 
     { 
      score--; 
      System.out.println(" Computer wins! Better luck next time!"); 
     } 
     if (computer.equals("Rock") && user.equals("Paper")) 
     { 
      score++; 
      System.out.println(" You win!!"); 
     } 
     if (computer.equals("Scissors") && user.equals("Rock")) 
     { 
      score++; 
      System.out.println(" You win!!"); 
     } 
     if (computer.equals("Paper") && user.equals("Scissors")) 
     { 
      score++; 
      System.out.println(" You win!!"); 
     } else if (computer.equals(user)) 
     { 
      System.out.println(" Tie! the game must be played again."); 
     } 
     System.out.println("We have matched wits" + gameCount + "times, and your score is" + score); 
     return; 
    } 

} 
+0

你的第一堂課對於我的編程課我們還沒有進入課堂上使用課程的部分,所以我不太明白他們做了什麼/他們是如何使用的。@ Fungucide – Progamminnoob

+1

這完全像你所擁有的在原始代碼中,它調用了曾經是你主要方法的play()方法。然後它詢問用戶是否想用playAgain()方法再次播放,然後重複直到用戶說不。另外,我對while循環進行格式化的方式也不同。 (條件)時使用do {...}。這種方式是while(條件){...},但它本質上是相同的東西 – WIR3D

+0

我的意思是這個類---- public class RPC { public static Sc​​anner sc = new Scanner(System.in); public static int score = 0; public static int gameCount = 0; – Progamminnoob