Hello, I am having problems trying to figure out how to modify a program for a l
ID: 3793925 • Letter: H
Question
Hello,
I am having problems trying to figure out how to modify a program for a lab in Java. I have to first define a void return type method named guessingGame() which the method has to be outside of the main() method but within the class definition. Then I have to declare local variables which are:
int x = 0,guess = 0, response = 1;
How to I write the void return type? Please and Thank you
Here is the code I have to work with:
// import a class for random numbers
importjava.util.Random;
publicclassGuessGame
{
// method to generate a random number
publicstaticintRandomNumber()
{
// local variable to hold a random number
intrNum = 0;
// a random object is created
Random rand = newRandom();
// a random number is generated
rNum = rand.nextInt(20) + 1;
// random number is returned to main()
returnrNum;
}
// the main() method
publicstaticvoid main(String args[])
{
// local variable to hold a random integer
int x = 0;
// local variable is assigned a random number
x = RandomNumber();
//display the random number
System.out.println(x);
}
}
// import a class for random numbers
importjava.util.Random;
publicclassGuessGame
{
// method to generate a random number
publicstaticintRandomNumber()
{
// local variable to hold a random number
intrNum = 0;
// a random object is created
Random rand = newRandom();
// a random number is generated
rNum = rand.nextInt(20) + 1;
// random number is returned to main()
returnrNum;
}
// the main() method
publicstaticvoid main(String args[])
{
// local variable to hold a random integer
int x = 0;
// local variable is assigned a random number
x = RandomNumber();
//display the random number
System.out.println(x);
}
}
Explanation / Answer
Please find the program that is error free as follows:
package test;
import java.util.Scanner;
//import a class for random numbers
import java.util.Random;
public class test1
{
// method to generate a random number
public static int RandomNumber()
{
// local variable to hold a random number
int rNum = 0;
// a random object is created
Random rand = new Random();
// a random number is generated
rNum = rand.nextInt(20) + 1;
// random number is returned to main()
return rNum;
}
// the main() method
public static void main(String args[])
{
// local variable to hold a random integer
int x = 0;
// local variable is assigned a random number
x = RandomNumber();
//display the random number
System.out.println(x);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.