我已经在这方面工作了一段时间,但似乎无法得到它。我需要将用户输入存储到另一个对象的数组中,但我不能让它工作。我不确定它是我的构造函数还是我错过了什么,但感谢任何帮助
这是输出程序
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 'p' to create a polygon");
String in = sc.next();
if (in.equals("p")) {
System.out.println("How many sides?");
int numSides = sc.nextInt();
int side=0;
Polygon ps;
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
side = sc.nextInt();
ps = new Polygon(side);
}
ps = new Polygon(side);
这是另一个类的构造函数
public class Polygon {
protected int[] sideLengths;
public Polygon(int sides){
sideLengths= new int[sides];
}
答案 0 :(得分:0)
你需要做
ps=new Polygon(numSides+1); //adding 1 because for-loop starts with index 1
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
side = sc.nextInt();
ps[i]=side;
}
由此我们所做的就是分配&#39; side&#39;用户输入到数组中特定索引的值。
希望这有帮助!
祝你好运答案 1 :(得分:0)
我理解你的程序的方式我认为你正在寻找这种逻辑
if (in.equals("p")) {
System.out.println("How many sides?");
int numSides = sc.nextInt();
int side = 0;
Polygon ps;
int sideLengths[] = new int[numSides];// array to store length of polygon of sides `numSides`
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
sideLengths[i-1] = sc.nextInt();// creating array to store lengths
}
ps = new Polygon(sideLengths);// generating your pollygon by sending the array
}
你的Polygon类看起来应该是这样的
class Polygon {
protected int[] sideLengths;// storing the lengths of all sides
public Polygon(int dimensions[]) {
sideLengths = dimensions ;
}
}
答案 2 :(得分:0)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 'p' to create a polygon");
String in = sc.next();
if(in.equalsIgnoreCase("p")) {
Integer sides[];
System.out.println("How many sides?");
int numSides = sc.nextInt();
int side=0;
if(numSides>0){
sides = new Integer[numSides];
}
Polygon ps;
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
side = sc.nextInt();
sides[i] = side;
}
ps = new Polygon(sides);
}
}
public class Polygon {
private Integer[] sides;
public Polygon(Integer[] sides){
this.sides = sides;
}
}
现在我们已经创建了一个大小为sides的数组,并且数组已作为参数传递给构造函数以初始化Polygon的对象
答案 3 :(得分:0)
它应该像
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 'p' to create a polygon");
String in = sc.next();
if (in.equals("p")) {
System.out.println("How many sides?");
int numSides = sc.nextInt();
int side=0;
Polygon ps = new Polygon(numSides);
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
side = sc.nextInt();
ps.addSide(side, i-1);
//ps = new Polygon(side);
}
//ps = new Polygon(side);
}}}
和Polygon类一样
public class Polygon {
protected int[] sideLengths;
public Polygon(int sides){
sideLengths= new int[sides];
}
public void addSide(int side, int index){
sideLengths[index] = side;
}
}
答案 4 :(得分:-1)
您正在正确调用构造函数,但在for循环结束后,您再次创建一个新的Polygon实例并将其分配给ps。创建一个int或ArrayList的Integer数组,并在for循环结束之前存储Polygon边长的每个实例。
ArrayList<int> psListpsSideLengthList = new ArrayList<int>;
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
side = sc.nextInt();
psListpsSideLengthList.add(side);
}