0

我正在尝试创建角色 2 骰子的程序。然后用户说“是”以再次角色骰子或说“否”以停止掷骰子。

import java.util.*;
    public class Dice
    {
        public static void main(String[] args)
        {

        Random dice1 = new Random();
        Scanner in = new Scanner(System.in);
        //varibles
        int die1;
        int die2;
        byte playagain=1;
        byte Yes = 1;
        byte No = 0;
        int total;
        int stop = 0;
        //Want find I way to change words into #s
        String start = Yes;
        while(stop<5 && start<Yes){
        stop+=1;
            die1=dice1.nextInt(6)+1;
            die2=dice1.nextInt(6)+1;
                total=die1 + die2;
                System. out. println("You rolled a "  + total+ ".");
        System. out. println("Do you want to play again?");
        System. out. println("Type Yes to keep playing you and No to stop.");
        /*I want people to be able to input Yes and that equal a # so I can use it in the While loop. Same with No.*/
        start=in.next();

        System. out. println("start is at " + start);

        }
        }
    }

我浏览了整个互联网,找不到任何帮助,所以这就是我要问的原因。

4

1 回答 1

0

如果您的意思是要从扫描仪中读取 int,请尝试使用Scanner.nextInt http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()

您可以使用hasNextInt来确定使用nextInt是否有效:http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextInt()

如果您已经有了字符串并想将其转换为 int,请尝试Integer.parseInt http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

一般来说,如果您想知道是否存在执行基本操作的函数,您应该检查 java.lang、java.util、java.io 等中的 API(取决于您认为它在哪里)

于 2013-04-15T04:36:23.793 回答