2014-02-24 67 views
0

我刚开始在一堂课中学习Java。这个任务是制作一个Rock,Paper and Scissors游戏,这里是我的源代码。我已经把它翻过来了,但还没有收到分数。如何在Java中使用if有条件地循环语句?

package rockpaperscissors; 

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

public class Main { 

public static void main(String[] args) { 
    // Create scanner input 
    Scanner input = new Scanner (System.in); 

    // Introduce user to game 
    System.out.println("Welcome to the Rock, Paper and Scissors game"); 

    // Ask the user for their move 
    System.out.println("\nPlease select a move\nEnter 0 for Scissors, 1 " 
      + "for Rock and 2 for Paper"); 
    int userMove = input.nextInt(); 

    // Ensure that userMove is between 0 and 2 
    if (userMove > 2 || userMove < 0) { 
     System.out.println("Invalid entry the result of this game will not " 
       + "be accurate! Please retry using either 0 for Scissors, 1" 
       + " for Rock and 2 for Paper\n"); 
    } 

    // Generate computerMove using java.util.Random 
     // Create instance first 
    Random MyRandom = new Random(); 

    //Now generate number 
    int computerMove = MyRandom.nextInt(3); 

    System.out.print("The computer played " + computerMove + "\nRemember" 
      + " 0 stands for Scissors, 1 is for Rock and 2 is for paper\n"); 

    // Determine draw result 
    if (userMove == computerMove) { 
     System.out.println("\nYou played " + userMove + ", The computer " 
       + "played " + computerMove + " The game is a draw!"); 
    } 
    // Determine computer win 
    else if(userMove == 0 && computerMove == 1) { 
     System.out.println("\nYou played " + userMove + ", The computer " 
       + "played " + computerMove + " The Computer Wins!"); 


    } 

    else if(userMove == 2 && computerMove == 0) { 
     System.out.println("\nYou played " + userMove + ", The computer " 
       + "played " + computerMove + " The Computer Wins!"); 

    } 

    else if(userMove == 1 && computerMove == 2) { 
     System.out.println("\nYou played " + userMove + ", The computer " 
       + "played " + computerMove + " The Computer Wins!"); 

    } 

    /* Determine User win, as no other moves can result in computer win all 
    other combinations will result in user win 
    */ 

      else { 
     System.out.print("\nYou played " + userMove + ", The computer " 
       + "played " + computerMove + " You Win!"); 
    } 



} 

}

我有上获取节目的数量来重新问,如果数量大于2或小于0的麻烦,它会说“无效的条目中的结果将是无效的。 ..“但它仍然会用程序的其余部分执行一个无效的数字。

如何让程序停止并提示用户输入有效的数字,然后执行程序?此外,这是我所做的第三个程序,所以任何其他建议,将不胜感激。

感谢

回答

1

你想要的是,如果你的条件满足时无法完成所有的其他代码。

现在,当满足条件时,您会发出错误消息 - 但不会停止执行其余功能。

你可以停下来。

所以 - 没有告诉你确切的答案......你如何退出函数? 这样做只是在错误信息显示后,你会没事的。

但是,如果您想要继续获取用户输入,直到您符合条件...需要在提取和检查的部分周围进行循环。

你知道什么循环?

你知道一个让你循环,直到你遇到一些条件?

你已经得到了你的条件......现在你只需要调整一个循环让你做你想要的代码 - 反复 - 直到满足条件。

伪代码

(即这是不是真正的Java - 你必须翻译):

some_loop do 
    fetch user input 
    give error message if its not ok 
until (user input is ok) 
+0

我不认为你阅读的整个问题。 (提示:新程序员并不总是使用正确的术语。) – ajb

+0

我正在处理它... :) –

+0

对于任何混淆抱歉,我真的不明白这些条款。 我们所讨论的唯一循环是,虽然老师不在意我不知道该怎么做,但我只想知道我的个人知识 – user3344771