java i = 1,2.。。。10,那么xi = x1,x2...x10该如何实现

swtongxue 2017-09-16 11:51:47
在java 中 如果i = 1,2.。。。10,那么令xi = x1,x2...x10该如何实现。已知j = 1.。。。10,那么shape aj = new Rectanrle2D.Double(xj,yj,cj,kj);该如何实现
...全文
366 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
海涅 2017-09-20
  • 打赏
  • 举报
回复
用数组不就好了
swtongxue 2017-09-20
  • 打赏
  • 举报
回复
引用 5 楼 qq_36859854 的回复:
用可变参接收实参试试?
可以说得具体一点吗??我就算是刚入门,不知道您说的这是什么意思
swtongxue 2017-09-20
  • 打赏
  • 举报
回复
引用 6 楼 cyh_syp 的回复:
int j = 1; String str1 = "x"+j; String str2 = "y"+j; String str3 = "c"+j; String str4 = "k"+j; shape aj = new Rectanrle2D.Double(str1,str2,str3,str4);
这样是不行的,我想用的xj,yj,cj,kj都是int类型,现在得到的是String了,即使强制转换了也是不可以的呢
cyh_syp 2017-09-20
  • 打赏
  • 举报
回复
Map<String,Integer> map = new HashMap<String,Integer>(); for(int i=1;i<=12;i++){ map.put("x"+i, i); } System.out.println("x12="+map.get("x12")); System.out.println"x12类型:"+map.get("x12").getClass()); //结果: x12=12 x12类型:class java.lang.Integer
cyh_syp 2017-09-20
  • 打赏
  • 举报
回复
int x12 = (int)(20 + Math.random() * (396 - 20 + 1)); 你是想创建12这样的参数?
cyh_syp 2017-09-18
  • 打赏
  • 举报
回复
int j = 1; String str1 = "x"+j; String str2 = "y"+j; String str3 = "c"+j; String str4 = "k"+j; shape aj = new Rectanrle2D.Double(str1,str2,str3,str4);
阿尔法程序狗 2017-09-17
  • 打赏
  • 举报
回复
用可变参接收实参试试?
swtongxue 2017-09-16
  • 打赏
  • 举报
回复
我的代码 package lw1.view; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Area; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import java.io.FileWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Random; import java.util.Vector; import java.awt.*; import javax.swing.*; import lw1.db.SqlHelper; import lw1.model.AreaModel; import lw1.tools.MyTools; public class HuaTu extends JDialog implements ActionListener{ int i = 0; int j = 1; int k = 0; int a = 0; int hgap = 10; int vgap = 10; //定义swin组件 JButton jb1,jb2,jb3,jb4,jb5; JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7; JTextField jtf1,jtf2,jtfi,jtfj,jtfk; JLabel p5_jl1,p5_jl2,p5_jl3,p5_jl4,p6_jl1,p6_jl2,p6_jl3,p6_jl4,p6_jli,p6_jlj,jlk; JTable jt = null; JScrollPane jsp = null; int s = 0; AreaModel ht = null; DrawRectanglePanel rectPanel = new DrawRectanglePanel(); // 创建面板类的实例 Random rand = new Random(); int xj = 0;int yj = 0;int cj = 0;int kj = 0; int x1 = (int)(20 + Math.random() * (360 - 20 + 1)); int y1 = (int)(20 + Math.random() * (140 - 20 + 1)); int c1 = 60;int k1 = 80; int x2 = (int)(20 + Math.random() * (396 - 20 + 1)); int y2 = (int)(20 + Math.random() * (170 - 20 + 1)); int c2 = 24;int k2 = 50; int x3 = (int)(20 + Math.random() * (396 - 20 + 1)); int y3 = (int)(20 + Math.random() * (170 - 20 + 1)); int c3 = 24;int k3 = 50; int x4 = (int)(20 + Math.random() * (390 - 20 + 1)); int y4 = (int)(20 + Math.random() * (180 - 20 + 1)); int c4 = 30;int k4 = 40; int x5 = (int)(20 + Math.random() * (380 - 20 + 1)); int y5 = (int)(20 + Math.random() * (148 - 20 + 1)); int c5 = 40;int k5 = 72; int x6 = (int)(20 + Math.random() * (380 - 20 + 1)); int y6 = (int)(20 + Math.random() * (148 - 20 + 1)); int c6 = 40;int k6 = 72; int x7 = (int)(20 + Math.random() * (396 - 20 + 1)); int y7 = (int)(20 + Math.random() * (170 - 20 + 1)); int c7 = 24;int k7 = 50; int x8 = (int)(20 + Math.random() * (390 - 20 + 1)); int y8 = (int)(20 + Math.random() * (140 - 20 + 1)); int c8 = 30;int k8 = 80; int x9 = (int)(20 + Math.random() * (396 - 20 + 1)); int y9 = (int)(20 + Math.random() * (180 - 20 + 1)); int c9 = 24;int k9 = 40; int x10 = (int)(20 + Math.random() * (380 - 20 + 1)); int y10 = (int)(20 + Math.random() * (180 - 20 + 1)); int c10 = 40;int k10 = 40; int x11 = (int)(20 + Math.random() * (260 - 20 + 1)); int y11 = (int)(20 + Math.random() * (100 - 20 + 1)); int c11 = 160;int k11 = 120; int x12 = (int)(20 + Math.random() * (396 - 20 + 1)); int y12 = (int)(20 + Math.random() * (170 - 20 + 1)); int c12 = 24;int k12 = 50; // public HuaTu(Frame owner, String title, boolean modal){ super(owner,title,modal); jp3 = new JPanel(new GridLayout(2,1,hgap,vgap)); jb1 = new JButton("生成"); jb1.addActionListener(this); jb2 = new JButton("删除"); jb2.addActionListener(this); jp3.add(jb1); jp3.add(jb2); jp2 = new JPanel(new BorderLayout()); jp2.add(jp3,"North"); jp5 = new JPanel(new BorderLayout()); jp5.add(rectPanel); jp1 = new JPanel(new BorderLayout()); jp1.add(jp2,"East"); jp1.add(jp5,"Center"); jp4 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); jb3 = new JButton("确定"); jb3.addActionListener(this); jb4 = new JButton("取消"); jb4.addActionListener(this); jp4.add(jb3); jp4.add(jb4); this.setLayout(new BorderLayout()); //this.add(jsp); this.add(jp1, BorderLayout.CENTER); this.add(jp4, BorderLayout.SOUTH); //展现 this.setSize(600,400); this.setLocation(250, 100); //this。setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } class DrawRectanglePanel extends JPanel { // 创建内部面板类 public void paintComponent(Graphics g) { // 重写paint()方法 Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Shape a0 = new Rectangle2D.Double(20,20,400,200); Area b0 = new Area(a0); int[] ary = new int[10]; // 集合 ,临时集合temp存放1~10个数字 ArrayList<Integer> temp = new ArrayList<Integer>(); //list集合存放需要的数字 ArrayList<Integer> list = new ArrayList<Integer>(); // 给集合添加1~10的数字 for (int i = 1; i < 11; i++) { temp.add(i); } // while循环 随即抽取集合的数字给数组 int index = 0; while (true) { if(list.size()==10){ break; } int it = temp.get((int) (Math.random() * 10)); //如果list存在这个数组就继续循环 if (list.contains(it)) { continue; } else { //如果list不存在这个数字,那么就把这个数字给数组,并且在list中加入这个数字 ary[index] = it; list.add(it); index++; } } // /输出数组的元素 for (int i = 0; i < ary.length; i++) { //System.out.print(ary[i] + " "); int j = 0; j = ary[i]; String s = "x" + j; int ss = Integer.parseInt(s); Shape a = new Rectangle2D.Double(ss,yj,cj,kj); Area bj = new Area(a); //System.out.println(str); g2.draw(b0); g2.draw(bj); } } } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }
swtongxue 2017-09-16
  • 打赏
  • 举报
回复
引用 1 楼 cyh_syp 的回复:
int j = 1; String str= "x"+j;
我这里面aj,xj,yj,cj,kj都涉及到这个问题
swtongxue 2017-09-16
  • 打赏
  • 举报
回复
引用 1 楼 cyh_syp 的回复:
int j = 1; String str= "x"+j;
那我想用在这里shape aj = new Rectanrle2D.Double(xj,yj,cj,kj);该如何实现呢
cyh_syp 2017-09-16
  • 打赏
  • 举报
回复
int j = 1; String str= "x"+j;

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧