我已经创建了一个基本的骰子游戏。我想有一个功能,如果你选择不再玩,你会得到一个你以前所有卷轴的列表。我已经为骰子制作了一个生成2个随机数的方法。我无法访问它们,因为它们在方法中。
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class DicegameV2 {
// method for generating random number for dice
static void dice_roll() {
Random rand = new Random();
int dice1 = rand.nextInt(6) +1;
int dice2 = rand.nextInt(6) +1;
System.out.println("you rolled " + dice1 + " and " + dice2);
}
public static void main(String[] args) {
ArrayList<Integer> last_dice_rolls = new ArrayList<Integer>();
boolean rollAgain = true;
// gets username
Scanner scanner = new Scanner(System.in);
System.out.println("What's your username?");
String username = scanner.nextLine();
System.out.println("Hello, " + username);
while (rollAgain) {
System.out.println("Roll the dice?");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("yes"))
dice_roll();
else if (choice.equalsIgnoreCase("no")) {
System.out.println("Thanks for playing!nyour last rolls was " + last_dice_rolls);
rollAgain = false;
} else {
System.out.println("Please input yes or no");
}
}
}
}
创建一个ArrayList来保存卷的想法是正确的,但正如您所发现的,您的卷位于dice_roll方法中的dice1和dice2变量中,因此您无法将它们放入ArrayList。
正如akuzminykh建议的那样,您可以从dice_roll((返回roll,但这有点棘手,因为它是两个数字,并且一个方法返回一个值,所以您必须将它们封装在某种东西中才能返回它们(这是正确的做法,但对于第一个程序来说有点复杂(。
Java是面向对象的,这意味着方法属于对象,对象也可以保存数据。这将是一个很好的方法来保存你试图积累的非瞬态数据。
然而,你的方法是";静态";,这意味着它们不是任何物体的一部分。main((必须是静态的,因为Java中的程序就是这样启动的。
所以你有几个步骤来实现这一点:
- 让main((方法只创建一个新的DicegameV2对象并在其上调用play((方法
- 将main((方法的当前内容移动到这个新的play((方法中,该方法不是静态的
- 将ArrayList last_dice_rolls移出方法,使其不是局部变量而是对象成员
- 使dice_roll((方法非静态,这样它就可以访问last_dice_rolls成员,如
last_dice_rolls.add(dice1);
- 循环last_dice_rolls并在末尾输出值
你的新main((方法看起来是这样的(我不会写你的整个程序(
public static void main(String[] args) {
DicegameV2 game = new DicegameV2();
game.play();
}
您可以创建一个类Rolls
public class Rolls{
private int idRoll;
private int dice1;
private int dice2;
public void setIdRoll(int id){
idRoll = id;
}
public void setDice1(int d1){
dice1 = d1;
}
public void setDice2(int d2){
dice2 = d2;
}
public String toString(){
return "Roll number: " + idRoll + " dice1: " + dice1 + " dice2: " + dice2 + "n";
}
}
然后在你的主类上使用
import java.util.*;
public class DicegameV2{
// method for generating random number for dice
public static void dice_roll(Rolls rolls) {
Random rand = new Random();
int dice1 = rand.nextInt(6) +1;
int dice2 = rand.nextInt(6) +1;
rolls.setDice1(dice1);
rolls.setDice2(dice2);
System.out.println("you rolled " + dice1 + " and " + dice2);
}
public static void main(String[] args) {
//I decided use LinkedList
List<Rolls> last_dice_rolls = new LinkedList<Rolls>();
int idRoll = 0;
boolean rollAgain = true;
// gets username
Scanner scanner = new Scanner(System.in);
System.out.println("What's your username?");
String username = scanner.nextLine();
System.out.println("Hello, " + username);
while (rollAgain) {
System.out.println("Roll the dice?");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("yes")){
Rolls rolls = new Rolls();
rolls.setIdRoll(idRoll++);
dice_roll(rolls);
last_dice_rolls.add(rolls);
}
else if (choice.equalsIgnoreCase("no")) {
System.out.println("Thanks for playing!nyour last rolls was " + "n");
for(Rolls r: last_dice_rolls){
System.out.println(r);
}
rollAgain = false;
} else {
System.out.println("Please input yes or no");
}
}
}
}
使arrayList last_dice_rolls成为Dicegame v2类的一个属性,这样您就可以在类中的任何位置访问它。如果您在main方法中声明它,它将只能在main方法内访问。试试这个:
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class DicegameV2 {
private static List<Integer> last_dice_rolls;
static void dice_roll() {
if(last_dice_rolls == null)
last_dice_rolls = new ArrayList<Integer>();
Random rand = new Random();
int dice1 = rand.nextInt(6) +1;
int dice2 = rand.nextInt(6) +1;
last_dice_rolls.add(dice1);
last_dice_rolls.add(dice2);
System.out.println("you rolled " + dice1 + " and " + dice2);
}
public static void main(String[] args) {
boolean rollAgain = true;
// gets username
Scanner scanner = new Scanner(System.in);
System.out.println("What's your username?");
String username = scanner.nextLine();
System.out.println("Hello, " + username);
while (rollAgain) {
System.out.println("Roll the dice?");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("yes"))
dice_roll();
else if (choice.equalsIgnoreCase("no")) {
System.out.println("Thanks for playing!nyour last rolls was ");
for(int roll : last_dice_rolls)
System.out.println(roll);
rollAgain = false;
} else {
System.out.println("Please input yes or no");
}
}
}
}
java共享编辑跟随标志