大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
double[][] score = new double[4][25];
成都创新互联公司是一家专业提供东阳企业网站建设,专注与网站设计制作、成都做网站、H5技术、小程序制作等业务。10年已为东阳众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。
for(int i=0;i4;i++) {
for(int j=0;j25;j++) {
score [i][j] = 100;
System.out.print("第"+j+"个学生的第"+i+"个学科的成绩是"+score [i][j]);
}
}
大概就是这个意思,你可以再加个存学生名字的String数组,和存学科名字的String数组;
例如String[] studentName = new String[25];
studentName[0] = "XiaoMing";
......
String[] subjectName = new String[4];
subjectName[0] = "English";
......
System.out.print("第"+j+"个学生的第"+i+"个学科的成绩是"+score [i][j]);就可以改成
System.out.print(studentName[j]+“的"+subjectName[i]+"成绩是"+score [i][j]);
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class MyFrame extends JFrame implements ActionListener{
JTable table =null;
JLabel lblName =null,lblPwd=null;
JTextField txtName =null,txtPwd =null;
private JButton btnAdd=null,btnUpdate =null,btnDel=null,btnSave=null;
DefaultTableModel model =null;
String path = new String("E:\\my\\Test1\\src\\user.ini");
Container c =null;
public MyFrame(){
lblName = new JLabel("用户名");
lblPwd = new JLabel("密码");
txtName = new JTextField(20);
txtPwd = new JTextField(20);
Vector columns = new Vector();
columns.add("用户名");
columns.add("密码");
Vector vec = readFile();
model = new DefaultTableModel(vec,columns);
table = new JTable(model);
JScrollPane scrollpane = new JScrollPane(table);
table.setRowSelectionAllowed(true);
btnAdd = new JButton("增加");
btnUpdate = new JButton("修改");
btnDel = new JButton("删除");
JPanel p2 = new JPanel();
c = this.getContentPane();
btnAdd.addActionListener(this);
//btnSave.addActionListener(this);
btnUpdate.addActionListener(this);
btnDel.addActionListener(this);
p2.add(lblName);
p2.add(txtName);
p2.add(lblPwd);
p2.add(txtPwd);
p2.add(btnAdd);
p2.add(btnUpdate);
p2.add(btnDel);
c.add(p2,BorderLayout.SOUTH);
c.add(scrollpane);
this.setSize(800,600);
}
@Override
public void actionPerformed(ActionEvent e) {
String [] rows = new String[2];
String id=null;
if(e.getSource() == btnAdd){
String name=txtName.getText().trim();
String pwd = txtPwd.getText().trim();
writeFile(name, pwd);
rows[0] =name;
rows[1] = pwd;
model.addRow(rows);
}else if(e.getSource()== btnSave){
}else if(e.getSource()==btnUpdate){
}
}
public void writeFile(String name,String pwd){
FileWriter writer =null;
BufferedWriter bw =null;
try {
writer= new FileWriter(path,true);
bw = new BufferedWriter(writer);
String context = ":"+"userName="+name+";pwd="+pwd+"\r\n";
bw.write(context);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Vector readFile(){
FileReader reader=null;
BufferedReader br =null;
try {
reader = new FileReader(path);
br = new BufferedReader(reader);
String context= null;
Vector vec = new Vector();
while((context = br.readLine())!=null){
if(!"".equals(context)){
Vector user = new Vector();
user.add(context.substring(context.indexOf('=')+1,context.indexOf(';')));
user.add(context.substring(context.lastIndexOf('=')+1));
vec.add(user);
}
}
return vec;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
br.close();
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
先来分析一下思路,从键盘输入和获取随机数,都很容易,关键点就在怎样获取对角线。假设从键盘输入的数是n,总共生成n行n列的整数,遍历n行n列,要加的数就是第i行第i列,还有第i行第n-i+1列。其中,如果i=n-i+1,则这一行只加n,不用加n-i+1了,就是说,当i等于(n-1)/2时,这一行只加一次即可。
import java.util.Scanner;
import java.util.Random;
public static void main(String[] args) {
Random rd = new Random();
int result=0;
Scanner scan = new Scanner();
System.out.println("请输入行数:");
Int n = scan.nextInt();
int arr[][]=new int[n][n];
for(int i=0;in;i++){
for(int j=0;jn;j++){
arr[i][j]=rd.nextInt(100);
}
}
for(int k=0;karr.length;k++){
for(int l=0;larr[k].length;l++){
result+=arr[k][l];
if(k!=n-k+1){
result+=arr[k][l];
}
}
}
System.out.println(result);
}
java表格就是java swing。
//创建表头
String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };
//创建显示数据
Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
{ "Sue", "Black", "Knitting", new Integer(2),
new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
/*
* JTable还提供了一个重载的构造方法,传入两个Vector
* JTable(Vector rowData, Vector columnNames)
*
*/
final JTable table = new JTable(data, columnNames);
table.setBackground(Color.YELLOW);
我把你的程序改了一下,成绩能出来了,你在我注释//设置表格中人员姓名及各科成绩 的循环中设置成绩。你还应该在程序中设置一下什么时候允许点击哪个按钮,否则很容易就抛出异常的。
public class Aaa extends JFrame implements ActionListener {
JTable table;
Object[][] a;
Object[] name = { "姓名", "英语", "数学", "总成绩", };
JButton button1, button2;
JTextField inputNumber;
int rows = 1;
JPanel p;
Aaa() {
a = new Object[rows][4];
init(a);
setBounds(550, 200, 550, 200);
//setSize(550,200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
void init(Object[][] a) {
button2 = new JButton("计算总成绩");
button1 = new JButton("确定");
inputNumber = new JTextField(10);
button1.addActionListener(this);
button2.addActionListener(this);
table = new JTable(a, name);
p = new JPanel();
p.add(new JLabel("输入表格行数"));
p.add(inputNumber);
p.add(button2);
p.add(button1);
add(p, BorderLayout.SOUTH);
add(new JScrollPane(table), BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String temp = inputNumber.getText();
temp = temp==null || "".equals(temp) ? "0" : temp;
rows = Integer.parseInt(temp);
a = new Object[rows][4];
//设置表格中人员姓名及各科成绩
for (int i = 0; i a.length; i++) {
for (int j = 0; j a[i].length; j++) {
if(j==0){
a[i][j] = "aaa";
}else if(j==a[0].length-1){
a[i][j]=0;
}else{
a[i][j]=65;
}
}
}
table = new JTable(a, name);
getContentPane().removeAll();
init(a);
validate();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
if (e.getSource() == button2) {
for (int i = 0; i rows; i++) {
int sum = 0;
//boolean boo=true;
for (int j = 1; j a[i].length-1; j++) {
sum = sum + Integer.parseInt(a[i][j].toString());
//boo=false;
table.repaint();
//if(boo=true){
a[i][3] = "" + sum;
table.repaint();
}
}
}
}
public static void main(String[]args){
Aaa win=new Aaa();
win.setTitle("表格");
}
}
先看代码,挨句解释:
一般遍历使用两种方式,1:得到总的行数和每行的列数,然后循环。2:使用迭代
先看第一种:
Java代码
package com.golden.test;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
* @author 崔素强
*
*/
public class PoiReadXls2 {
public static void main(String[] args) {
File f = new File("c:\\a.xls");
try {
FileInputStream is = new FileInputStream(f);
HSSFWorkbook wbs = new HSSFWorkbook(is);
HSSFSheet childSheet = wbs.getSheetAt(0);
// System.out.println(childSheet.getPhysicalNumberOfRows());
System.out.println("有行数" + childSheet.getLastRowNum());
for (int j = 0; j childSheet.getLastRowNum(); j++) {
HSSFRow row = childSheet.getRow(j);
// System.out.println(row.getPhysicalNumberOfCells());
// System.out.println("有列数" + row.getLastCellNum());
if (null != row) {
for (int k = 0; k row.getLastCellNum(); k++) {
HSSFCell cell = row.getCell(k);
if (null != cell) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
System.out.print(cell.getNumericCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
System.out.print(cell.getStringCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
System.out.println(cell.getBooleanCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
System.out.print(cell.getCellFormula() + " ");
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println(" ");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知类型 ");
break;
}
} else {
System.out.print("- ");
}
}
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
得到Excel的文件然后读取,这个很简单。关键有两个地方,也许在网上会看到有的这样使用有的那样使用。
System.out.println("有行数" + childSheet.getLastRowNum());
System.out.println(childSheet.getPhysicalNumberOfRows());
System.out.println("有列数" + row.getLastCellNum());
System.out.println(row.getPhysicalNumberOfCells());
如果人都拷贝代码进行使用了,不知道有什么区别。太多的区别不知道,但是有一点我发现了,那就是如果中间各行或者隔列的话getPhysicalNumberOfRows和getPhysicalNumberOfCells就不能读取到所有的行和列了。
再者,一定要对单元格的格式进行判断switch (cell.getCellType()),不同的单元格格式使用不同的方法。最后加上为止类型,以防万一。
而且在数字类型里,又分为了纯数字和时间格式:
Java代码
case HSSFCell.CELL_TYPE_NUMERIC: // 数值型
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 如果是date类型则 ,获取该cell的date值
value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();
} else { // 纯数字
value = String.valueOf(cell.getNumericCellValue());
}
还有一种迭代的方法:
Java代码
package com.golden.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
*
* @author 崔素强
*
*/
public class PoiReadXls {
@SuppressWarnings( { "unchecked", "deprecation" })
public static void main(String[] args) {
File f = new File("c:\\a.xls");
try {
InputStream input = new FileInputStream(f);
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
// System.out.print("行:" + row.getRowNum() + " ");
Iterator cells = row.cellIterator();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
// System.out.println("列:" + cell.getCellNum());
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
System.out.print(cell.getNumericCellValue() + " ");
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
System.out.print(cell.getStringCellValue() + " ");
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
System.out.println(cell.getBooleanCellValue() + " ");
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
System.out.print(cell.getCellFormula() + " ");
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println(" ");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知类型 ");
break;
}
}
System.out.println();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
这种方法,如果数据的紧凑的,使用还是方便的,但是我发现,如果是空行或者是空列,他就会隔过去。具体的自己试试就知道了。
另外,也能看到这里得到Excel文件的方式是通过File,如果要引用到Struts2里,这是很简单的,因为Struts2上传时Action里定义的就是File或者File数组。