2015-02-12 2 views
0

Что-то не так с этим кодом, и я не уверен, что, я пытаюсь сделать случайное число между 0,8 и 1,2, тогда это можно использовать для увеличения длины ветвей, созданных с помощью этого программа.Ошибка со случайным числом

import java.awt.*; 
import javax.swing.*; 
import java.util.Random; 


public class OneFractalTree extends JApplet { 

final double ONE_DEGREE = Math.PI/180; 
final double BRANCHANGLE = ONE_DEGREE*30; 
final double SHRINKFACTOR = .65;   
final int START_LENGTH = 75;    

//Draws branches 
public void drawbranch(Graphics g, 
         double startx, //coordinates of branch start 
         double starty, 
         double length, //length of branch 
         double angle ) //angle of branch 
{ 
    double endx = startx + Math.sin(angle) * length; //Calculates the end coordinates of the branch 
    double endy = starty + Math.cos(angle) * length; 

    /** 
    * The loop draws the branches and continues until the length becomes too small 
    * i.e. when length is less than 1, length becomes smaller by shrinkfacter every iteration) 
    */ 
    if(1 < length) {  

     Random rand = new Random(); 
     int randomNum = rand.nextInt((12 - 8) + 1) + 1; 
     double randomNumdouble = randomNum/10; 



     g.setColor(length < 5 ? Color.green : Color.black); //Sets color according to length 
     g.drawLine((int)startx, (int)starty, (int)endx, (int)endy); //Coordinates to draw line 
     drawbranch(g, endx, endy, (length * SHRINKFACTOR) * randomNumdouble, angle - BRANCHANGLE); //1st branch creation 
     drawbranch(g, endx, endy, (length * SHRINKFACTOR) * randomNumdouble, angle + BRANCHANGLE); //2nd branch creation 
     drawbranch(g, endx, endy, (length * SHRINKFACTOR) * randomNumdouble, angle); //3rd branch creation 
    } 
} 


public void paint(Graphics g) { 
    Rectangle r = getBounds(); //Finds height of applet viewer 
    drawbranch(g, r.width/2, r.height, START_LENGTH, Math.PI);  //Calls method to draw branch 
} 

} 
+0

Почему бы вам не рассказать нам, что не так? – tnw

+0

'double randomNumdouble = randomNum/10;' try with '/ 10.0;'. Также вместо 'Math.PI/180' вы можете использовать' Math.toRadians (1) ', чтобы получить значение radian, представляющее одну степень, которую может быть легче читать. – Pshemo

+1

Используйте 'randomNum/10f' вместо' randomNum/10' – Titus

ответ

0

Правильная формула

Random.nextDouble() * (max - min) + min; 

Но исправить код:

int randomNum = rand.nextInt((12 - 8) + 1) + 1; 
double randomNumdouble = randomNum/10.0; // 10 will be interpreted as an `int`, whereas 10.0 will be a `double` 
+0

Блестяще, спасибо! – JL9

0

randomNum/10 Является ли число операции, попробуйте использовать /10.0 или /10f использовать операции с плавающей точкой.

Проблема с /10, что она будет пол значение до ближайшего целого числа

0

Вы объявили int randomNum который является целым числом. Таким образом, randomNum/10 производит целочисленное значение, так как randomNum и 10 являются целыми. Наконец, вы присваиваете результат типа int переменной типа double без кастования любого типа. Сделать его double randomNumdouble = randomNum/10.0

Смежные вопросы