好的,在我的课堂上,我们正在开展一个项目,我们必须将程序中的每个数字加倍。我们这样做是为了扩展我们为课堂制作的动画。这是我的类缩放类:
import java.util.Scanner;
import java.io.*;
public class DoubleEmRight
{
public static void main(String[]args) throws Exception
{
File file = new File(args[0]);
Scanner kb = new Scanner(file);
FileWriter input = new FileWriter("HolidayGraphics2.java");
PrintWriter output = new PrintWriter(input);
String s;
do
{
int xn = 0;
int countnum = 0;
int xl = 0;
int countlet = 0;
boolean hasNum = false;
boolean mult = false;
s = kb.nextLine();
String[]spLet = s.split("\\D"); //array w/ nums
String[]spNum = s.split("\\d"); //array w/ letters
String[]blklst = {"try", "for", "Color", "if", "waves.addPoint(x"};
for (int i = 0; i < spLet.length; i++)
{
if (spLet[i].length() >=1)
{
countnum++;
}
}
String[]spLetNoSpc = new String[countnum];
for (int i = 0; i < spLet.length; i++)
{
if (spLet[i].length() >=1)
{
spLetNoSpc[xn] = spLet[i];
xn++;
}
}
for (int i = 0; i < spNum.length; i++)
{
if (spNum[i].length() >=1)
{
countlet++;
}
}
String[]spNumNoSpc = new String[countlet];
for (int i = 0; i < spNum.length; i++)
{
if (spNum[i].length() >=1)
{
spNumNoSpc[xl] = spNum[i];
}
}
search:
for (int x = 0; x < spLetNoSpc.length ; x++)
{
find:
for (int j = 0; j < blklst.length; j++)
{
if (s.contains(blklst[j]))
{
mult = true;
break find;
}
}
if (!(s.matches(".*[0-9].*")))
{
output.print(s);
break search;
}
else if (s.length() == 0)
{
output.print(s);
break search;
}
else if (s.matches("for (int x = 0; x < 500; x++)"))
{
output.print(spNumNoSpc[x] + (Integer.parseInt(spLetNoSpc[x])) * Integer.parseInt(args[1]));
if ((x == spLetNoSpc.length - 1)&&(spNumNoSpc.length > spLetNoSpc.length))
{
output.println(spNumNoSpc[x+1]);
}
break search;
}
else if (mult)
{
output.print(s);
break search;
}
else
{
output.print(spNumNoSpc[x] + (Integer.parseInt(spLetNoSpc[x])) * Integer.parseInt(args[1]));
if ((x == spLetNoSpc.length - 1)&&(spNumNoSpc.length > spLetNoSpc.length))
{
output.println(spNumNoSpc[x+1]);
}
}
}
output.println();
}while(kb.hasNextLine());
output.close();
input.close();
}
}
这会将我动画的java文件中的所有数字加倍,并将其打印在一个新文件HolidayGraphics2.java中。不幸的是,它不会打印这样的行:
import javax.swing.*;
但打印这样的行:
win.setSize(500,650);
也不打印我的任何括号。 如果该行不包含数字,则这些行应该从我的动画程序中打印原始行:
if (!(s.matches(".*[0-9].*")))
{
output.print(s);
break search;
}
有谁能告诉我为什么不包含数字的行不打印?这是动画的代码,以防你需要它
import javax.swing.*;
import java.awt.*;
import javax.swing.ImageIcon;
//import java.awt.Graphics2D;
public class HolidayGraphics
{
public static void main (String[] args)
{
JFrame win;
Container contentPane;
Graphics g;
Panel controlPanel;
win = new JFrame("Holiday Graphics");
win.setSize(500,650);
win.setLocation(100,100);
win.setVisible(true);
contentPane = win.getContentPane(); // activates graphics in window
g = contentPane.getGraphics();
try {Thread.sleep(400);} catch (Exception e) {}
for(int w = 0; w < 10000; w++)
{
g.setColor(new Color(231,252,252)); //sky
g.fillRect(0,0,500,650);
g.setColor(Color.YELLOW); //sun
g.fillOval(20,20,80,80);
g.drawLine(60,110,60,145);
g.drawLine(40,90,5,125);
g.drawLine(80,90,115,125);
g.drawLine(80,30,115,0);
g.drawLine(110,60,145,60);
g.setColor(new Color(255,255,255)); //ice
g.fillRect(300,170,200,50);
Polygon waves = new Polygon(); //waves
for (int x = 0; x < 500; x++)
{
waves.addPoint(x, 180 + (int)Math.sin(x*((Math.PI)/2))*3);
}
waves.addPoint(500, 650);
waves.addPoint(0, 650);
g.setColor(new Color(67,129,243));
g.fillPolygon(waves);
g.setColor(new Color(67,129,243)); //water
g.fillRect(0,190,500,700);
g.setColor(new Color(255,255,255)); //ice over water
g.fillRect(300,170,200,30);
for (int i = 0; i < 21; i = i+2)
{
g.setColor(Color.WHITE);
g.fillRect(340,174 - i*6,10,6);
}
g.setColor(Color.RED);
g.fillRect(340,168,10,6);
g.setColor(Color.RED);
g.fillRect(340,156,10,6);
g.setColor(Color.RED);
g.fillRect(340,144,10,6);
g.setColor(Color.RED);
g.fillRect(340,132,10,6);
g.setColor(Color.RED);
g.fillRect(340,120,10,6);
g.setColor(Color.RED);
g.fillRect(340,108,10,6);
g.setColor(Color.RED);
g.fillRect(340,96,10,6);
g.setColor(Color.RED);
g.fillRect(340,84,10,6);
g.setColor(Color.RED);
g.fillRect(340,72,10,6);
g.setColor(Color.RED);
g.fillRect(340,60,10,6);
g.setColor(Color.WHITE); //sign
g.fillOval(305,24,80,30);
g.setColor(Color.RED);
g.drawOval(305,24,80,30);
g.setColor(Color.RED);
g.drawString("North Pole",313,43);
g.setColor(Color.ORANGE); //penguin
g.fillOval(400,178,20,10);
g.fillOval(425,178,20,10);
g.setColor(Color.BLACK);
g.fillOval(388,82,70,100);
g.fillOval(385,132,15,30);
g.fillOval(446,132,15,30);
g.setColor(Color.WHITE);
g.fillOval(400,102,25,25);
g.fillOval(421,102,25,25);
g.fillOval(398,123,50,55);
g.setColor(Color.BLACK);
g.fillOval(410,110,6,6);//eye
g.fillOval(430,110,6,6);//eye
g.drawLine(406,110,415,105);//eyebrow
g.drawLine(439,110,430,105);//eyebrow
g.setColor(Color.ORANGE);
g.fillOval(400,178,20,10);
g.fillOval(425,178,20,10);
Polygon nose = new Polygon();
nose.addPoint(415,118);
nose.addPoint(431,118);
nose.addPoint(423,126);
g.fillPolygon(nose);
g.setColor(new Color(255,122,119)); //house
g.fillRect(150,450,200,100);
g.setColor(Color.BLACK);
g.drawString("Santa's Workshop", 190, 470);
for (int i = 0; i < 7; i=i+2)
{
g.setColor(Color.RED);
g.fillRect(150,480 + 10*i,10,10);
}
for (int i = 1; i < 6; i=i+2)
{
g.setColor(Color.WHITE);
g.fillRect(150,480 + 10*i,10,10);
}
for (int i = 0; i < 7; i=i+2)
{
g.setColor(Color.RED);
g.fillRect(340,480 + 10*i,10,10);
}
for (int i = 1; i < 6; i=i+2)
{
g.setColor(Color.WHITE);
g.fillRect(340,480 + 10*i,10,10);
}
g.setColor(new Color(124,29,5)); //chimney
g.fillRect(170,400,10,50);
g.setColor(new Color(130,62,0)); //roof
Polygon tri1 = new Polygon();
tri1.addPoint(150,450);
tri1.addPoint(250,390);
tri1.addPoint(350,450);
g.fillPolygon(tri1);
for (int i = 0; i < 390; i++)
{
g.setColor(Color.ORANGE);
g.fillOval(70+i,350,40,20); //fish 1
Polygon fishtri1 = new Polygon();
fishtri1.addPoint(70+i,350);
fishtri1.addPoint(70+i,370);
fishtri1.addPoint(87+i,360);
g.fillPolygon(fishtri1);
g.fillOval(70+i,220,40,20); //fish 2
Polygon fishtri2 = new Polygon();
fishtri2.addPoint(70+i,220);
fishtri2.addPoint(70+i,240);
fishtri2.addPoint(87+i,230);
g.fillPolygon(fishtri2);
g.fillOval(430 - i,285,40,20); //fish 3
Polygon fishtri3 = new Polygon();
fishtri3.addPoint(470-i,285);
fishtri3.addPoint(470-i,305);
fishtri3.addPoint(453-i,295);
g.fillPolygon(fishtri3);
try {Thread.sleep(10);} catch (Exception e) {}
if (!(i==389))
{
g.setColor(new Color(67,129,243));
g.fillOval(70+i,350,40,20);
g.fillPolygon(fishtri1);
g.fillOval(70+i,220,40,20);
g.fillPolygon(fishtri2);
g.fillOval(430 - i,285,40,20);
g.fillPolygon(fishtri3);
}
}
for (int i = 0; i < 7; i++) //bubbles
{
g.setColor(Color.BLUE);
g.drawOval(167 - i*15, 380-i*30, 10 + i*i, 10 + i*i);
try {Thread.sleep(200*i);} catch (Exception e) {}
}
}
}
}