Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I cleaned up a bit your code so it compiles, you might be better off with two classes</p> <p>Josephus.java</p> <pre><code>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 &lt;= 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 &lt; numPlayers; g++) { for (int k = 0; k &lt;= m; k++) { last = last.next; } // last.next; last = last.next; } in.close(); } } </code></pre> <p>and Link.java</p> <pre><code>public class Link { public int num; public Link next; public Link(int d) { num = d; next = null; } } </code></pre> <p>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.</p> <p><strong>Changes:</strong></p> <ol> <li>Changed `"class main"` to `public static void main(){...)` The Java runtime will look for this method when it's called.</li> <li>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)</li> <li>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. </li> <li>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. </li> <li>Added `first.next = last;` to avoid the null pointer exception that was happening.</li> </ol> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload