简体   繁体   中英

I'm a complete beginner at java and have entangled my methods

I'm making a horse-race themed program for my mom that compares the money taken in by her employees and ties it to a horse. I've created two methods which entirely rely on each other and have no idea how to call them into my main method. I, of course, also need to add a graphical element to this at some point, and figuring out how to make the program work with decimal integers would also be ideal. My main issue right now is I need to know how to call print3largest and inputs in my main method, or how to generally make this not a dumpster fire and maybe reduce it to less than 3 methods that aren't entangled like this.

I've searched through repository websites for hours now looking for a solution, but as I have no professional experience in any kind of programming I severely lack the terminology to find an answer, assuming anyone else is stupid enough to run into the same problem I have. I'm extremely limited in my programming knowledge, with java being the only thing I've ever messed with thanks to a course in high school. Sadly, that hardly helps as it was almost entirely through an interface that was essentially just scratch.

import java.util.Scanner;

class HorseComparison
{   
    public static void main(String[] args)
    {
        //no clue how to call print3largest or inputs here without ruining everything
    }
    static void print3largest(int arr[], int arr_size, String firsthorse, String secondhorse, String thirdhorse) 
    { 
        int i, first, second, third; 
        if (arr_size < 3) 
        { 
            System.out.print(" Invalid Input "); 
            return; 
        } 

        third = first = second = Integer.MIN_VALUE; 
        for (i = 0; i < arr_size ; i ++) 
        { 
            if (arr[i] > first) 
            { 
                third = second; 
                second = first; 
                first = arr[i]; 
            } 
            else if (arr[i] > second) 
            { 
                third = second; 
                second = arr[i]; 
            } 

            else if (arr[i] > third) 
                third = arr[i]; 
        } 
        inputs(first, second, third);
        System.out.println("The horse in the lead is " + firsthorse + " with " +
                       first + " dollars.");
        System.out.println("The runner up is " + secondhorse + " with " +
                second + " dollars.");
        System.out.println("Third place is " + thirdhorse + " with " +
                third + " dollars.");
    } 

    static void inputs(int first, int second, int third) 
    { 
        Scanner sc = new Scanner(System.in);
        int size;
        System.out.println("How many horses are competing?");
        size = sc.nextInt();
        int[] arr = new int[size];
        System.out.println("Enter the amount of money taken in by each horse (rounded to the nearest dollar and separated by spaces)");
        //For reading the element
        for(int i=0;i<size;i++) {
            arr[i] = sc.nextInt();
        int n = arr.length; 
        String firsthorse;
        String secondhorse;
        String thirdhorse;
            System.out.println("Which horse has taken in "+ first +"?");
            firsthorse = sc.toString();
            System.out.println("Which horse has taken in "+ second +"?");
            secondhorse = sc.toString();
            System.out.println("Which horse has taken in "+ third +"?");
            thirdhorse = sc.toString();
        print3largest(arr, n, firsthorse, secondhorse, thirdhorse); 
    } 
  }
} 

I want it to display the 3 highest amounts along with the input name of the horse tied to those amounts.

you can call static methods directly by the method name

 print3largest() 

or you can use the classname before method name example

HorseComparision.print3largest() ```

Since both methods are static and so the Main method. Static methods can be called

  1. Directly with method name, if you are calling from inside the class. Eg : print3largest(.. args), inputs(.. args)
  2. call using ClassName.Method name. This can be used if you are calling method from outside or inside the class. Eg: HorseComparison.print3largest(.. args), HorseComparison.inputs(.. args)

I don't feel like there really is enough information about what the program is intended to do for a clear, direct answer to be provided, but I will do my best.

First, what I would suggest, is you take a good look at this program and determine how you can separate out each responsibility. For example, do you really need to call inputs from print3largest , or could you possibly call this directly from your main?

Once you have established the intent of each function, consider making each function return a result. Generally speaking, you want parameters to be immutable . Learning functional programming habits now will help you down the road.

Here is what I would do:

  1. Copy this file to a backup file.
  2. Start a new Java project, create your class.
  3. Write all of your display code. That is, develop the initial user experience. What inputs do you want to ask from the user? Capture those inputs.
  4. Given those inputs, write your core algorithm, which currently appears to be primarily in print3largest . Return those results back to the caller.
  5. Display your results back to the end user.

This might result in more functions, but that isn't a bad thing. I would also advise that you consider creating a separate class to hold some of this logic. This will give you an opportunity to learn about objects and separation of concerns .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM