「沿两条对角线动java」两个对角线
今天给各位分享沿两条对角线动java的知识,其中也会对两个对角线进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
在java里;定义一个二维数组及相应方法求下列两条对角线的之和
/**
* @author eatonfang
* @version 1.0
*
*/
public class Test extends Father implements InterfaceTest {
public Test() {
}
/**
* @param args
*/
public static void main(String[] args) {
int[][] testArray = new int[][] {
{2,5,7,3,9},
{1,3,4,5,6},
{3,4,5,6,7},
{7,5,6,2,8},
{5,9,2,3,6}};
Test test = new Test();
test.sum(testArray);
test.transpose(testArray);
}
/**
* @param args
*/
public void sum (int[][] array) {
int arraySize = array.length;
int sumLine1 = 0;
int sumLine2 = 0;
for (int i = 0; i arraySize; i++) {
sumLine1 += array[i][i];
sumLine2 += array[arraySize - i -1][i];
}
System.out.println("对角线1: " + sumLine1);
System.out.println("对角线2: " + sumLine2);
System.out.println("2对角线和: " + (sumLine1 + sumLine2));
}
/**
* @param args
*/
public void transpose (int[][] array) {
System.out.println("转置前结果:");
for (int[] temp : array) {
for (int value : temp) {
System.out.print(value + " ");
}
System.out.println();
}
System.out.println("转置後结果:");
for (int i = 0; i array.length; i++) {
for (int j = 0; j array.length; j++) {
System.out.print(array[j][i] + " ");
}
System.out.println();
}
}
}
运行结果:
对角线1: 18
对角线2: 29
2对角线和: 47
转置前结果:
2 5 7 3 9
1 3 4 5 6
3 4 5 6 7
7 5 6 2 8
5 9 2 3 6
转置後结果:
2 1 3 7 5
5 3 4 5 9
7 4 5 6 2
3 5 6 2 3
9 6 7 8 6
你看是不是你要的效果 ,我这里的二维数组不是固定5×5的,其它的也行,但是是针对行数和列数相等的矩阵
J2ME。对角线移动
主类
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class hello extends MIDlet {
Display dis;
Move move = new Move();
public void startApp() {
dis = Display.getDisplay(this);
dis.setCurrent(move);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
控制动态
public class Move extends Canvas implements Runnable{
int width;
int height;
int X;
int Y;
int RecX;
int RecY;
public Move(){
width=this.getWidth();
height=this.getHeight();
RecX=100;
RecY=100;
X=width/2-RecX/2;
Y=height/2-RecY/2;
Thread thread = new Thread(this);
thread.start();
}
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0,width, height);
g.setColor(0, 0255, 255);
g.drawRect(width/2-RecX/2, height/2-RecY/2, RecX, RecY);
g.setColor(0,255,0);
g.fillArc(X, Y, 5, 5, 0, 360);
}
int movex=5;
int movey=5;
public void run() {
while(true){
try {
X = X + movex;
Y = Y + movey;
Thread.sleep(100);
repaint();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
用JAVA编写随机数生成一个4*4的方阵,然后分别算出两条对角线之和
package test;
import java.util.Random;
public class Test {
public static void main(String[] args) {
new Test().f();
}
/**
* 随机数生成一个4*4的方阵,然后分别算出两条对角线之和
*/
private void f(){
Random random=new Random();
int[][] arr=new int[4][4];
int sum1=0,sum2=0;
for(int i=0;i4;i++){
for(int j=0;j4;j++){
arr[i][j]=Math.abs(random.nextInt()%10);
System.out.print(arr[i][j]+" ");
if(i==j){
sum1+=arr[i][j];
}
if(i+j==3){
sum2+=arr[i][j];
}
}
System.out.println();
}
System.out.println("和是"+sum1);
System.out.println("和是"+sum2);
}
}
沿两条对角线动java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于两个对角线、沿两条对角线动java的信息别忘了在本站进行查找喔。
发布于:2022-12-04,除非注明,否则均为
原创文章,转载请注明出处。