import java.util.Random;
import java.io.*;
import java.util.*;
/**
Courtney Fox
Professor Yao
Midterm Part 1
10/10/17
Purpose:
The purpose of this program is to develop a Nim game that consists of a
pile of stones ranging from 10-16. From that pile, both the player and
computer have to pick up to 3 stones and whoever gets the last stone
loses.
Logic:
**/
public class FoxMidQ1
{
public static void main(String[] args)
{
//Variables
int user = 0;
//int computer;
//int loser;
int gamenum = 0;
//Scanner
Scanner input = new Scanner(System.in);
//Welcome Output
System.out.println("Welcome to Nim Game!");
//Get pile size: Randomly generate 10-16
int[] pile = {10, 11, 12, 13, 14, 15 , 16};
int stones = pile[(int)(Math.random() * pile.length)];
System.out.println("Game #"+ (gamenum + 1) +": There are "+ stones + "
stones in the pile.");
System.out.println("You can remove up to 3 stones from pile at a
time.");
//User takes stones
System.out.println("How many stones would you like to remove? ");
user = input.nextInt();
我得到了初開始,但我被困在其中部分用戶只假設取1,2,或者從一堆石頭3。我嘗試過這樣做,而如果其他和這些循環都沒有做我想做的事情,因爲用戶只能假設有一個回合,那麼它的計算機就會從堆中取出3塊石頭。我如何限制用戶僅輸入1-3
'如果(輸入> 3)'顯示一條消息,並要求再次輸入? –
因爲任何數字都是你的測試失敗大於一或小於3 –
的System.out.println(「你有多少石頭要刪除?」); user = input.nextInt(); 如果(用戶< 1 || user > 3) { 的System.out.println( 「請僅需要最多每圈3米的石頭。」); System.out.println(「你想移除多少塊石頭?」); user = input.nextInt(); } 別的 { INT stone1 =石頭 - 用戶; System.out.println(「There +」+ stone1 +「left left。」); } 我如何獲得它不斷向用戶詢問@LeonardoAlvesMachado – Courtney