输入以个维数,输出以下形式的矩阵和数列,以维数N=4为例:
0 0 0 0
0 1 1 1
0 1 2 2
0 1 2 3
用小应用程序编昂 下边是我编的
前台:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField; public class JLMC extends Applet implements ActionListener{
JTextField t1=new JTextField("",10);
JTextField t2=new JTextField("",10);
JButton b1=new JButton("确定");
JButton b2=new JButton("清除");
JLabel l1=new JLabel("输入题号");
JLabel l2=new JLabel("输入维数");
JTextArea tt=new JTextArea("",50,10);
JPanel pp=new JPanel();
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b2)){
t1.setText("");
t2.setText("");
tt.setText("");
if (e.getSource().equals(b1)){
}}}
public void init(){
this.setLayout(new BorderLayout());
this.add(pp,BorderLayout.SOUTH);
this.add(tt,BorderLayout.CENTER);
JPanel Panel=new JPanel();
Panel.setLayout(new GridLayout(2,4,12,6));
pp.setLayout(new FlowLayout());
pp.add(l1);
pp.add(t1);
pp.add(l2);
pp.add(t2);
pp.add(b1);
pp.add(b2); b1.addActionListener(this);
b2.addActionListener(this);
}
}
后台:
public class Text {
int a [][];
int l;
public Text (int w){
l=w;
a=new int[l][l];
for (int i=0;i <l;i++){
if (w <=i)
a[i][w]=a[i][w-1]+1;
else
a[i][w]=a[i][w-1]; System.out.print(a[i][w]+""+(w==l-1?"\n":" "));
}
} }
就是不会调用
0 0 0 0
0 1 1 1
0 1 2 2
0 1 2 3
用小应用程序编昂 下边是我编的
前台:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField; public class JLMC extends Applet implements ActionListener{
JTextField t1=new JTextField("",10);
JTextField t2=new JTextField("",10);
JButton b1=new JButton("确定");
JButton b2=new JButton("清除");
JLabel l1=new JLabel("输入题号");
JLabel l2=new JLabel("输入维数");
JTextArea tt=new JTextArea("",50,10);
JPanel pp=new JPanel();
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b2)){
t1.setText("");
t2.setText("");
tt.setText("");
if (e.getSource().equals(b1)){
}}}
public void init(){
this.setLayout(new BorderLayout());
this.add(pp,BorderLayout.SOUTH);
this.add(tt,BorderLayout.CENTER);
JPanel Panel=new JPanel();
Panel.setLayout(new GridLayout(2,4,12,6));
pp.setLayout(new FlowLayout());
pp.add(l1);
pp.add(t1);
pp.add(l2);
pp.add(t2);
pp.add(b1);
pp.add(b2); b1.addActionListener(this);
b2.addActionListener(this);
}
}
后台:
public class Text {
int a [][];
int l;
public Text (int w){
l=w;
a=new int[l][l];
for (int i=0;i <l;i++){
if (w <=i)
a[i][w]=a[i][w-1]+1;
else
a[i][w]=a[i][w-1]; System.out.print(a[i][w]+""+(w==l-1?"\n":" "));
}
} }
就是不会调用
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField; public class JLMC extends JFrame implements ActionListener{
JTextField t1=new JTextField("",10);
JTextField t2=new JTextField("",10);
JButton b1=new JButton("确定");
JButton b2=new JButton("清除");
JLabel l1=new JLabel("输入题号");
JLabel l2=new JLabel("输入维数");
JTextArea tt=new JTextArea("1111111111111111111111",50,10);
JPanel pp=new JPanel();
public JLMC()
{
init();
} public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b2)){
t1.setText("");
t2.setText("");
tt.setText("");
}
if (e.getSource().equals(b1)){
paint();
}
}
public void paint()
{
String num = t2.getText();
try{
int intNum = Integer.parseInt(num);
int aa[][] = new Text(intNum).a;
String text = "";
DecimalFormat d =null;
if(num.length()==2)
{
d = new DecimalFormat("00");
}else if(num.length()==3)
{
d = new DecimalFormat("000");
}else if(num.length()==4)
{
d = new DecimalFormat("0000");
}else
{
d = new DecimalFormat("0");
}
for(int i=0;i<aa.length;i++)
{
for(int j=0;j<aa.length;j++)
{
text = text+d.format(aa[i][j])+" ";
}
text = text+"\n";
}
tt.setText(text);
System.out.println(text);
}catch(Exception e)
{
e.printStackTrace();
//如何弹出窗口!不会
System.out.println("你输入的不是一个整数!");
}
} public void init(){
this.setLayout(new BorderLayout());
this.setSize(500,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tt.setText("2222222222222222");
this.add(pp,BorderLayout.SOUTH);
this.add(tt,BorderLayout.CENTER);
JPanel Panel=new JPanel();
Panel.setLayout(new GridLayout(2,4,12,6));
pp.setLayout(new FlowLayout());
pp.add(l1);
pp.add(t1);
pp.add(l2);
pp.add(t2);
pp.add(b1);
pp.add(b2); b1.addActionListener(this);
b2.addActionListener(this);
this.setVisible(true);
}
public static void main(String[]args)
{
new JLMC();
}
}
package test;
public class Text {
int a [][];
int l;
public Text (int w){
l=w;
int index=0;
a=new int[l][l];
for (int i=0;i<l;i++){
for(int j=0;j<l;j++)
{
if(i==0||j==0)
{
a[i][j]=0;
}
else if(i==j)
{
a[i][j]=index;
}else if(i<j)
{
a[i][j]=a[i][j-1];
}else
{
a[i][j] = a[i][j-1]+1;
}
System.out.print(a[i][j]+" ");
}
index++;
System.out.println();
}
} }
}}} 在这里调用 new Text();但是你的程序应该只能在Console下打印这些内容,而且没有注释,变量名不清,所以不能猜到t1,t2,tt的作用
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;public class JLMC extends JApplet implements ActionListener{
JTextField t1=new JTextField("",10);
JTextField t2=new JTextField("",10);
JButton b1=new JButton("确定");
JButton b2=new JButton("清除");
JLabel l1=new JLabel("输入题号");
JLabel l2=new JLabel("输入维数");
JTextArea tt=new JTextArea("",50,10);
JPanel pp=new JPanel();
public JLMC()
{
} public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b2)){
t1.setText("");
t2.setText("");
tt.setText("");
}
if (e.getSource().equals(b1)){
paint();
}
}
public void paint()
{
String num = t2.getText();
try{
int intNum = Integer.parseInt(num);
int aa[][] =new Text(intNum).a;
String text = "";
DecimalFormat d =null;
if(intNum>10 && intNum<101)
{
d = new DecimalFormat("00");
}else if(intNum>100 && intNum<1001)
{
d = new DecimalFormat("000");
}else if(intNum>1000 && intNum<10001)
{
d = new DecimalFormat("0000");
}else
{
d = new DecimalFormat("0");
}
for(int i=0;i<aa.length;i++)
{
for(int j=0;j<aa.length;j++)
{
text = text+d.format(aa[i][j])+" ";
}
text = text+"\n";
}
tt.setText(text);
System.out.println(text);
}catch(Exception e)
{
e.printStackTrace();
//如何弹出窗口!不会
JOptionPane.showMessageDialog(this,"你输入的不是一个整数!");
}
} public void init(){
this.setLayout(new BorderLayout());
this.setSize(500,400);
this.add(pp,BorderLayout.SOUTH);
this.add(tt,BorderLayout.CENTER);
JPanel Panel=new JPanel();
Panel.setLayout(new GridLayout(2,4,12,6));
pp.setLayout(new FlowLayout());
pp.add(l1);
pp.add(t1);
pp.add(l2);
pp.add(t2);
pp.add(b1);
pp.add(b2); b1.addActionListener(this);
b2.addActionListener(this);
this.setVisible(true);
}
public static void main(String[]args)
{
new JLMC();
}}