gpt4 book ai didi

java - 如何访问我的方法 (Java)?

转载 作者:行者123 更新时间:2023-11-29 04:25:30 26 4
gpt4 key购买 nike

我已尽最大努力查看调试器和测试,但我无法解决这个问题。

在此方法中,它会跳过 for 循环内部。但是在roll()方法中,arraylist获取值?

public int lol(int a) {
num = 0;
for (int i = 0; i < objectDie.getDices().size(); i++) {
if (objectDie.getDices().get(i) == a) {
num += a;
}
}
return num;
}

我将发布我所有的类以创建更好的代码图像,麻烦制造者在我链接的最后一个类的底部。

主要

package dicegame;

public class DiceGame {
public static void main(String[] args) {
Game test = new Game();
test.numberDices();
Player spil = new Human();
spil.takeTurn(test);
}
}

游戏

package dicegame;

import java.util.Scanner;

public class Game {

Die DieObjekt = new Die();
Scanner sc = new Scanner(System.in);

public void numberDices() {
System.out.println("How many dices would you like to play with?");
int numberOfDices = sc.nextInt();
DieObjekt.setNumber(numberOfDices);
}

public void play() {
DieObjekt.roll();
DieObjekt.printRoll();
}
}

package dicegame;

import java.util.ArrayList;
import java.util.Random;

public class Die {

ArrayList<Integer> dices = new ArrayList<>();
Random ran = new Random();
int number;

public ArrayList<Integer> getDices() {
return dices;
}

public void setNumber(int number) {
this.number = number;
}

public void roll() {
for (int i = 0; i < number; i++) {
dices.add(ran.nextInt(6) + 1);
}
}

public void printRoll() {
System.out.println("You got ");
for (int i = 0; i < dices.size(); i++) {
System.out.print(dices.get(i) + ", ");
}
}
}

人类 - 此类中的最后一个方法是麻烦制造者

package dicegame;

import java.util.Scanner;

public class Human implements Player {

int one, two, three, four, five, six, num;
Scanner sc = new Scanner(System.in);
Die objectDie = new Die();
Game object = new Game();

@Override
public void takeTurn(Game object) {
object.play();
System.out.println("If you want to stop, press enter, if not press"
+"the number on the die that you would like to save");
int valg = sc.nextInt();
switch (valg) {
case 1:
one = lol(1);
System.out.println("You got " + one + " points in the"
+"ones");
break;
case 2:
two = lol(2);
System.out.println("You got " + two + " points in the"
+"twos");
break;
case 3:
three = lol(3);
System.out.println("You got " + three + " points in the"
+"threes");
break;
case 4:
four = lol(4);
System.out.println("You got " + four + " points in the"
+"fours");
break;
case 5:
five = lol(5);
System.out.println("You got " + five + " points in the"
+"fives");
break;
case 6:
six = lol(6);
System.out.println("You got" + six + " points in the"
+"sixes");
break;
default:
System.out.println("You have stopped, and that is OK");
break;
}
}

public int lol(int a) {
num = 0;
for (int i = 0; i < objectDie.getDices().size(); i++) {
if (objectDie.getDices().get(i) == a) {
num += a;
}
}
return num;
}
}

最佳答案

在类游戏中,您完成了创建骰子列表的过程(设置总数并通过滚动添加值)=>数组列表有值

但是你忘了用人类类来做

====================

编辑

这里有一些修复游戏的建议

    /**
* Don't try to create dice and game in human class, you can take it from previous game which you were created in the static main
*/
//Die objectDie = new Die();
//Game object = new Game();

就这样吧

Game object;

当你从 takeTurn 方法获得游戏对象时,然后存储它

public void takeTurn(Game object) {
this.object = object;
object.play();

.....
}

然后在你的 lol 方法中,尝试遍历你刚刚从 takeTurn 方法获得的所有游戏骰子

public int lol(int a) {
num = 0;
for (int i = 0; i < object.getDices().size(); i++) {
if (object.getDices().get(i) == a) {
num += a;
}
}
return num;
}

最后,记得为 Game 类创建 getDices 方法 :)

public class Game {

Die DieObjekt = new Die();

public List<Integer> getDices() {
return DieObjekt.getDices();
}

关于java - 如何访问我的方法 (Java)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46588404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com