「矩形个数java」矩形个数公式
今天给各位分享矩形个数java的知识,其中也会对矩形个数公式进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、用Java编写对100个随机生成长宽的矩形依照面积大小进行排序。
- 2、用Java实现一个三个矩形
- 3、java中如何给矩形内放数字
- 4、java中关于矩形类
- 5、用java写一个函数,计算m*n大小的棋盘上面的矩形数量。(这一题更多是要看算法和思路)
- 6、用java定义一个4*5矩形数组
用Java编写对100个随机生成长宽的矩形依照面积大小进行排序。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class RandomRectangleSort {
public static void main(String[] args) {
// 每次的随机数不同
Random rand = new Random();
// 排序用List
ListInteger acrlist = new ArrayListInteger();
// 随机生成长和宽,计算面积
for (int i = 0; i 100; i++) {
int length = rand.nextInt(100) + 1;
int width = rand.nextInt(80) + 1;
int acreage = length * width;
acrlist.add(acreage);
}
// 按照面积排序
Collections.sort(acrlist);
// 打印看结果
for (int j : acrlist) {
System.out.println(j);
}
}
}
望采纳~
用Java实现一个三个矩形
三个矩形,几行几列呀?
class rect
{
public static void main(String[] args)
{
for(int k=0;k3;k++)
{
for(int i=0;i3;i++)//设置行数
{
for(int j=0;j5;j++)//设置列数
System.out.print("*");
System.out.println();
}
System.out.println();
System.out.println();
}
}
}
java中如何给矩形内放数字
你根据鼠标的选择画一个矩形(不用显示出来),矩形有一个方法叫public boolean intersects(Rectangle r);确定此 Rectangle是否与指定的 Rectangle r相交。如果两个矩形的交集为非空,则它们是相交的。如果用鼠标画出的矩形和其他矩形相交,就证明已经把他选进来了,让后就把这个矩形放到你想放的数组或是列表里面都可以。
java中关于矩形类
public class Rectangle2 {
// 属性:
private double m_height;
private double m_width;
private double m_x1;
private double m_y1;
private double m_x2;
private double m_y2;
private boolean m_isRectangle;
// 操作:
public Rectangle2() {
this(0.0, 0.0, 0.0, 0.0);
}
public Rectangle2(double aX1, double aY1, double aX2, double aY2) {
setCoordinates(aX1, aY1, aX2, aY2);
}
public void setCoordinates(double aX1, double aY1, double aX2, double aY2) {
m_x1 = aX1;
m_y1 = aY1;
m_x2 = aX2;
m_y2 = aY2;
setHeight();
setWidth();
isRectangle();
}
public boolean isRectangle() {
if ((m_height = 0 m_width = 0) (m_x1 != m_x2 || m_y1 != m_y2)) {
System.err.println("输入的坐标构成一个矩形");
m_isRectangle = true;
} else {
if ((m_x1 == m_x2 m_y1 != m_y2)
|| (m_x1 != m_x2 m_y1 == m_y2)) {
System.err.println("输入的坐标构成一条直线");
} else if (m_x1 == m_x2 m_y1 == m_y2) {
System.err.println("输入的坐标构成一点");
}
m_isRectangle = false;
}
return m_isRectangle;
}
public boolean isSquare() {
return isRectangle() m_height == m_width;
}
private void setHeight() {
double side1 = Math.abs(m_x1 - m_x2);
double side2 = Math.abs(m_y1 - m_y2);
m_height = side1 = side2 ? side1 : side2;
}
private void setWidth() {
double side1 = Math.abs(m_x1 - m_x2);
double side2 = Math.abs(m_y1 - m_y2);
m_width = side1 = side2 ? side1 : side2;
}
public double getHeight() {
return m_height;
}
public double getWidth() {
return m_width;
}
public double perimeter() {
return 2 * (m_height + m_width);
}
public double area() {
return m_height * m_width;
}
}
public class Rectangle2Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle2 rect1 = new Rectangle2();
System.err.println(rect1.isRectangle());
System.err.println(rect1.isSquare());
System.err.println(rect1.getHeight());
System.err.println(rect1.getWidth());
System.err.println(rect1.perimeter());
System.err.println(rect1.area());
Rectangle2 rect2 = new Rectangle2(22, 33, 66, 99);
System.err.println(rect2.isRectangle());
System.err.println(rect2.isSquare());
System.err.println(rect2.getHeight());
System.err.println(rect2.getWidth());
System.err.println(rect2.perimeter());
System.err.println(rect2.area());
}
}
用java写一个函数,计算m*n大小的棋盘上面的矩形数量。(这一题更多是要看算法和思路)
public class test {
public static void main(String args[]) {
System.out.println(getRectangleNum(1, 1));
System.out.println(getRectangleNum(2, 2));
System.out.println(getRectangleNum(2, 3));
}
public static int getRectangleNum(int m, int n) {
int x = 0, y = 0;
for (int i = 1; i = m; i++) {
for (int j = i; j = m; j++) {
x++;
}
}
for (int i = 1; i = n; i++) {
for (int j = i; j = n; j++) {
y++;
}
}
return x * y;
}
}
测试结果:
1
9
18
分别对应1*1,2*2,2*3的棋盘
算法原理:
每一个矩形取它的左上角(x1,y1)和右下角(x2,y2)的格子,棋盘左上角(0,0)右下角(m,n)
则需要满足条件:x1x2,y1y2
通过计算满足条件的x1,x2对的个数x和y1,y2对的个数y
他们乘积就是矩形的总个数。
用java定义一个4*5矩形数组
import java.util.Scanner;
public class ThreadTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入14个数字:");
int[] num = new int[14];
int count = 0 ;
for(int i = 0 ; i 14 ; i++){
num[i] = sc.nextInt();
count += num[i];
}
System.out.println("大于平均值的数:");
int pav = count / 14 ;
for(int i = 0 ; i 14 ; i++){
if(num[i] pav){
System.out.print(num[i]+" ");
}
}
int min=num[0],max=num[0] ;
for(int i = 1 ; i 14 ; i++){
if(min num[i]){
min = num[i];
}
if(max num[i]){
max = num[i];
}
}
System.out.println();
System.out.println("这些数字中最大的值为:"+min+" , 最小的是:"+max);
System.out.print("请输入一个字符串:");
String str = sc.next();
System.out.print("请输入字符:");
String a = sc.next();
str = str.replaceAll(a, "");
System.out.println("删除后的字符串:"+str);
}
}
关于矩形个数java和矩形个数公式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。