需要制作高低游戏,但无法弄清楚如何修复错误

时间:2018-06-07 14:08:12

标签: java compiler-errors game-physics

import java.util.Scanner;
import java.util.Random;
public class ASDFhighlow
{
public static void main(String[] args) 

{

  int money = 100;
        int roll1;
        int roll2;
        int userBet;
        int lostwin;
        char c;
        int determineWinnings;
        Scanner in = new Scanner(System.in);
        Random r = new Random();
        do

        {

            if (money < 1)
                break;
            System.out.println(" You have " + money + "  dollars. ");
            userBet = getBet(in, money);
            if (userBet == 0)
                break;

            c = getHighLow(in);
            roll1 = 1+ r.nextInt(5);
            System.out.println(" Die 1 rolls : " + roll1);
            roll2 = 1+r.nextInt(5);
            System.out.println(" Die 2 rolls : " + roll2);
            System.out.println("Total of two dice is: " + (roll1 + roll2));
            lostwin = determineWinnings(c, userBet, roll1 + roll2);

            if (lostwin < 0)
                System.out.println("You lost!");
            else
                System.out.println("You won " + lostwin + " dollars! ");
            money = money + lostwin;
        } while (money > 0);

        System.out.println("Goodbye!");
    }

    private static int getBet(Scanner inScanner, int moneyPot) {
        int bet;

        do {
            System.out.println("Enter an amount to bet (0 to quit): ");
            bet = inScanner.nextInt();
        } while (bet > moneyPot && bet != 0);

        return bet;
    }

    private static char getHighLow(Scanner inScanner) {
        System.out.println("High, low or sevens (H/L/S): ");
        String str = inScanner.next();

        return str.charAt(0);
    }
}

这是错误信息:
找到1个错误: 文件:/ Users / adriendaleyfrenette / Desktop / EXAM FOLDER / ASDFhighlow.java [line:28] 错误:对于ASDFhighlow类型,方法determineWinnings(char,int,int)未定义

我无法弄清楚如何使它真正起作用我仍然是新的,所以任何帮助都是适用的

2 个答案:

答案 0 :(得分:1)

以下是答案
您已进行以下方法调用

lostwin = determineWinnings(c, userBet, roll1 + roll2);

但是,您尚未定义方法determineWinnings。名为determineWinnings的术语的唯一声明是主方法顶部的一个int,它永远不会被初始化。您需要创建一个如下所示的新方法:

private int determineWinnings(char c, int userBet, int rollSum)

下次如何解决这个问题:
您收到的错误消息告诉您程序失败的原因。在这种情况下,它表示您尝试在第28行调用的方法未定义。学会仔细查看收到的错误消息,并了解他们想说的内容。如果您无法从那里弄清楚或者令人困惑,请在错误中搜索文本。它可能与您的错误的措辞不完全相同,但应该让您指出要调试程序的事物类型的正确方向。

答案 1 :(得分:0)

您已将determineWinnings声明为int变量,并且您已调用具有相同名称的方法,但未创建该方法的任何定义。您需要为determineWinnings()方法创建定义,我可以看到它应该是返回类型int

你需要做这样的事情:

private static int determineWinnings(char someChar, int betAmount, int rollSum){
    //write your logic here

    return someIntegerValue;
}