【问题标题】:jGRASP says my for loop is incorrectjGRASP 说我的 for 循环不正确
【发布时间】:2013-10-23 21:50:10
【问题描述】:

这是我的 for 循环... jGRASP 说从我的 for 循环开始有一个问题,我看不到问题,需要一些帮助来修复我的代码...

公共类 DirectoryLookup { public static void main(String[] args) 抛出 IOException {

  if (args.length == 0) {
      System.out.println("Please supply data file");
      System.exit(0);
      }

  else {
      System.out.println("Database server is Ready for Number Lookups!");
      }

  File inputDataFile = new File(args[0]);
  Scanner inputFile = new Scanner(inputDataFile)
  Scanner input = new Scanner(input.in);

  int n = inputFile.nextInt();
  int list[] = new int[n];

  found = false;

  while (Scanner.hasNext()); {
     System.out.println("Enter number to check value");
     int x = input.nextInt();

     for (i = 0, i < n, i++); 
        if (x == list(i)); {
           found = true; break 
           System.out.println("x is in the list");

        else {
           System.out.println("x is not in the list");
           }
     }

【问题讨论】:

    标签: java


    【解决方案1】:

    我在你的 for 循环中看到的第一个问题是你从不将 i 声明为整数。你不得不说 for(int i = 0...)。

    我看到的第二个问题是您忘记了 for 循环的括号。它以分号结尾,而不是括号和右括号。

    我看到的第三个问题是你永远不会关闭你的 if 语句括号。现在 else 语句在 if 语句中。这没有任何逻辑或句法意义。

    您的 while 语句条件也错误。而不是scanner.blah 您需要使用输入或输入文件使用的变量名称。

    您从未声明找到您的布尔变量。

    希望有所帮助,

    最好的问候

    【讨论】:

      【解决方案2】:

      while 条件、for 循环声明和if 条件后面都有分号。它们需要被删除,否则 Java 会将分号本身视为正文。

      break 语句中缺少分号,else 之前缺少结束大括号 }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-22
        • 1970-01-01
        相关资源
        最近更新 更多