如何放置随机数而不重叠?Java小程序。简单但新的编码
用户输入其姓名和幸运号码。颜色背景,数字和每个句子用不同的颜色。对文本和数字使用不同大小的字体样式。所有随机数应随机放置在屏幕上 我想把数字随机地放在屏幕上不同的地方,但不要让它们放在上面(重叠),比如使用你的直觉,优秀的猜测等等。我尝试在数学中添加数字。随机,但它只限制了该数字。该区域中的数字将始终是该颜色。i、 e.对于我代码中的a,在该空间中始终为绿色。我对编码还不熟悉,我不知道我会怎么做。任何建议都将受到极大的赞赏 我评论了一些部分,因为我正在检查a和b如何放置随机数而不重叠?Java小程序。简单但新的编码,java,random,applet,Java,Random,Applet,用户输入其姓名和幸运号码。颜色背景,数字和每个句子用不同的颜色。对文本和数字使用不同大小的字体样式。所有随机数应随机放置在屏幕上 我想把数字随机地放在屏幕上不同的地方,但不要让它们放在上面(重叠),比如使用你的直觉,优秀的猜测等等。我尝试在数学中添加数字。随机,但它只限制了该数字。该区域中的数字将始终是该颜色。i、 e.对于我代码中的a,在该空间中始终为绿色。我对编码还不熟悉,我不知道我会怎么做。任何建议都将受到极大的赞赏 我评论了一些部分,因为我正在检查a和b import java.awt.
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
public class luckynumbers extends Applet{
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int name;
double lucky;
int a = (int)(0+Math.random() * 99);
int b = (int)(Math.random() * 99);
// int c = (int)(Math.random() * 99);
// int d = (int)(Math.random() * 99);
// int e = (int)(Math.random() * 99);
// int f = (int)(Math.random() * 99);
int w1 = (int)(0+Math.random() * 100);
int h1 = (int)(0+Math.random() * 200+200);
int w2 = (int)(Math.random() * 300+100);
int h2 = (int)(Math.random() * 400+200);
// int w3 = (int)(Math.random() * 300);
// int h3 = (int)(Math.random() * 300);
// int w4 = (int)(Math.random() * 300);
// int h4 = (int)(Math.random() * 300);
// int w5 = (int)(Math.random() * 300);
// int h5 = (int)(Math.random() * 300);
// int w6 = (int)(Math.random() * 300);
// int h6 = (int)(Math.random() * 300);
public void init(){
String name;
String input1 = JOptionPane.showInputDialog("Please, enter your name");
name = input1;
String input2 = JOptionPane.showInputDialog("Please, enter your lucky number?");
lucky = Integer.parseInt(input2);
}
public void paint(Graphics g){
Color blue2 = new Color(156, 207, 222);
Color pink = new Color(212, 84, 129);
Color bg = new Color(200, 150, 204); //RGB
setBackground(bg);
Font z = new Font("ComicSans", Font.BOLD, 45);
g.setFont(z);
g.setColor(Color.blue);
g.drawString("LUCKY NUMBERS GAME", 50,55); //Text starts at that point
g.drawString(" by " + name,750,55);
z = new Font("ComicSans", Font.BOLD, 16);
g.setColor(Color.red);
g.drawString("Lucky Number: " + (Math.round(lucky)), 750,350);
g.setColor(Color.green);
z = new Font("Impact", Font.ITALIC, 50);
g.setFont(z);
g.drawString(a +" ", w1, h1);
g.setColor(Color.yellow);
g.drawString(b +" ", w2, h2);
//
// g.setColor(Color.black);
// g.drawString(c +" ", w3, h3);
//
// g.setColor(Color.red);
// g.drawString(d +" ", w4, h4);
//
// g.setColor(Color.blue2);
// g.drawString(e +" ", w5, h5);
//
// g.setColor(Color.pink);
// g.drawString(f +" ", w6, h6);
g.setColor(Color.red);
z = new Font("Verdana", Font.ITALIC, 18);
g.setFont(z);
g.drawString("Use your intuition! ", 400, 100);
g.setColor(Color.orange);
z = new Font("Curlz MT", Font.ITALIC, 20);
g.setFont(z);
g.drawString("Try to guess the number! ", 50, 150);
g.setColor(pink);
z = new Font("Garamond", Font.BOLD, 32);
g.setFont(z);
g.drawString("Excellent guess! ", 280, 260);
g.setColor(blue2);
z = new Font("Lucida Sans", Font.ITALIC, 27);
g.setFont(z);
g.drawString("Try a larger number! ", 10, 430);
g.setColor(Color.yellow);
z = new Font("Arial", Font.BOLD, 20);
g.setFont(z);
g.drawString("You can always play again! ", 450, 600);
z= new Font("Arial", Font.ITALIC, 25);
g.setFont(z);
g.setColor(Color.white);
g.drawString("Today's Date: "+day+"/"+month+"/"+year,900,500);
}
}
你需要知道句子的文本在哪里,我在代码中放置它们的位置。但是我该怎么处理它们呢@MadProgrammer当您计算随机数的位置时,您需要检查该位置是否与文本位置冲突。在本例中,只需获取一个新的随机位置否,您需要在句子出现的位置创建某种列表,以便在生成“随机”位置时,您可以执行一些“碰撞检测”并确定是否需要生成新位置。1)为什么要编写小程序?如果是老师指定的,请参考。2) 为什么要使用AWT?请参阅,了解放弃AWT使用组件而支持Swing的许多好理由。