java级计算器中的方法

时间:2014-09-01 23:33:51

标签: java netbeans methods parameters

我昨晚发布了一个关于在使用java(netbeans)中的方法时制作一个grad计算器的问题我仍然真的很挣扎,并且想知道是否有人可以帮我处理下面的代码? 我们必须制作一个等级计算器,并从用户那里获取他们的测试标记,该测试可能的最大标记以及加权。例如。 30/50 * 50%=总加权分数。我必须使用方法,但我仍然对参数和放置用户输入部分的位置感到困惑。任何帮助将不胜感激!

     import java.util.Scanner;

    public class GradeCalculator {


    public static void main()
{


    System.out.println("Your overall score is: " +CalculateMark(finalMark)); 

}


  public static double CalculateMark (int overallscore) 
{

    Scanner in = new Scanner(System.in);
    int score1 = in.nextInt();
    System.out.print("Enter mark: ");
    if(score1 >=0 || score1<1000){

    } System.out.print("Enter Max mark: ");
     int maxMark = in.nextInt();
     if (maxMark >=0 || maxMark<1000);{

    } System.out.print("Enter weighting as a decimal (eg. 75% = 0.75): ");
        double weighting = in.nextDouble();
         if (weighting <0 && weighting>=10){

            } double finalMark;
             finalMark= (score1/maxMark)*weighting;

  return  finalMark;

  }

}

3 个答案:

答案 0 :(得分:2)

您需要将代码细分为逻辑点,例如......

  • 要求用户输入...
  • 获取该输入并将其传递给您的“计算”方法
  • 允许计算方法验证输入...
  • 如果有效,请计算最终得分,如果没有,则传回错误代码
  • 根据需要重复......

因此,您需要做的第一件事就是从用户那里获得输入。接下来,您需要为该值提供某种验证。接下来,您需要(如果有效),计算得分并将其返回给调用者。

首先尝试使每个步骤先工作,然后再写下一步。准备好按照要求重新构建代码......没有什么是一成不变的。

您可能希望查看the Getting Started tutorialthe Learning the Java Language tutorial,特别是Classes and ObjectsDefining Methods

部分

答案 1 :(得分:0)

首先,您的主要方法签名是错误的,应该是:

public static void main (String[] args) {
    //Code here
}

在Netbeans中,您只需输入psvm并按Tab键,它就会为您填写以上内容。

其次,我不会在你的Calculate方法中得到用户的输入,我会在main中创建3个变量,并在那里获取它们,在获得每个变量后执行验证,然后将3个变量传递给Calculate方法,这看起来像是:

public static double CalculateMark(int mark, int maxMark, double weight) {
    //Code here
}

答案 2 :(得分:0)

我可能没有明白你的计划,但我希望这会有所帮助:

import java.util.Scanner;

public class GradCalc {
private double finalMark = 0; //I would make this an instance variable
private static double maxMark = 0; //these need to be double if you want decimal answers
private static double score1 = 0;


public static void main(String[] args) // you need the String[] args for main to run
{


    System.out.println("Your overall score is: " +String.format("%.2f", CalculateMark())); 
    //String.format was to make it 2 decimal place lengths

}


  public static double CalculateMark ()// remove parameters to alter instance var
{

    Scanner in = new Scanner(System.in);
    System.out.print("Enter Max mark: "); //call this first
    maxMark = in.nextInt();
    System.out.print("Enter mark: "); //I switched these next two lines
    score1 = in.nextInt();
    while(true){ // this makes sure the user won't give a neg answer
                // or one that is over the max Mark (you can change this if you want)
        if(score1 >=0 && score1<=maxMark){
            //nothing was in this if statement
            break;
        }
        System.out.print("Enter mark again. The latter was not applicable: ");
        score1 = in.nextInt();
    } 
    System.out.print("Enter weighting as a decimal (eg. 75% = 0.75): ");
    double weighting = in.nextDouble();
    while (true){
        if (weighting > 0 && weighting<=1){ //you probably had the conditional signs mixed up
        //nothing was in this if statement either
        break;
        } 
        System.out.print("Weighting decimal was not between 0 and 1. Please enter again: ");
        weighting = in.nextDouble();
    }
    double finalMark;
    finalMark= (score1/maxMark)*weighting;

  return  finalMark;

  }
}