Very new to Java and I am looking to change the following to allow the same variable call to occur only once.
The second "b" variable is only called during rectangle and triangle. This does work just want to see if I can get that one extra line out of the main.
The "b" variable can not be moved out from the if statements as the program will not start as the user will only enter the a variable.
import java.util.*;
public class Main {
public static void main(String[]args) {
Scanner in = new Scanner(System.in);
System.out.print("? ");
String word = in.next();
Shape s = null;
while (!word.equals("quit")) {
double a = in.nextDouble();
if (word.equals("triangle")){
double b = in.nextDouble();
s = new Shapet (a, b);
}else if (word.equals("rectangle")){
double b = in.nextDouble();
s = new Shaper (a, b);
}else if (word.equals("square")){
s = new Shapes (a);
}else if (word.equals("circle")){
s = new Shapec (a);
}else if (word.equals("pentagon")){
s = new Shapep (a);
}
System.out.printf("Area of %s = %.2f\n", s, s.area());
System.out.print("? ");
word = in.next();
}
}
}
You could use switch which will not make the code shorter but more readable:
String word = in.next();
Shape s = null;
while (!word.equals("quit")) {
double a = in.nextDouble();
switch(word) {
case "triangle":
s = new Shapet (a, in.nextDouble());
break;
case "rectangle":
s = new Shaper (a, in.nextDouble());
break;
case "square":
s = new Shapes (a);
break;
case "circle":
s = new Shapec (a);
break;
case "pentagon":
s = new Shapep (a);
break;
}
word = in.next();
}
If you simply want to reduce lines of code, possibly you can go for inline code:
double a;
while (!word.equals("quit")) {
a = in.nextDouble();
if (word.equals("triangle")) {
s = new Shapet(a, in.nextDouble());
} else if (word.equals("rectangle")) {
s = new Shaper(a, in.nextDouble());
} else if (word.equals("square")) {
s = new Shapes(a);
}
// ...
}
P.S. Avoid declaring variables inside loops
If I where you I would leave the code as is and simply change the variable names to something which is more appropriate.
I would let each chunk within your if statements handle their own variables. This would make the code easier to read.
Something like so:
while (!word.equals("quit")) {
if (word.equals("triangle")){
double base = in.nextDouble(); //You could maybe print out 'Enter base length, or something like that, same goes for the one below.
double height = in.nextDouble();
s = new Shapet (base, height);
}
In this case I am assuming that you want to calculate the area of some shape and maybe render it. If this is not the case, then maybe renaming Shapet to something which gives more information on the purpose of the class might also help.
Keep in mind that although you should not write bloated code, readability is key. Thus, if you need to take an extra couple of lines to make your code easier to follow, you should, in most cases, take that path.
I think the variables are inside the if and they are all local. So you can take them out of the if. Because you are studying Java, I'd like to recommend you to change the class name to make it more Object Oriented. You also can use enum for the ShapeType. I think it's a bit far your question but I hope this help you to have a better view (Pls also change the variable "a", "b" to the meaningful one. I still keep it in my sample).
enum ShapeType {
triangle("triangle"), rectangle("rectangle"), square("square"), circle("circle"), pentagon("pentagon");
private String value;
ShapeType(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
}
Scanner in = new Scanner(System.in);
String word = in.next();
//check the type is ok or not
ShapeType shapeType = ShapeType.valueOf(word);
String quit = "quit";
Shape s = null;
double a = in.nextDouble();
double b = in.nextDouble();
while (!word.equals(quit)) {
switch(shapeType) {
case triangle:
s = new Triangle(a, b);
break;
case rectangle:
s = new Ractangle (a, b);
break;
case square:
s = new Square (a);
break;
case circle:
s = new Circle (a);
break;
case pentagon:
s = new Pentagon (a);
break;
default:
System.out.println("Invalid shape! Pls retry");
}
}
Related
I don't know how to phrase that question without it sounding confusing but it's essentially this:
TL;DR:
I have been trying to call circleChoice() located in the subclass Circle from a switch statement in inputProcess() located in the main class Shapes. I don't know how, but i get a java.lang.StackOverflowError on the Console. I might have screwed up the logic. I could fix this easily if it wasn't a school project that required the usage of subclasses.
package shapes;
import java.lang.Math.*;
import java.util.Scanner;
public class Shapes {
//CONSTRUCTORS
//choosing
static Scanner sc = new Scanner(System.in);
//inputs
protected String radDiameter;
protected int choice;
protected double a;
protected double b; //b1
protected double d; //b2
protected double c;
protected double h;
protected double radius;
//outputs
protected double perimeter;
protected double area;
//CONSTUCTORS
public class Circle extends Shapes{ //error
public void circleChoice() {
System.out.println("Pick your poison: ");
System.out.println("[P] Perimeter");
System.out.println("[A] Area");
radDiameter = sc.next();
this.radDiameter.toUpperCase();
switch (radDiameter) {
case "P":
System.out.println("Enter Radius for Perimeter: ");
CirclePerimeter();
break;
case "A":
System.out.println("Enter Radius for Area: ");
CircleArea();
break;
}
}
public double CirclePerimeter() {
super.radius = sc.nextDouble();
this.perimeter = 2 * Math.PI * super.radius;
System.out.println(perimeter);
return perimeter;
}
public double CircleArea() {
super.radius = sc.nextInt();
this.area = Math.PI * (super.radius*super.radius);
System.out.println(this.area);
return area;
}
}
public class Triangle extends Shapes{
public void triangleChoice() {
switch (choice) {
case 1:
System.out.println("Enter Radius for Perimeter: ");
TrianglePerimeter();
break;
case 2:
System.out.println("Enter Radius for Area: ");
TriangleArea();
break;
}
}
public double TrianglePerimeter() {
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
perimeter = a*b*c;
System.out.println(this.perimeter);
return perimeter;
}
public double TriangleArea() {
b = sc.nextInt();
h = sc.nextInt();
area = 12 * (b*h);
System.out.println(this.area);
return area;
}
}
private Circle obj1 = new Circle(); //error
private Triangle obj2 = new Triangle();
public void inputProcess() {
Shapes s = new Shapes();
System.out.println("Geometric Shapes");
System.out.println("Choose Shape to continue:");
System.out.println("[1] Circle");
System.out.println("[2] Triangle");
System.out.println("[0] Exit");
System.out.println("Enter your choice: ");
s.choice = sc.nextInt();
switch (s.choice) {
case 1 :
obj1.circleChoice();
break;
case 2 :
obj2.triangleChoice();
break;
case 0:
System.exit(s.choice);
break;
default :
System.out.println("Invalid output! Please run again!");
break;
}
}
public static void main(String[] args) {
Shapes p = new Shapes();
p.inputProcess();
}
}
How the program should work:
I have a class that contains the method inputProcess which has a switch statement that should call either method circleChoice() from class Circle, or method triangleChoice from class Triangle depending on the user's input. Let's focus on Circle to make it a bit easier.
circleChoice() has a Scanner input from radDiameter and a switch statement that will call either circlePerimeter() or circleArea(). The previous two methods have formulas that will solve a circle's perimeter or area given by another Scanner input (radius) in each method, which works just fine.
After that, I created the object private Circle obj1 = new Circle(); just outside every subclass, but still inside the main class Shapes in order to call the methods from Circle in inputProcess().
inputProcess() has a switch statement that should call either obj1.circleChoice(); or obj2.triangleChoice(); depending on the value of s.choice.
Problem:
When I try to run it, it just shows a java.lang.StackOverflowError on console which is highlighting line 24 and 93. It's obviously a logical error, since Eclipse didn't show any warning highlights.
Normally, I would be able to complete this task without using any additional classes, but this is for a school project that requires me to use them.
Answer is simple. Because Circle is a subclass to Shape, when you instantiate Shape, and then Circle as it's attribute, then Circle instantiates Circle as it's attribute (because Circle IS Shape), and then Circle instantiates Circle as it's attribute and so on... So you have the infinite recursion.
You really should refactor your code, SuperClass should not aggregate SubClasses. And they should be also should not be inner classes of their SuperClass. Then it should work.
I am creating a small game-type program,
Firstly I'm asking the user to give his name,
Secondly, I'm asking the user to choose one of the spaceships,
Thirdly, I want the user to add 2 modification, to improve the stats...
Here is the problem, I want to change some values of the spaceship.
The problem is that I don't know how to determine the chosen spaceship and how to change its values. Example: The Heavy-type Armour is 90 right now if I add the Advanced Hull Armour modification, i want it to increase to 100
public class Game {
static void playerName() {
System.out.println("What's your name?:");
String name = Keyboard.readString();
System.out.println("Okay,"+name+" it is!\n");
}
static class StarShip {
String name;
String armour;
String attack;
String mobility;
}
public static void main (String args[]) {
System.out.println("Hello Rookie! \n");
playerName();
System.out.println("Let's pick your first ship!\n");
System.out.println("Choose one of these:");
//insert ships here:
StarShip ship1 = new StarShip();
ship1.name = "Heavy-type";
ship1.armour = "Armour=90";
ship1.attack = "Firepower=50";
ship1.mobility = "Mobility=40";
StarShip ship2 = new StarShip();
ship2.name = "Medium-type";
ship2.armour = "Armour=60";
ship2.attack = "Firepower=60";
ship2.mobility = "Mobility=60";
StarShip ship3 = new StarShip();
ship3.name = "Light-type";
ship3.armour = "Armour=20";
ship3.attack = "Firepower=60";
ship3.mobility = "Mobility=90";
System.out.println("1 -"+ship1.name+"- "+ship1.armour+" "+ship1.attack+" "+ship1.mobility);
System.out.println("2 -"+ship2.name+"- "+ship2.armour+" "+ship2.attack+" "+ship2.mobility);
System.out.println("3 -"+ship3.name+"- "+ship3.armour+" "+ship3.attack+" "+ship3.mobility);
int chosen = Keyboard.readInt();
switch (chosen) {
case 1:
System.out.println("Heavy-type! Excellent choice! Great armour and guns! \n");
break;
case 2:
System.out.println("Medium-type! An all-rounder, a mix of everything! \n");
break;
case 3:
System.out.println("Light-type! Fast and mobile, but has little armour! \n");
break;
}
System.out.println("Lets pimp out your ship! \n");
String advancedHull = "1 - Advanced Hull Armour - Armour +10";
String betterAmmo = "2 - Better Ammo \t - Firepower +10";
String booster = "3 - Booster \t\t - Mobility +10";
System.out.println(advancedHull+"\n"+betterAmmo+"\n"+booster);
}
}
Currently you the fields of the StarShip are all Strings, when you add two strings they are concatenated, eg "abc"+"def" = "abcdef" regardless of what they say. You want to use ints these are "java numbers" and add as you would expect eg 10 + 20 = 30
class StarShip {
String name;
int armour;
int attack;
int mobility;
}
...
StarShip ship1 = new StarShip();
ship1.name = "Heavy-type";
ship1.armour = 90;
ship1.attack = 50;
ship1.mobility = 40;
...
When the user selects the ship we want to save this chose into a variable, like so:
Starship chosenShip; // must be declared outside the switch statement
switch (shipChoice ) {
case 1:
System.out.println("Heavy-type! Excellent choice! Great armour and guns! \n");
chosenShip = ship1;
Break;
...
Assuming advancedHull is selected and you include logic similar to how ships are selected
if (modificationChoice = 1){
chosenShip.armour = chosenShip.armour + 10;
} ...
You may want to declare a toSting() methods in StarShip like so
#Override
public String toString() {
return name +" Armour=" + armour +", Firepower=" + attack +", Mobility=" + mobility;
}
This way you can call System.out.println(ship1.toString()) to print the ship info.
Example:
System.out.println(ship1.toString());
// outputs: Heavy-type Armour=90, Firepower=50, Mobility=40
ship1.armour += 10; // java shorthand for `ship1.armour + ship1.armour + 10`
System.out.println(ship1.toString());
// outputs: Heavy-type Armour=100, Firepower=50, Mobility=40
So, I have a variable in a parent class that I am trying to change in a subclass with getter/setter methods. But, the value is just staying the same and I have no idea why.. What am I doing wrong? Any help is appreciated!
Here is a breakdown of the program: In the driver class, you choose what you want to do, then it uses the current value variable and a number you choose (operand2) to get the answer. The add, subtract, multiply and divide are in the memory calculator class. It can also clear, which sets the current value variable to zero. Now, we are adding a sub class to it that does exponents and logarithms.
specifics: The variable currentValue in the MemoryCalc class stays the same when I try to use the power or log methods in the ScientificMemCalc class. In that class it uses a getter method to get the current value and then attempts to use a setter method to change the current value. But nothing changes. And another problem: the getter method gets a zero value from the currentValue field.
Here is driver class with main method:
package ScientificMemCalc;
import java.util.Scanner;
import ScientificMemCalc.MemoryCalc;
public class ScientificCalcDriver {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MemoryCalc calculator = new MemoryCalc();
ScientificMemCalc scientificCalc = new ScientificMemCalc();
int menu = 0;
double operand2, answer;
while (menu !=8) {
answer = calculator.getCurrentValue();
System.out.println("The current value is: " + answer);
menu = getMenuOption();
switch(menu) {
case 1:
// Add
operand2 = calculator.getOperand("What is the second number?: ");
calculator.add(operand2);
break;
case 2:
// Subtract
operand2 = calculator.getOperand("What is the second number?: ");
calculator.subtract(operand2);
break;
case 3:
// Multiply
operand2 = calculator.getOperand("What is the second number?: ");
calculator.multiply(operand2);
break;
case 4:
// Divide
operand2 = calculator.getOperand("What is the second number?: ");
calculator.divide(operand2);
break;
case 5:
// Power
operand2 = calculator.getOperand("What is the second number?: ");
scientificCalc.power(operand2);
break;
case 6:
// Logarithm
scientificCalc.log();
break;
case 7:
// Clear
operand2 = 0;
calculator.clear();
break;
case 8:
// Quit
System.out.println("Goodbye!");
break;
}
}
}
public static int getMenuOption() {
Scanner input = new Scanner(System.in);
int choice = 0;
// Display menu
System.out.println("Menu:");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Power");
System.out.println("6. Logarithm");
System.out.println("7. Clear");
System.out.println("8. Quit");
// Get menu input
System.out.print("What would you like to do?: ");
choice = input.nextInt();
while (choice < 1 || choice > 8) {
System.out.print("Invalid. Try again: ");
choice = input.nextInt();
}
return choice;
}
}
Here is the memory calculator class:
package ScientificMemCalc;
import java.util.Scanner;
public class MemoryCalc {
private double currentValue;
public double getOperand(String prompt) {
Scanner input = new Scanner(System.in);
System.out.print(prompt);
return input.nextDouble();
}
public double getCurrentValue() {
return currentValue;
}
public void setCurrentValue(double temp) {
currentValue = temp;
}
public void add(double operand2) {
// Add
currentValue += operand2;
}
public void subtract(double operand2) {
// Subtract
currentValue -= operand2;
}
public void multiply(double operand2) {
// Multiply
currentValue *= operand2;
}
public void divide(double operand2) {
// Divide
if (operand2 == 0) {
System.out.println("You cannot divide by zero!");
currentValue = Double.NaN;
}
else {
currentValue /= operand2;
}
}
public void clear() {
// Clear
currentValue = 0;
}
}
And finally the subclass to add scientific functions:
package ScientificMemCalc;
public class ScientificMemCalc extends MemoryCalc {
public void power(double operand2) {
// Power
double currentValue = getCurrentValue();
double temp = Math.pow(currentValue, operand2);
setCurrentValue(temp);
}
public void log() {
// Logarithm
double currentValue = getCurrentValue();
double temp = Math.log(currentValue);
setCurrentValue(temp);
}
}
Short answer
You're interacting with TWO DIFFERENT OBJECTS calculator and scientificCalc. They do not share any state.
What can you do?
Use scientificCalc for all the calculations. Thus the value will be one and only.
cons : the things can become much more complicated once you introduce another calculator types
Pass the calculator to constructor of ScientificMemCalc as a constuctor parameter.
cons : same as for #1
Use a separate class/object for storing the state. (as suggested by #user2864740)
Do not store the state inside the calculator (Why would you need that?). Pass all the operands (current state will be always operand #1) to the methods and return the result to caller:
int menu = 0;
double operand2, answer;
while (menu !=8) {
// You don't need this line
//answer = calculator.getCurrentValue();
System.out.println("The current value is: " + answer);
menu = getMenuOption();
switch(menu) {
case 1:
// Add
operand2 = calculator.getOperand("What is the second number?: ");
//
answer = calculator.add(answer, operand2);
break;
//... modify other operations in the same way
}
In the ScientificMemCalc class you extend MemoryCalc, this lets ScientificMemCalc call the methods created in MemoryCalc and provides access to its own set of variables in MemoryCalc. However, when creating two separate objects of each class, they will both have access to a variable of the same name and type, but they will be two separate variables in two separate objects.
One solution is passing in a MemoryCalc object into the constructor of the ScientificMemCalc class then calling the getCurrentValue()/setCurrentValue() methods in respects to that object. Allowing access to the same variable.
package scientificMemCalc;
public class ScientificMemCalc {
//store the MemoryCalc object so we have access to the needed data
private MemoryCalc memCalc;
//store the MemoryCalc
public ScientificMemCalc(MemoryCalc memCalc) {
this.memCalc = memCalc;
}
public void power(double operand2) {
// Power
double currentValue = memCalc.getCurrentValue();
double temp = Math.pow(currentValue, operand2);
memCalc.setCurrentValue(temp);
}
public void log() {
// Logarithm
double currentValue = memCalc.getCurrentValue();
double temp = Math.log(currentValue);
memCalc.setCurrentValue(temp);
}
}
Then when instantiating your ScientificMemCalc object pass in the reference for your MemoryCalc.
MemoryCalc calculator = new MemoryCalc();
ScientificMemCalc scientificCalc = new ScientificMemCalc(calculator);
Hope this helps!
I cannot get my program to take a user input as a string and then see if it equals "yes" or "no" and if it equals neither, then to display "incorrect entry." It always displays "incorrect entry" regardless if I type "yes" or "no." I have tried a few different types of if and do/while's but I just can't seem to get it:
Class file:
import java.util.Scanner;
public class PhysicsProblem
{
private double vI; // initial velocity
private double vF; // final velocity
private double t; // time
private double deltaX; // change in the x value
//Make sure to add acceleration
public PhysicsProblem (double vI, double vF, double t, double deltaX)
{
this.vI = vI;
this.vF = vF;
this.t = t;
this.deltaX = deltaX;
}
public void setVi(String strVi)
{
while (!(strVi.equals("no") || strVi.equals("yes")));
{
System.out.println("incorrect entry");
}
if (strVi.equals("yes"))
{
System.out.println("Enter the initial velocity: ");
vI = new Scanner(System.in).nextDouble();
}
if (strVi.equals("no"))
{
System.out.println("The program is assuming you want to solve" +
"for intial velocity");
}
}
Program:
import java.util.Scanner;
public class PhysicsProblemSolver
{
public static void main (String[] args)
{
double vI = 0;
double vF = 0;
double t = 0;
double deltaX = 0;
Scanner scan = new Scanner(System.in);
PhysicsProblem problem1 = new PhysicsProblem (vI, vF, t, deltaX);
// Checks to see if initial velocity is given
System.out.println("Do you know the initial velocity? (Type 'yes' or 'no')");
String strVi = scan.next();
problem1.setVi(strVi);
I know it looks like an incomplete program, and it is, but I just needed help with this one section, so I tried not to include the unnecessary parts. Sorry if it's confusing!
you can do the testing in main itself i you can,
Scanner sc = new Scanner(System.in);
String input;
do{
System.out.println("enter valid input");
input=sc.next();
}while(!(input.equals("yes") || input.equals("no")));
Then simply pass input to function.
remove unnecessary checks there. as others mentioned its infinite loop(deadlock).
while (!(strVi.equals("no") || strVi.equals("yes")));
while (!(strVi.equals("no") || strVi.equals("yes")));
will always evaluate to true if the input is neither 'yes' nor 'no' so it will go into infinite loop.
Have you tried:
public void setVi(String strVi){
if(strVi.equalsIgnoreCase("yes")){
//What if yes
}else if(strVi.equalsIgnoreCase("no")){
//What if no
}else{
//What if not yes nor no
}
}
I am beginning Java programming, and I have written a program which tests how many moons each planet has. Here is a shortened version with only four of the planets.
import java.util.Scanner;
import java.util.Random;
public class one {
public static void main(String args[]){
//SET VARIABLES
String zero="Mercury", one="Venus", two="Earth", three="Mars";
//SET VARIABLES
for (int x=1; x<=1000; x++){
System.out.println("Moons");
Random r = new Random();
for (int y=1; y<=1; y++){
int rI = r.nextInt(4);
if (rI == 0){
question(zero,0);
}
else if (rI == 1){
question(one, 0);
}
else if (rI == 2){
question(two, 1);
}
else if (rI == 3){
question(three, 2);
}
}
}
}
public static void question(String n, int num){
Scanner input = new Scanner(System.in);
System.out.print("How many moons does " + n + " have? ");
int ans = input.nextInt();
if (ans == num){
System.out.println("Correct!");
}
else if (ans != num){
System.out.println("Incorrect!");
question(n, num);
}
}
}
How would I go about not having to write "else if" so many times? This becomes very tedious with more statements. Keep in mind I am a beginner, and this code is about the limit of my current abilities.
You could use arrays like so:
String[] planets = { "Mercury", "Venus", "Earth", "Mars" };
int moons[] = { 0, 0, 1, 2 };
and call:
if (rI >= 0 && rI < planets.length) {
question(planets[rI], moons[rI]);
}
A better way to write this code. It is more readable and it is very easy to make any updates.
import java.util.Scanner;
import java.util.Random;
public class one {
public static void main(String args[]){
//SET VARIABLES
String planets []=new String[4];
planets[0]="Mercury";
planets[1]="Venus";
planets[2]="Earth";
planets[3]="Mars";
int moons []=new int[4];
moons[0]=0;
moons[1]=0;
moons[2]=1;
moons[3]=2;
//SET VARIABLES
while(true){
System.out.println("Moons");
Random r = new Random();
int rI = r.nextInt(4);
question(planets[rI],moons[rI]);
}
}
public static void question(String n, int num){
Scanner input = new Scanner(System.in);
System.out.print("How many moons does " + n + " have? ");
int ans = input.nextInt();
if (ans == num){
System.out.println("Correct!");
}
else if (ans != num){
System.out.println("Incorrect!");
question(n, num);
}
}
}
You could go with switch or if-else as long as you logic does not evolve. Otherwise you need to start the object-oriented programming.
Create a class "Planet" and another class for each planet that inherits from Planet and add the planet specific information to each of it. Then you're good for the future when you may plan to add some more questions for the planets.
Take a look at the switch statement in Java. That is designed to help overcome a screwy if-elseif-elseif-elseif-else block:
switch(rI){
case 2: question(two, 1);
break;
case 3: question(three, 2);
break;
default: doSomethingClever(...);
}
There are some other cleanups you could employ, but getting that switch block in place addresses your initial comment. Good luck.
You can try using switch instead of the long if-else ladder.
For more information on switch read the java documentation
In your case it could be something like:
switch (rI) {
case 0:
question(zero, 0);
break;
case 1:
question(one, 0);
break;
case 2:
question(two, 1);
break;
case 3:
question(three, 2);
break;
default:
break;
}
You could use the switch keyword:
switch (rI) {
case 0:
question(zero, 0);
break;
case 1:
question(one, 0);
break;
case 2:
question(two, 1);
break;
case 3:
question(three, 2);
break;
default:
// do something
}
The code under default executes when no match is found.
You could also use arrays:
String[] planets = new String[]{"Mercury", "Venus", "Earth", "Mars"};
int[] nums = new int[]{0, 0, 1, 2};
...
// in the loop:
question(planets[rI], nums[rI]);
Of course, there are other ways to approach the problem, but I think for someone who is just learning these should do the trick. One thing you might want to look into, however, (once you get deeper into Java), is the concept of a Map, much like a dictionary in Python. You could maintain a map that maps a planet (string) to the number of moons it has (int).
I would prefer enums over arrays here. See this enums tutorial (especially the planets example ;) ). Add another field (just like mass and radius) for moon count.
I would start with forgetting that vector-thing and putting Planets and their Moons together in a POJO (Plain Old Java Object) and putting those objects into the Array:
class Planet {
int moons;
String name;
Planet(String name, int moons) {
this.name = name;
this.moons = moons;
}
public String getName() {
return this.name;
}
public getMoons() {
return this.moons;
}
public void question(){
Scanner input = new Scanner(System.in);
System.out.print("How many moons does " + this.name + " have? ");
int ans = input.nextInt();
if (ans == this.moons){
System.out.println("Correct!");
}
else {
System.out.println("Incorrect!");
}
}
}
public class one {
public static void main(String args[]){
//SET VARIABLES
List<Planet> planets = new ArrayList<Planets>();
Planet earth = new Planet("Earth", 1);
// put other planets
//SET VARIABLES
for (int x=1; x<=1000; x++) {
System.out.println("Moons");
Random r = new Random();
int rI = r.nextInt(planets.size());
Planet p = planets.get(rI);
p.question();
}
}
}