Can someone please help write a method for Java?

2009-08-27 6:41 am
The getNextLuckyNumber() method is used to draw another lucky number and make sure all the lucky numbers generated are unique.
This method is passed a String parameter containing the numbers which have been generated so far (each number is separated by a space.)
This method is to generate a single number between 1 and 100 inclusive, convert the number into a String (with no extra space at both ends) and check if this number is already in the list of numbers passed in as a parameter.
If the number is already in the parameter a new random number should be generated until a number is generated which is not already in the parameter String.


The getLuckyNumbersString() method
obtain a String of four unique Lucky Numbers (numbers between 1 to 100 inclusive).
This method should make fourcalls to the getNextLuckyNumber() method and concatenate each number returned by the getNextLuckyNumber() method.
The String returned by the method should be made up of four unique lucky numbers with a single space between them and no space at each end.

回答 (3)

2009-08-30 7:48 pm
✔ 最佳答案
You should make you getLuckyNumberString method first, then do the other method.

public String getLuckyNumberString()
{
    String luckyNumberString = "";
    for (int i = 0; i < 3; i++)
    {
        luckyNumberString = luckyNumberString + getNextLuckyNumber(luckyNumberString) + " ";
    }
    luckyNumberString = luckyNumberString + getNextLuckyNumber(luckyNumberString);
    return luckyNumberString;
}

Now, for the other method,.
public String getNextLuckyNumber (String luckyNumberString)
or
public int getNextLuckyNumber (String luckyNumberString)

To generate a random number:
Use the java.util.Random class.
Random generator = new Random();

The API for Random is here:
http://java.sun.com/javase/6/docs/api/java/util/Random.html

Now you want to seed the generator so it does not generate the same sequence of numbers.
Date current = new Date();
generator.setSeed( current.getTime() );

The API for Date is here:
http://java.sun.com/javase/6/docs/api/java/util/Date.html

To get a random number from the generator:
int number = generator.nextInt();

To make the number between 0 and 99, use;
number = number % 100;

Then you need to add 1 to it to make it between 1 and 100.

To make the number a string:
String numberAsString = "" + number;

To check if the number is in the lucky number string:
int check = luckyNumberString.indexOf (numberAsString);

If check is -1, the number is not in the string.


So you should be able to put all of that together to make the method.
2009-08-27 6:45 am
We're not going to do your homework for you. Do your own homework.
2016-12-17 9:01 am
a potential can no longer see the interior of sight variables of yet another approach. So, fallingDistance() can no longer see the definitions of (distance) or (test). This code won't deliver collectively. you should declare a double variable named "distance" interior the suggested as approach, however the caller does not be waiting to work out that the two. You bypass information to a potential using arguments (such as you probably did with time), and you come information using the function return value. yet as a manner to return a value, you could say what style of value you're returning interior the tactic announcement. The "void" in "public static void fallingDistance(...)" says there is no return value. Use "public static double ..." particularly to declare that a double value is back. public static double fallingDistance(double time) { return 0.5 * 9.8 * time*time; } Then in significant(), uncomment the scanner enter for time and then use: distance = fallingDistance(time); this is the simplest use of a function...to compute a result. (I corrected the formula for falling distance. this is fairly d = (a million/2)at², with a=9.8 and t² = time*time consequently.) in case you desire that (gravity) variable for use, particularly of 9.8, then you certainly want bypass yet another variable to your function: public static double fallingDistance(double time, double accel) { return 0.5*accel*time*time; } Java helps distinctive variations of a function, provided they have distinctive argument lists. you have got the two variations interior an identical application and use whichever grow to be appropriate. that's suggested as "overloading", and you will see it plenty interior the regularly occurring Java applications. there is no could enter or demonstrate a value in fallingDistance(), via the way. which would be in significant() the place the gap variable is defined. Later, the want arises to have create a potential to instantaneous for an enter, parse it, detect undesirable enter and manage it properly, and get in touch with that from significant(), yet do no longer attempt to place too plenty into one approach. each and each approach could have a distinctive objective.


收錄日期: 2021-05-01 00:58:32
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090826224106AAfr4VG

檢視 Wayback Machine 備份