Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的代码哪里出错了?_Java_Methods_Main_Statements - Fatal编程技术网

Java 我的代码哪里出错了?

Java 我的代码哪里出错了?,java,methods,main,statements,Java,Methods,Main,Statements,编辑: 我想我复制的代码有点错误 public class GradeCalculator { public static void calculateGrade(int mark) { if (mark >= 70) System.out.println("grade = A"); if (mark >= 60) System.out.println("grade = B"); if (mark >= 50) System.out.println("grade

编辑:

我想我复制的代码有点错误

public class GradeCalculator {

 public static void calculateGrade(int mark) {
  if (mark >= 70) System.out.println("grade = A");
  if (mark >= 60) System.out.println("grade = B");
  if (mark >= 50) System.out.println("grade = C");
  if (mark >= 40) System.out.println("grade = D");
  if (mark <  40) System.out.println("grade = F");
 }

 public static void main(String[] args) {

 }
}
公共类等级计算器{
公共静态无效计算等级(整数标记){
如果(标记>=70)系统输出打印项次(“等级=A”);
如果(标记>=60)系统输出打印项次(“B级”);
如果(标记>=50)系统输出打印项次(“等级=C”);
如果(标记>=40)系统输出打印项次(“等级=D”);
如果(标记<40)系统输出打印项次(“等级=F”);
}
公共静态void main(字符串[]args){
}
}
我才刚刚开始尝试Java(或任何编程)。我正在练习问题。我有这段代码,需要在main方法中编写语句来测试它

我该怎么做

感谢您的帮助/正确方向的指点


Mark

您需要从
main
调用该方法。我强烈建议您仔细阅读,您缺少一个非常基本的概念

一旦你这样做了,你需要有
else
。在你的情况下,如果第一个
if
得到满足,那么下面的
if
将得到满足


想想看,
if(mark>=70)
那么肯定
if(mark>=x)
对于任何
x好吧,为了开始这场闹剧,让我们把你的代码编辑成一种能够以一种感性的方式编译和工作的东西:

public class GradeCalculator {

    public static void claculateGrade (int mark) {
        if (mark >= 70) System.out.println("grade = A");
        else if (mark >= 60) System.out.println("grade = B");
        else if (mark >= 50) System.out.println("grade = C");
        else if (mark >= 50) System.out.println("grade = D");
        else if (mark >= 40) System.out.println("grade = E");    
    }

    public static void main (String[] args)  {

    }

}
现在,要打印一些人会得到的分数,你可以这样称呼你的“claculateGrade”方法:

public static void main (String[] args)  {
    claculateGrade(55);
}
这将在控制台上打印“grade=C”。完整代码:

public class GradeCalculator {

    public static void claculateGrade (int mark) {
        if (mark >= 70) System.out.println("grade = A");
        else if (mark >= 60) System.out.println("grade = B");
        else if (mark >= 50) System.out.println("grade = C");
        else if (mark >= 50) System.out.println("grade = D");
        else if (mark >= 40) System.out.println("grade = E");    
    }

    public static void main (String[] args)  {
        claculateGrade(55);
    }

}
现在,我们可以进一步改进这一点。让我们先让该方法返回等级:

public static char getGrade (int mark) {
    if (mark >= 70) return 'A';
    else if (mark >= 60) return 'B';
    else if (mark >= 50) return 'C';
    else if (mark >= 50) return 'D';
    else if (mark >= 40) return 'E';

    /* if below 40, return "fail" */
    return 'F';
}
这会将您的代码更改为:

public class GradeCalculator {

    public static char getGrade (int mark) {
        if (mark >= 70) return 'A';
        else if (mark >= 60) return 'B';
        else if (mark >= 50) return 'C';
        else if (mark >= 50) return 'D';
        else if (mark >= 40) return 'E';

        /* if below 40, return "fail" */
        return 'F';
    }

    public static void main (String[] args)  {
        System.out.println("Grade: " + getGrade(55));
    }

}

你计算的成绩应该返回成绩,而不是打印。如果你想打印它,调用者可以打印成绩

public static char calculateGrade(int mark) {
    return "UUUUEDCBAAAA".charAt(mark/10);
}

System.out.println("Grade = " + calculateGrade(65));
代码的工作方式是,它利用了每个区域都是10的倍数这一事实。也就是说,当你除以10,你就得到了

100 -> 10
90 - 99 -> 9
80 - 89 -> 8
70 - 79 -> 7
60 - 69 -> 6
50 - 59 -> 5
40 - 49 -> 4
30 - 39 -> 3
20 - 29 -> 2
10 - 19 -> 1
0 - 9 -> 0
所以现在所有的数字都变成了0到10的区域。但是我们想要分数作为一个字母,这样你就得到了

100 -> A
90 - 99 -> A
80 - 89 -> A
70 - 79 -> A
60 - 69 -> B
50 - 59 -> C
40 - 49 -> D
30 - 39 -> E
20 - 29 -> U
10 - 19 -> U
0 - 9 -> U

为了使代码正常工作,您需要调用您的方法,因为您的所有编码都是在该部分中完成的,即根据条件打印

public class GradeCalculator { // This is your class

     public static void calculateGrade(int mark) { 
  //This is the method containing conditions
    on the basis of these conditions the corroesponding
   sentences will be  printed
      if (mark >= 70) System.out.println("grade = A");
      if (mark >= 60) System.out.println("grade = B");
      if (mark >= 50) System.out.println("grade = C");
      if (mark >= 40) System.out.println("grade = D");
      if (mark <  40) System.out.println("grade = F");
     }
    //now u need to call the method from your main method.. main method is the entry point of your program so it will start from main then check your method call and do as per the code written there
     public static void main(String[] args) {
    // to call your method just right your method name and enter marks

    calculateGrade(50); // you can enter any integer 
     }
    }
public class GradeCalculator{//这是你的班级
公共静态无效计算等级(整数标记){
//这是包含条件的方法
根据这些条件,相应的腐蚀
句子将被打印出来
如果(标记>=70)系统输出打印项次(“等级=A”);
如果(标记>=60)系统输出打印项次(“B级”);
如果(标记>=50)系统输出打印项次(“等级=C”);
如果(标记>=40)系统输出打印项次(“等级=D”);
如果(标记<40)系统输出打印项次(“等级=F”);
}
//现在你需要从main方法调用这个方法。main方法是你程序的入口点,所以它将从main开始,然后检查你的方法调用,并按照那里写的代码执行
公共静态void main(字符串[]args){
//要调用您的方法,请选择正确的方法名称并输入标记
calculateGrade(50);//您可以输入任何整数
}
}

你的代码无法编译,伙计。如果你的代码中有一个bug,你应该做的第一件事就是使用调试器来找出你的程序为什么要这样做。我还建议你在IDE中使用重新格式化。顺便说一句,你可以将上面的代码转换成一行代码。很难看到代码是什么doing@OlaviMustanoja我已经更新了我的a答案。是除以10不清楚,还是在字符串中查找字母不清楚?对不起,我的评论有点模糊。我的意思是,对于一个明显的初学者来说,这段代码可能有点难以理解。现在你已经解释过了,这很好。我认为在编辑之前我没有看到答案。现在,所有的回复都有意义(对于一个明显的初学者:))谢谢you@OlaviMustanoja我希望初学者能够了解它的功能,尽管我不希望初学者想象这样编写代码所以问题是elseif(一旦我发布了我在原始问题中想要发布的代码!)谢谢你的回复。