Reputation: 490
It's my first question here so please be patient :-) I try to code very simple Java game. First I want to generate a sample task like "2+2 = ". Than use user must answer what is the result. Next I want to generate a char and other random number.
2 +2 = (4) if answer == result, than generate random char nad number 4 - 1 = (3) if answer == result, than generate random char nad number 3 + 5 = (9)
Currently I am stuck with the character generation.
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random randomNumber = new Random();
int userRandomNumber = randomNumber.nextInt(10) - 1;
int myRandomNumber = randomNumber.nextInt(10) - 1;
int i = giveResult(userRandomNumber, myRandomNumber);
System.out.println(userRandomNumber);
System.out.println(myRandomNumber);
System.out.println(i);
}
public static int giveResult(int userRandomNumber, int myRandomNumber) {
Random randomNumber2 = new Random();
int switchRandomNumber = randomNumber2.nextInt(2);
char operatorRandom = '+';
int result = 0;
switch (switchRandomNumber) {
case 1:
operatorRandom = '+';
result = userRandomNumber + myRandomNumber;
//return result;
break;
case 2:
operatorRandom = '-';
result = userRandomNumber - myRandomNumber;
//return result;
break;
default:
result = 0;
}
return result;
}
}
How to correct my code and what next steps do?
Regards!
Upvotes: 1
Views: 1539
Reputation:
You have made this program more complex than it really needs to be.
The simplest way to achieve what you are aiming for(or at least what I think you are aiming for) is like this:
public static void main(String[] args) {
Random rand = new Random();
int NumberOfAnswers = addOrSubtract.length;
int pick = rand.nextInt(NumberOfAnswers);
choice = addOrSubtract[pick];
int randomNumber1 = rand.nextInt(10);
int randomNumber2 = rand.nextInt(10);
if (choice == "+") {
answer = randomNumber1 + randomNumber2;
} else {
answer = randomNumber1 - randomNumber2;
}
System.out.println(randomNumber1 + " " + choice + " " + randomNumber2 + " = ");
Scanner input = new Scanner(System.in);
int inputAnswer = input.nextInt();
if (inputAnswer == answer) {
System.out.println("Correct! The answer was: " + answer);
main(null);
} else {
System.out.println("Incorrect. The answer was: " + answer);
System.out.println("Good-Bye!");
System.exit(0);
}
input.close();
}
Note: I have pretty much completely re-done your program, so if you have any questions about what anything does, please comment below.
Upvotes: 1
Reputation: 636
The first thing to understand about Random is it is a Class. So I see by your naming convention you might be misunderstanding what a class is.
Consider Random like a factory that makes things, so when you call .next() on it, it pops out a number for you. So it is in itself not a number, its a number maker
Next up, you can actually use the same Random class in both of your functions by just declaring it outside of them and initializing it at the start.
So you can do something like this to start:
public class Main {
private Random rnd;
public static void main(String[] args) {
rnd = new Random();
The '= new Random()' part of the code is called initializing. When you first declare the Class "Private Random rnd;" It's like you have the factory, but its actually an empty factory at the moment without any parts in it yet.
Initiliazing a class is like building the factory, getting all its parts, turning it on, and basically setting it up to start doing stuff
If you try calling .next() on an uninitialized Random class, you'll get an error!
Ok so now Random is working, how can we use it? Well lets take a look at Microsoft's documentation on Random: https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx
Oh cool, you can actually see there are several versions of next.
You can do .next(), or you can do .next(number) and you can do .next(number, number)
They even show you what the different versions do.
Next up, you can use string interpolation (fancy words for sticking variables in string) for printing, via string.Format()
Example:
Var myString = string.Format("{0}{1}{2}={3}", "1", "+", "2", "3")
This will give you the string "1+2=3"
The next trick you'll want to know is converting.
You can call .toString() on most objects, including integers. It should also be noted that string.Format will call toString() on everything you pass into it automatically, no need to .toString() it first!
So you actually can achieve the same results above with this code.
Var myString = string.Format("{0}{1}{2}={3}", 1, '+', 2, 3)
But if you want the string you can just call:
var myString = myNumber.toString()
Let me know if that works for you!
Upvotes: 0