Yahoo Clever wird am 4. Mai 2021 (Eastern Time, Zeitzone US-Ostküste) eingestellt. Ab dem 20. April 2021 (Eastern Time) ist die Website von Yahoo Clever nur noch im reinen Lesemodus verfügbar. Andere Yahoo Produkte oder Dienste oder Ihr Yahoo Account sind von diesen Änderungen nicht betroffen. Auf dieser Hilfeseite finden Sie weitere Informationen zur Einstellung von Yahoo Clever und dazu, wie Sie Ihre Daten herunterladen.

java programming?

public static int getDescription(int i)

{

return description(i);

}

is this even how I would write it? im not even sure anymore

2 Antworten

Relevanz
  • vor 5 Jahren
    Beste Antwort

    Hey, Michael. It looks like you are trying to return an integer using a method; I will try to explain how you can do this, and why it works.

    You would probably want to write this as such:

    public static int getDescription(int i){

    return i+10;

    }

    If you were to type, System.out.println(getDescription(10)) into the main method, it would print out the number 20.

    You first start off by declaring the method as "public;" this, essentially, tells the program to allow you to use the method at any time within the same class. Next, you tell the program that this method will return an integer, which is just a regular whole number. After this, you name the method. The name can be anything you'd like, in this case, we named it "getDescription," but you could have named it literally anything and it would still have worked just the same.

    Next, in parenthesis, you created a "parameter." Parameters allow you to send information to the method whenever you use it. In this case, we told it that we are going to be passing in an integer, then we named the integer "i". The name of the integer is arbitrary, we could have named it literally anything, but "i" works just fine. Finally, we told the method what we want it to tell us, or "return" to us whenever we use it. In this case, I chose to have it return the number you send to it "i" + 10.

    So when we finally use the method it will do what we told it to do, which is to return to us the number we give it plus ten. So, if we were to type System.out.println(getDescription(20)) into the main method, it would print out the number "30," since 30 is the number we gave it (20) plus 10.

    Hopefully that cleared it a bit for you!

  • ?
    Lv 7
    vor 5 Jahren

    Almost certainly that's wrong. description(i); looks like a function call, and I doubt you're supposed to have a function just called description. And if a function called getDescription is returning an int, something is wrong with how you're naming functions. It should almost certainly be returning a String.

    Your assignment (which you've posted a bit more of in other questions, but not the whole thing, at least that I've seen) frankly doesn't make a lot of sense to me. description was a String field, right? Then why are you supposed to be passing an int in? Is there a list of descriptions somewhere? Are they supposed to be in an array? If description is actually supposed to be an array of Strings, then

    return description[i];

    would make a lot of sense.

    Perhaps you should ask someone in your class for help. They would at least know the whole assignment.

Haben Sie noch Fragen? Jetzt beantworten lassen.