2016-10-01 25 views
-2

在我的java任務中,我創建了一個程序來執行應該發生的事情,但是我的教授的反饋是:此程序的主要任務是讓您學習創建方法;但是,您並未在程序中創建任何方法。我該如何添加方法?在java中添加方法(jGrasp)

/* Daniel Martos 
    This program will make use of methods with parameters and return values, 
    the scanner class, string methods, and nested for Loops. It will ask the 
    user for their full name and the number of credits that they are taking. 
    It will then calculate the cost of their tuition and print out the user's 
    full name, and tuition cost. 
*/ 

// Import the java.util package in order to work with the scanner class. 
import java.util.*; 

public class Martos3 { 
// Create constants. 
    public static final double PER_CREDIT = 302.00; 

    public static void main (String [] args) { 
// Create variables and objects. 
    int credTaken; 
    double cost; 
    String fullName; 

// Create the console object to allow us to read information in from the keyboard. 
    Scanner console = new Scanner (System.in); 

// Create executable code. 
    System.out.println ("Welcome to the Cal U Tuition Calculator Program"); 

    System.out.print ("Please enter your full name: "); 
    fullName = console.nextLine(); 

    System.out.print ("Please enter the number of credits taken: "); 
    credTaken = console.nextInt(); 

// Add a Nested for Loop to add the dashes 
    for (int i = 1; i <= 3; i++) { 
     for (int j = 1; j <= 50; j++) { 
     System.out.print ("-"); 
     } 
     System.out.println(); 
    } 

// Calculate the tuition cost. 
    cost = (credTaken * PER_CREDIT); 

// Print out "Tuition calucation for" and the user's full name followed by a . 
    System.out.println ("Tuition calulation for " + fullName + "."); 
// Print out "Credits Taken =" with the number of credits that the user enters. 
    System.out.println ("Credits Taken = " + credTaken); 
// Print out "Tuition Cost = $" and the cost of tutition calculation. 
    System.out.println ("Tuition Cost = $" + cost); 

    } 
} 
+0

您是否試圖獲取信息,究竟是什麼方法? –

+0

第一種方法應該用於詢問學生的姓名,第二種方法應該是詢問學生有多少學分,第三種方法是計算學費。我通過電子郵件發送了我的教授(首選聯繫方式)併發布到我的課堂討論版上,我沒有收到任何反饋。我很絕望,因爲今晚會到期。 –

回答

0

看看下面的一段代碼,修改代碼

displayResults是用於顯示結果的方法。 calculateFees是用於計算的另一種方法。現在剩下的部分取決於你,即你的要求是怎樣的?我用這種方式給了你示例代碼,你可以實現其他的東西。

// Create constants. 
public static final double PER_CREDIT = 302.00; 

public static void main(String[] args) throws IOException { 
    // Create variables and objects. 
    int credTaken; 
    String fullName; 

    // Create the console object to allow us to read information in from the 
    // keyboard. 
    Scanner console = new Scanner(System.in); 

    // Create executable code. 
    System.out.println("Welcome to the Cal U Tuition Calculator Program"); 

    System.out.print("Please enter your full name: "); 
    fullName = console.nextLine(); 

    System.out.print("Please enter the number of credits taken: "); 
    credTaken = console.nextInt(); 
    displayResults(credTaken,fullName); 

} 

static void displayResults(int credTaken, String fullName) { 
    // Add a Nested for Loop to add the dashes 
    for (int i = 1; i <= 3; i++) { 
     for (int j = 1; j <= 50; j++) { 
      System.out.print("-"); 
     } 
     System.out.println(); 
    } 
    double cost = calculateFees(credTaken); 
    // Print out "Tuition calucation for" and the user's full name followed 
    // by a . 
    System.out.println("Tuition calulation for " + fullName + "."); 
    // Print out "Credits Taken =" with the number of credits that the user 
    // enters. 
    System.out.println("Credits Taken = " + credTaken); 
    // Print out "Tuition Cost = $" and the cost of tutition calculation. 
    System.out.println("Tuition Cost = $" + cost); 

} 

static double calculateFees(int credTaken) { 
    // Calculate the tuition cost. 
    return (credTaken * PER_CREDIT); 
} 
+0

非常感謝您......我希望我可以用它作爲我當前任務的很好參考。 –