2014-09-01 103 views
-1

昨天晚上我发布了一个关于在java中使用方法的同时使用方法的问题(netbeans)我仍然非常努力并想知道是否有人可以用我的代码来帮助我? 我们必须制作一个等级计算器,并从用户那里获得他们测试标记的输入,该测试可能的最大标记以及权重。例如。 30/50 * 50%=整体加权标记。我必须使用方法,但我仍然对参数和用户输入部分的位置感到困惑。任何帮助将不胜感激!java中的方法 - 等级计算器

 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; 

    } 

}

+2

这个网站是不是在这里做你的功课。你需要学习和理解你的代码中的重大问题。我真的不知道这个代码是如何运行的?你甚至不知道主要的方法签名是**公共静态无效的主要(字符串[]参数)** – 2014-09-01 23:39:14

+0

我会把扫描仪的主要方法,并得到你的值,而不是调用一个函数传递未定义变量作为参数,这应该不会工作 – Shadow 2014-09-01 23:45:10

回答

2

您需要打破你的代码到逻辑点,例如...

  • 向用户提供输入...
  • 以输入和通它给你的“计算”方法
  • 允许计算方法验证输入...
  • 如果有效,计算最后的比分,如果不是,传回错误代码
  • 重复嘀......

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

首先尝试使每个步骤先行工作,然后再编写下一个步骤。随时准备按照需要重新构建代码......没有任何事情是成立的。

你可能想看看the Getting Started tutorialthe Learning the Java Language tutorial,特别是部分上Classes and ObjectsDefining Methods

+2

+1不用于给出答案 – 2014-09-01 23:41:29

+0

@KickButtowski没有完全重写代码,没有太多可以做的这个问题 – MadProgrammer 2014-09-01 23:44:15

+0

我知道,但我明白,你没有给回答:) – 2014-09-01 23:45:18

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 
} 
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; 

    } 
}