【问题标题】:I don't understand how to fix the syntax error我不明白如何修复语法错误
【发布时间】:2015-12-10 20:57:02
【问题描述】:
public static void main(String[] args){
int passed = 0;
int failed = 0;
int N;
int grades;
{       
    while ( passed  + failed ) < N {

    if grades < 6 {
        failed = failed + 1;
    else 
        passed = passed + 1;

    system.out.println passedperc = F/30*100
    system.out.println failedperc = P/30*100

    }
  }
}

【问题讨论】:

  • Java 不是 Python,您必须在 whileifsimilar 语句处将布尔表达式放入括号中。
  • 谢谢,但我是新手,你能用更简单的话解释一下吗?
  • 你写了“if grades “while ((通过 + 失败)
  • 这是我第一次尝试 Java,所以请不要这么粗鲁@shmosel 谢谢
  • @IrisDibra,抱歉,我无法抗拒。但我真的不确定这是否适合提出此类问题。

标签: java syntax error-handling syntax-error


【解决方案1】:

您的代码有一些语法错误...

我修改并注释为评论内容和原因

...
{       // this is not nescesary
        while ( passed  + failed  < N) { // the hole condition mut be between ()
        if (grades < 6) { //same in the if condition
            failed = failed + 1;
        } //need to close the breakets
        else {
         passed = passed + 1;
        }
        System.out.println(passedperc = F/30*100);
        System.out.println(failedperc = P/30*100);  println is a method, so the parameters mus be enclosed in ()
}   // this is not nescesary

【讨论】:

  • system 需要大写为System。 passperc、failedperc、F 和 P 永远不会被声明。
【解决方案2】:

你的语法完全不正确。

这是一个好的开始,尽管 passper、failedperc、F 和 P 甚至都没有被声明过。此外,没有正确遵循变量约定。

public class TestClass {

    public static void main(String[] args) {
        int passed = 0;
        int failed = 0;
        int n = 0; //don't leave uninitialized and uncaptialize
        int grades = 0;  //same here

        while ((passed + failed) < n) { //parens not correct

            if (grades < 6) {//same here and missing brackets
                failed = failed + 1; //can be changed to failed += 1;
            } else
                passed = passed + 1;  //also can be changed using +=

        }
        //System print statements are wrong and variables are never declared or initialized
        System.out.println(passedPerc = f / 30 * 100);//use camelCase don't use capital variables unless they are constants
        System.out.println(failedPerc = p / 30 * 100);//same here

    }
}

【讨论】:

  • 谢谢。这很有帮助。你能帮忙解决这个问题吗:@Michael Queue public static void main(String[] args) { int avg; int N; int grades; int sum = 0; for(int i=0; i &lt; N.length; i++) sum = sum + grades [i]; double avg = sum / N.length; System.out.println("Average value of grades is : " + avg); } }
  • 局部变量未分配默认值。局部变量在使用之前必须通过初始化或赋值显式地赋予一个值,编译器可以使用明确赋值的规则来验证。由于您的变量在您的 main method 内,因此需要为它们指定一个起始值。换句话说,它们需要被初始化,因此,只需将avgNgrades 设置为零。另外,只是一点友好的建议......看看how to ask 否则你可能会在你的问题上被否决。
  • 另外,您将 avg 分配为 int,然后在 for 循环中将其重新分配为 double。最后,您的循环必须重新设计。 N 是 0 所以说 for int i
猜你喜欢
  • 2012-01-28
  • 2019-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多