import java.io.*;
import java.util.*;
import java.awt.*;
public class PracDriver {
public static void main ( String[] args) throws FileNotFoundException{
DrawingPanel panel = new DrawingPanel(600, 600);
Graphics g = panel.getGraphics();
Picture pic1 = new Picture<>();
Scanner read = new Scanner(System.in);
String filename = read.nextLine();
File file = new File(filename);
System.out.println(filename);
read.close();
Scanner in = new Scanner("start picture A");
in.useDelimiter(" ");
while(in.hasNextLine()){
String token1 = in.next();
if (token1.equalsIgnoreCase("start")){
System.out.println("john");
String token2 = in.next();
if(token2.equalsIgnoreCase("picture")){
System.out.println("doe");
char t = in.next().charAt(0);
Picture t = new Picture();
}
else{
System.out.println("Improper imput from file");
}
}
else if (token1.equalsIgnoreCase("end")){
System.out.println("john");
String token2 = in.next();
if(token2.equalsIgnoreCase("picture")){
System.out.println("doe");
}
}
else if(token1.equalsIgnoreCase("erase")){
g.setColor(Color.WHITE);
g.drawRect(0, 0, 600, 600);
g.fillRect(0, 0, 600, 600);
g.setColor(Color.BLACK);
}
else if(token1.equalsIgnoreCase("cirlce")){
int token2 = in.nextInt();
int token3 = in.nextInt();
int token4 = in.nextInt();
Circle circ = new Circle("circ", token2, token3, token4);
pic1.addShape(circ);
}
因此,在我的扫描仪中,我有一些测试代码(应该从文件中读取,但这并不重要),该代码读取“起始图片A”。我有一个定界符,因此它分别读取每个字符串。它包含“开始”和“图片”部分,并且看起来工作正常。问题是我的图片必须以“开始图片”之后的char命名,在这种情况下为A。因此,我需要输入该char的输入,然后以它命名我的Picture变量。因此,在这种情况下,我需要将图片称为图片A = new Picture();。问题是我不知道该怎么做。我无法将char转换为我的Picture类对象。另一个问题是我的if else语句的其余部分也需要使用新图片(在本例中为图片A)来添加Shapes(我的类)。但这不会 工作一个局部变量。我将不胜感激。也忽略图片p1。需要将其删除或更换。
您试图做的事是不可能的。但是,您正在做出一些严肃的设计决策,这些决策使您尝试做的事情变得无关紧要,因为您可以使用其他方法来完成所需的工作。
第一件事:您的代码,所有变量以及您在源文件中放入的任何在编译时就应该知道的东西都称为“编译时”依赖项或引用。这些是Java编译器将要求您指定的内容,然后任何人都可以运行该程序。变量名称包含在此列表中(进一步详细介绍将尝试讲授基本的Java,因此在这里我不会做...)
根据用户输入,您需要代码执行的所有操作都称为“运行时”依赖项或引用。命令行上的用户输入,从文件系统加载的文件,UI事件等都是可以在何处构造编译后的代码以处理这些编译时未知项(因此,运行时)的示例。
如果您需要基于某些用户输入来标识对象,请在类中声明一个方法,可用于为其分配用户输入值并创建所需的映射。使用aMap
是另一种方式,您可以将对象与键(键/值对)关联并完成您要执行的操作。
例子:
public class Picture {
private String name;
// other properties omitted for clarity
public Picture() {
this.name = "some default";
}
// use getters/setters to access and assign object data, which can include
// user-input obtained at runtime
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
}
在您的代码中,您可以通过执行与您的操作类似的操作来利用此功能:
if (token1.equalsIgnoreCase("start")){
System.out.println("john");
String token2 = in.next();
if(token2.equalsIgnoreCase("picture")){
System.out.println("doe");
char inputChar = in.next().charAt(0); // use a more descriptive name...
Picture picture = new Picture();
picture.setName(Character.toString(inputChar)); // note the "generic" name for this picture object variable. The runtime information is stored in the object as a property.
}
else{
System.out.println("Improper imput from file");
}
}
这里重要的一点是要注意,用于引用对象的变量名称与程序在运行时对程序的意图无关,它用于在开发过程中确定目的和范围。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句