-11
座標(xy整数)ともう1つの整数を保存します。変数x、y、steps、および他のクラスのオブジェクトを作成してリンクリストに格納するクラスがありますが、これはforループ内のオブジェクトを作成する場合にのみ機能します。同じ名前のオブジェクトを作成するとき、コンパイラは "メソッドメインで既に定義されている変数"と言います。 forループの外側にオブジェクトを作成するにはどうすればよいですか?リンクされたリストにオブジェクトを追加する方法JAVA?
これは、別のオブジェクトだけでなく、これらの二つがあります私はこのような何かをしたいが、それは
import java.util.LinkedList;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
LinkedList<Coordinates> coords = new LinkedList<Coordinates>();
Coordinates xy = new Coordinates();
System.out.println("give x");
xy.x=sc.nextInt();
System.out.println("give y");
xy.y=sc.nextInt();
xy.steps=xy.steps+1;
coords.add(xy);
Coordinates xy = new Coordinates();
System.out.println("give x");
xy.x=sc.nextInt();
System.out.println("give y");
xy.y=sc.nextInt();
xy.steps=xy.steps+1;
coords.add(xy);
}
}
動作しないループ
import java.util.LinkedList;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
LinkedList<Coordinates> coords = new LinkedList<Coordinates>();
for (int j=0; j<3;j++)
{
Coordinates xy = new Coordinates();
System.out.println("give x");
xy.x=sc.nextInt();
System.out.println("give y");
xy.y=sc.nextInt();
xy.steps=xy.steps+1;
coords.add(xy);
}
}
}
のために働くものです!
コードを投稿して問題をよりよく説明する必要があります。あなたが何を求めているかは非常に不明です。 – Carcigenicate