【发布时间】:2013-09-29 20:45:09
【问题描述】:
我正在尝试解决约瑟夫斯问题,但我不允许使用其他人的代码 sn-ps。考虑到这一点,我的代码中有 27 个错误,但不知道如何修复它。您能向我解释为什么它不会编译吗?我想看看我的逻辑是否有缺陷,但我无法测试它,因为它不会编译。任何其他提示都非常受欢迎!这是我的代码:
import java.util.*;
public class Josephus
{
public class Link
{
public int num;
public Link next;
public Link (int d)
{
num = d;
next = null;
}
}
public class Main
{
Scanner in = new Scanner(System.in);
System.out.println("How many players");
int numPlayers = in.nextInt();
Link first, last;
first = last = new Link (1);
for(int k=2; k<=numPlayers; k++)
{
last.next = new Link(k);
last = last.next;
}
last.next = first;
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;
}
}
}
【问题讨论】:
-
考虑使用带有语法高亮的 IDE..
-
您使用的是什么 IDE?日食?
-
-1 至少没有引用前 2-3 个错误。
-
除了明显的语法错误外,请注意:
first = last = new Link (1);。这很危险。对象不像基元。 2 变量将引用相同的对象。您可能需要 2 个不同的对象... -
好的,我的导师要求我们使用 Jgrasp。错误是“预期的标识符”并指向括号,但现在使用“public static void Main (String[] args)”解决方案后只有4个错误首先是指向公共类和void之间的空间的相同错误静态无效主要(字符串 [] 参数)。下一个错误是无效的方法声明;需要返回类型 public class void Main (String [] args)。这个错误指向大写M。下一个错误是error: not a statement last.next;这指向上一个和下一个之间的时间段。我能修复的最后一个。
标签: java compilation josephus