2014-09-01 105 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; 

    } 
}