java中的方法(等级计算器)

我们一直在学习java中的方法(使用netbeans),我仍然对使用方法感到困惑。 一个功课问题基本上要求使用方法设计一个等级计算器,方法是提示用户标记 ,可能的最大标记 ,该测试的权重 ,然后为该测试产生最终得分。 例如。 (35/50)* 75%=总分

但是,我正在努力使用方法,我想知道是否有人可以指出我正确的方向,为什么我的代码下面有一些错误,并没有运行? 我不想要任何完整的答案,因为我想尝试尽我所能,而不是抄袭。 任何帮助将不胜感激 :)! (也请你好,因为我是编程的新手,我不是很好)谢谢!

import java.util.Scanner; public class gradeCalc { public static void main(String[] args) { Scanner in = new Scanner(System.in); scoreCalc(); System.out.print("Your score is" + scoreCalc()); } public static double scoreCalc (int score1, int maxMark, double weighting, double finalScore) { Scanner in = new Scanner(System.in); System.out.print("Enter mark"); in.hasNextInt(); score1 = in.nextInt(); System.out.print("Enter Max mark"); in.hasNextInt(); maxMark = in.nextInt(); System.out.print("Enter weighting as a decimal (eg. 75% = 0.75)"); in.hasNextInt(); weighting = in.nextInt(); finalScore = (score1/maxMark)* weighting; return finalScore; } } 

您正在调用方法scoreCalc()而不传递您定义的参数。 当你调用它时,它被定义为有3个参数。

 scoreCalc(7, 10, 3.0, 8.0); 

此外,在创建类时,请使用大写GradeCalc启动它

正如您所看到的, scoreCalc方法需要一组参数,但您可以在没有参数的情况下调用它。

第二种: Scanner in = new Scanner(System.in);没有必要Scanner in = new Scanner(System.in); 进入你的main(String[] args)方法。 您将其称为scoreCalc方法。

第三:你两次打电话给scoreCalc 。 第一个调用在System.out.println之前,第二个调用在System.out.println之前。 您的应用程序将询问用户两次输入值。

将结果值存储在变量中并稍后显示:

 double result = scoreCalc(.... required params .....); System.out.println("Your score is: " + result); 

开始 :

1)遵循编码惯例。 (class级名称应以大写字母开头)。

2)在您的上下文中,您不需要Scanner in = new Scanner(System.in);main()因为你没有使用它。

3)你是一个没有参数的方法scoreCalc() 。 然而,需要使用参数调用该方法。

4)一种方法,是一个模块。 它作为增加可重用性的代码块。 所以我建议在main()方法中接受来自user的值,然后将它们传递给方法进行计算。

我想起了几件事:

  1. 你执行scoreCalc()两次。 可能你想执行一次并将结果保存在double变量中: double score = scoreCalc()

  2. 说到scoreCalc() :您的定义需要4个参数,而您没有该参数作为该方法的输入。 您应该从方法定义中删除它们,而是在方法体中添加score1maxMarkweightingfinalScore变量声明。

  3. 在main函数中,声明并实例化一个不使用的Scanner对象。

  4. 注意混合intdouble算法。

要指出的一些错误/错误是: –

1)你不需要这个语句Scanner in = new Scanner(System.in); 在你的main() ,因为你没有通过该函数从用户那里获取输入。

2)你的函数scoreCalc (int score1, int maxMark, double weighting, double finalScore)接受参数,例如它的调用应该看起来像scoreCalc(15, 50, 1.5, 2.7) ,但是你把它称为scoreCalc() ,那个没有来自main()参数。

编辑: – 您的程序中还有一个严重的缺陷,可能有用,但编码不好。 我不会为它提供代码,并会将实现留给您。 从main()中的用户输入(使用scanner ),将结果分配给临时变量,然后将该变量作为参数传递给函数scoreCalc()

 //pseudocode in main() Scanner in = new Scanner(System.in); int score= in.nextInt(); . . . scoreCalc(score,...); 

或者你可以在没有参数的情况下制作你的scoreCalc函数,并在其中输入用户输入(如present),最后只将结果返回到main()

这两种方法看起来都合适,您可以自由选择:)

与其他答案相反,我将从另一件事开始。

你忘记了最重要的方法 – 那就是Constructor

您必须创建一个成绩计算器,因此您创建一个表示成绩计算器对象的类(类型)。 使用Java约定,此类应命名为GradeCalculator ,不要使用Calc类的缩写,以使名称GradeCalculator

所以回到constructor – 你还没有创建你的Calculator对象。 可能不需要它,你可能无法实现你的目标,但这不是一个好习惯。

因此,也可以使用此方法 – 这样,您将创建实际的Calculator对象。

它可以这样实现:

 public static void main(String[] args) { GradeCalculator myCalculator = new GradeCalculator(); } 

而现在你可以想象你的计算器就在你面前。 你可以用它吗? 获得一个标记将是一个良好的开端 – 所以,你可以做的是:

 myCalculator.getMark() 

现在你必须定义一个方法getMark()

 private void getMark() { } 

您将在其中提示用户输入。 你也可以这样做:

 myCalculator.getMaxMark() { } 

并且那样获得最大标记(在定义方法之后)。

同样可以调用方法myCalculator.getWeighting()myCalculator.calculateFinalResult()myCalculator.printResult()

这样你就可以拥有以下mehtods(它可以做的事情)的实际对象:

 public GradeCalculator() { } //constructor private void getMark() { } //prompts user for mark private void getMaxMark() { } //prompts user for max mark private void getWeighting() { } //prompts user for weighting factor private void calculateFinalResult() // calculates the final result private void printResult() // prints the result. 

我打算用方法创建一个计算器 – 而且我会高度评价。 尝试将您正在创建的类视为真实对象,并创建表示对象实际行为的方法。 你越早开始这样做对你越好。 在一种方法中编写整个代码并不是一种好的做法,在更大的应用程序中可能会导致各种问题。 因此,即使在进行小型项目时,也要尝试使用最佳实践来实现它们,这样就不会产生糟糕的习惯。

编辑:这样你的整个程序看起来像这样:

 import java.util.Scanner; public class GradeCalculator { //here you define instance fields. Those will be visible in all of your classes methods. private Scanner userInput; //this is the userInput the calculator keypad if you will. private int mark; //this is the mark the user will enter. private int maxMark; //this is the mark the user will enter. private int weightingFactor; //this is the weighting factor the user will enter. private int result; //this is the result that will be calculated. public static void main(final String args[]) { Scanner userInput = new Scanner(System.in); //create the input(keypad). GradeCalculator calculator = new GradeCalculator(userInput); //create the calculator providing it with an input(keypad) calculator.getMark(); calculator.getMaxMark(); calculator.getWeightingFactor(); calculator.printResult(); } private GradeCalculator(final Scanner userInput) { this.userInput = userInput; //from now the provided userInput will be this calculators userInput. 'this' means that it's this specific calculators field (defined above). Some other calculator may have some other input. } private void getMark() { } //here some work for you to do. private void getMaxMark() { } //here some work for you to do. private void getWeightingFactor() { } //here some work for you to do. private void printResult() { } //here some work for you to do. } 

请注意,在构造Calculator对象之后,您不必使用静态方法。