节目没有显示骰子? [英] Program not showing the dice?

查看:50
本文介绍了节目没有显示骰子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的程序显示了两个骰子上的数字;它没有打开显示骰子的小程序?



Right now my program shows the number on both of the dice, however; it doesn't open up an applet showing the dice?

public class TestDie {
    public static void main(String[] arguments) {
        Die firstDie = new Die();
        Die secondDie = new Die();
        firstDie.rollValue(6);
        secondDie.rollValue(6);
        System.out.println("The first die rolled a " + firstDie.value);
        System.out.println("The second die rolled a " + secondDie.value);
    }
}







import java.awt.*;

 public class Die {
     public int value;

     public Die() {
         value = 0;
     }

     public void rollValue(int maxValue) {
         double tempValue = Math.random() * maxValue;
         value = (int) Math.floor( tempValue ) + 1;
     }

     public void drawDie(Graphics screen, int x, int y) {
         screen.setColor(Color.red);
         screen.fillRoundRect(x, y, 100, 100, 20, 20);
         screen.setColor(Color.black);
         screen.drawRoundRect(x, y, 100, 100, 20, 20);
         screen.setColor(Color.white);
         if (value > 1) {
             screen.fillOval(x+5, y+5, 20, 20);
             screen.fillOval(x+75, y+75, 20, 20);
        }
         if (value > 3) {
             screen.fillOval(x+75, y+5, 20, 20);
             screen.fillOval(x+5, y+75, 20, 20);
         }
         if (value == 6) {
             screen.fillOval(x+5, y+40, 20, 20);
             screen.fillOval(x+75, y+40, 20, 20);
         }
         if (value % 2 == 1) {
             screen.fillOval(x+40, y+40, 20, 20);
         }
     }
}

推荐答案

你需要为你的骰子添加一个吸气剂对象死:



You need to add a getter for the dice to your object Die:

public int getDie(){
  return value;
}





然后你可以打电话给那个吸气剂:



Then you can call that getter:

System.out.println("The first die rolled a " + firstDie.getDie());
System.out.println("The second die rolled a " + secondDie.getDie());





编辑:



请打破静态主函数:





And please break out of that static main function:

public class TestDie {

     public TestDie(){
          // rest of code from main here
     }
     
     public static void main(String[] arguments) {
          TestDie oDie = new TestDie();
     }
}


这篇关于节目没有显示骰子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆