我正在尝试创建角色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);
}
}
}
我看了整个互联网,找不到任何帮助,所以这就是为什么我问。
如果你的意思是你想从扫描器中读取一个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中的api。朗,java。跑龙套,java。等等(取决于你认为它会在哪里)