【发布时间】:2016-04-05 14:27:20
【问题描述】:
所以基本上我的问题出在这个程序中,我会在放入后更好地解释
package learning;
//This class has an important method that i have been working on called Dice6 don't delete
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class GameBoard {
public static void main(String[] args) {
System.out.println("Welcome To The Gameboard");
System.out.println("What is your name player 1?");
Scanner Scan = new Scanner(System.in);
String ScanResult1 = Scan.nextLine();
System.out.println("Well... " + ScanResult1 + " This is the Board");
System.out.println("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~|");// Don't Question its
// placement its weird,
// but it works
System.out.println(" |");
System.out.println(" |~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
System.out.println("Lets start with a Dice Roll");
System.out.println(ScanResult1 + " Got a Roll of " + Dice6(10));
Scan.close();
}
static int Dice6(int y) {
ArrayList<Integer> Dice = new ArrayList<Integer>();
Dice.add(1);
Dice.add(2);
Dice.add(3);
Dice.add(4);
Dice.add(5);
Dice.add(6);
int DiceAmount = Dice.size();
while (DiceAmount != 6) {
DiceAmount= DiceAmount - 1;
Dice.add(Dice.size() + 1);
}
Collections.shuffle(Dice);
Collections.shuffle(Dice);
Integer DiceResult = Dice.get(3);
return DiceResult;
}
}
好的,我遇到的问题来自 System.out.println(ScanResult1 + "得到一卷" + Dice6(10)); 到程序的底部,基本上我正在尝试使用参数 10 在 ArrayList 中添加 Array 值......假设发生的情况是 ArrayList 中假设有 10 个值,具体取决于我在参数中输入的内容....我已经尝试了很多事情并做了很多修复,我曾经做过 for(blank x : Dice) 来显示所有值,而 10 并没有出现只有原来的 6....如果任何人都知道如何解决这个问题,请告诉我
【问题讨论】:
-
我没有关注你。是什么让您认为您的代码应该将 10 个元素放入您的
List? -
此外,如果您遵循标准命名约定,您的代码将更容易被其他人阅读。特别是,方法和局部变量的名称应该以小写字母开头,采用驼峰式。
-
“根据我在参数中输入的内容,假设 ArrayList 中有 10 个值”是什么意思?我想你在谈论方法
Dice6(),但是如果应该有正好10个值,那么参数与它有什么关系?或者你的意思是参数应该指定有多少个值? -
另外,您是否认为您的
Dice6()方法是生成随机数的真正可怕的实现?我建议您查看Random的课程,以获得更直接的方式。 -
@John Bollinger 你是对的。编码标准非常糟糕..我最初认为 Dice6(10) 是一个构造函数。
标签: java if-statement arraylist methods parameters