I cleaned up a bit your code so it compiles, you might be better off with two classes
Josephus.java
import java.util.Scanner;
public class Josephus {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("How many players");
int numPlayers = in.nextInt();
Link first = new Link(1);
Link last = new Link(1);
for (int k = 2; k <= numPlayers; k++) {
last.next = new Link(k);
last = last.next;
}
last.next = first;
first.next = last;
System.out.println("How many skips");
int m = in.nextInt();
for (int g = 0; g < numPlayers; g++) {
for (int k = 0; k <= m; k++)
{
last = last.next;
}
// last.next;
last = last.next;
}
in.close();
}
}
and Link.java
public class Link {
public int num;
public Link next;
public Link(int d) {
num = d;
next = null;
}
}
This compiles and accepts input, then throws an error. I haven't fixed that since I don't know exactly what you're trying to achieve.
Changes:
- Changed `"class main"` to `public static void main(){...)` The Java runtime will look for this method when it's called.
- Extracted class `Link` to it's own file other solutions are possible, like some other answers say, you could declare it as static or instantiate `Josephus` (and probably some other ways)
- Commented out the line `last.next`, this doesn't actually solve any problem, it just eliminates the compilation error and allows you to compile, since I don't see what you're trying to do here I couldn't think of a better solution.
- It's not necessary to compile but I added the line `in.close();` at the end of the main method to free allocated resources, without this line it'll throw a compile warning.
- Added `first.next = last;` to avoid the null pointer exception that was happening.
All that you should need now is to implement the Josephus logic inside the nested loops and output your results (if desired) all of which should be quite language agnostic.