I know it has something to do with my bracket placement, but I am not sure where the error is occurring. Keep in mind this is a 2nd method within my class.
import java.util.Scanner*;
import java.util.Arrays.*;
public class BasicMathTest
{
//Creates an array to store the numbers that will be used in the math
problems
int alpha[] = {4, 8, 10, 15, 25, 30};
int beta[] = {2, 4, 5, 3, 5, 10};
double Problems = alpha.length;
public static void main(String args[])
{
System.out.println("Welcome to this Math Quiz. Here you will add, subtract, multiply and divide!");
//Ask the user to choose their type of problem
Scanner kbReader = new Scanner(System.in);
System.out.println("Select Addition (1), Subtraction (2), Multiplication (3) or Division (4): ");
int Selection = kbReader.nextInt();
//Calculates the users score
double score = Selection * 100 / Problems;
System.out.println("Your score on the test: " + score + "%");
}
public static double Selection ()
{
int score = 0; //Stores the number of correct answers
int correct; //Stores the correct answer
for (int i = 0; i < Problems; i++)
{
if (Selection == 1)
{
System.out.println(alpha[i] + " + " + beta[i]);
correct = alpha[i] + beta[i];
}
else if(Selection == 2)
{
System.out.print(alpha[i] + " - " + beta[i]);
correct = alpha[i] - beta[i];
}
else if (Selection = 3)
{
System.out.print(alpha[i] + " * " + beta[i]);
correct = alpha[i] * beta[i];
}
else if(Selection == 4)
{
System.out.print(alpha[i] + " / " + beta[i]);
correct = alpha[i] / beta[i];
}
}
System.out.println(" ");
}
return score;
}
Output: E:\Xinox Software\JCreatorV4LE\MyProjects\CreateTask\BasicMathTest.java:52: illegal start of type
return score;
^
E:\Xinox Software\JCreatorV4LE\MyProjects\CreateTask\BasicMathTest.java:52: <identifier> expected
return score;
^
2 errors
Problems:
you forgot an opening braces at "Selection == 4" if.
Write "==" instead of **"=""" in "else if (Selection == 3)"
type of score is double
Could you show us how you use score var ?
Look at this and let me know if it is right :
public static double Selection ()
{
double score = 0; //change from int to double
int correct; //Stores the correct answer
for (int i = 0; i < Problems; i++)
{
if (Selection == 1)
{
System.out.println(alpha[i] + " + " + beta[i]);
correct = alpha[i] + beta[i];
}
else if(Selection == 2)
{
System.out.print(alpha[i] + " - " + beta[i]);
correct = alpha[i] - beta[i];
}
else if (Selection == 3)
{
System.out.print(alpha[i] + " * " + beta[i]);
correct = alpha[i] * beta[i];
}
else if(Selection == 4)
{ // missing brace
System.out.print(alpha[i] + " / " + beta[i]);
correct = alpha[i] / beta[i];
}
System.out.println(" ");
}
return score;
}
I am stuck at a part where in a game, I use while loop and to end the loop and get the results of the game, I want either "player1" or "player2" to enter "Q", and so i tried doing it like this:
if (player1.equals("Q") || player2.equals("Q")){
go = false; //go is a boolean variable
}
This doesn't seem to work as I have to enter "Q" for both player1 and player2 for the game to end, but instead I just want either of them to enter "Q" and the game would stop.
Code:
import java.util.Scanner;
public class Team {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Soccer Game Between 2 Teams");
System.out.println("Win is 2 points" + "\n" + "Loss is worth 0 points" + "\n" + "Overtime is worth 1 point");
System.out.println("Type W, O, or L" + "\n" + "Type Q to end the game");
int pointsw = 0;
int pointsl = 0;
int pointso = 0;
int pointsw2 = 0;
int pointsl2 = 0;
int pointso2 = 0;
int totalpoints = 0;
int totalpoints2 = 0;
int counter = 0;
int counter2 = 0;
boolean go = true;
System.out.println("\n" + "Enter team one:");
String phrase = keyboard.next();
System.out.println("\n" + "Enter team two:");
String phrase2 = keyboard.next();
System.out.println();
while (go) {
System.out.println("Enter " + phrase + " Result:");
String team1 = keyboard.next();
System.out.println("Enter " + phrase2 + " Result");
String team2 = keyboard.next();
if (team1.equals("W") || team1.equals("w")) {
pointsw += 2;
} else if (team1.equals("O") || team1.equals("o")) {
pointso += 1;
} else if (team1.equals("L") || team1.equals("l")) {
pointsl += 0;
}
counter++;
if (team2.equals("W") || team2.equals("w")) {
pointsw2 += 2;
} else if (team2.equals("O") || team2.equals("o")) {
pointso2 += 1;
} else if (team2.equals("L") || team2.equals("l")) {
pointsl2 += 0;
}
counter2++;
totalpoints = pointsw + pointso + pointsl;
totalpoints2 = pointsw2 + pointso2 + pointsl2;
if (team1.equals("Q") || team2.equals("Q")) {
go = false;
if (totalpoints > totalpoints2) {
System.out.println(phrase + " wins with " + totalpoints + " points");
System.out.println("It took " + phrase + " " + counter + " rounds to win");
} else if (totalpoints < totalpoints2) {
System.out.println(phrase2 + " wins with " + totalpoints2 + " points");
System.out.println("It took " + phrase2 + " " + counter2 + " rounds to win");
} else if (totalpoints == totalpoints2) {
int totalrounds = counter + counter2;
System.out.println("It is tie game between " + phrase + " and " + phrase2);
System.out.println("The game lasted till " + totalrounds + " rounds");
}
}
}
}
}
You should reorganize your code:
while (true) {
System.out.println("Enter " + phrase + " Result:");
String team1 = keyboard.next().toLowerCase();
if ("q".equals(team1)) {
break;
}
System.out.println("Enter " + phrase2 + " Result");
String team2 = keyboard.next().toLowerCase();
if ("q".equals(team2)) {
break;
}
if (team1.equals("w")) {
pointsw += 2;
} else if (team1.equals("o")) {
pointso += 1;
} else if (team1.equals("l")) {
pointsl += 0;
}
counter++;
if (team2.equals("w")) {
pointsw2 += 2;
} else if (team2.equals("o")) {
pointso2 += 1;
} else if (team2.equals("l")) {
pointsl2 += 0;
}
counter2++;
totalpoints = pointsw + pointso + pointsl;
totalpoints2 = pointsw2 + pointso2 + pointsl2;
} // loop completed
if (totalpoints > totalpoints2) {
System.out.println(phrase + " wins with " + totalpoints + " points");
System.out.println("It took " + phrase + " " + counter + " rounds to win");
} else if (totalpoints < totalpoints2) {
System.out.println(phrase2 + " wins with " + totalpoints2 + " points");
System.out.println("It took " + phrase2 + " " + counter2 + " rounds to win");
} else if (totalpoints == totalpoints2) {
int totalrounds = counter + counter2;
System.out.println("It is tie game between " + phrase + " and " + phrase2);
System.out.println("The game lasted till " + totalrounds + " rounds");
}
I'm not completely sure, but I think the issue is that after player 1 / player 2 says 'Q'
the scanner is still waiting for the next line to read.
String phrase = keyboard.next();
System.out.println("\n"+"Enter team two:");
String phrase2 = keyboard.next();//if player 1 types q this next() method must be resolved before it will continue to the logic
so add an if statement before play 2 goes asking if player 1 typed 'Q' , if so calculate scores and end game, if player 1 did not type 'Q' use else statement to continue on to player 2's turn
Creating a program that uses a menu to call separate modules of a simple health tracker for a beginner programming course.
Would appreciate some help concerning the exact reason why the array isn't working properly and is "resolved to a string"
I have a lot more to add before i can submit the program but this is holding me up.
It is in Module 3, the line attempting to recall the array
I'm leaving the entire code so far here because I don't understand what I've done wrong and am hoping this place is more helpful than the useless forums at uni.
public class HealthMate {
double bmi, bmr, heightM, weightKG;
int age, week = 7, days = 1;
int calories[] = new int[days];
int menuChoiceInt;
char genderChar;
boolean male;
public static void main(String[] args) {
HealthMate firstObj = new HealthMate();
firstObj.menu();
}
public void menu() {
while (menuChoiceInt != 4) {
String menu = "HealthMate Alpha 0.1 \n " + "Please make a numerical selection \n";
menu += "[1] Enter or Update your Details\n";
menu += "[2] Return BMI and BMR \n"; // menu options call different modules
menu += "[3] Weekly Tracker and Advice \n";
menu += "[4] Exit \n";
String menuChoiceString = JOptionPane.showInputDialog(menu);
menuChoiceInt = Integer.parseInt(menuChoiceString);//
if (menuChoiceString != null) {
if (menuChoiceInt == 1) {
genderChar = JOptionPane.showInputDialog("Please Enter your Gender as either M or F").charAt(0);
heightM = Double.parseDouble(
JOptionPane.showInputDialog("Enter Height in Meters,\n eg 1.73 for 173 cm.: "));
if (heightM <= 0) {
heightM = Double.parseDouble(JOptionPane.showInputDialog("Error! Enter a postitive number"));
}
weightKG = Double.parseDouble(JOptionPane.showInputDialog("Enter Weight in Kilograms"));
if (weightKG <= 0) {
weightKG = Double.parseDouble(JOptionPane.showInputDialog("Error! Enter a postitive number"));
}
bmi = weightKG / Math.pow(heightM, 2.0);
male = genderChar == 'M';
if (male) {
bmr = (10 * weightKG) + (62.5 * heightM) - (5 * age) + 5;
} else {
bmr = (10 * weightKG) + (62.5 * heightM) - (5 * age) - 161;
JOptionPane.showMessageDialog(null,"Your Specific BMI and BMR have been ");
menuChoiceInt = Integer.parseInt(menuChoiceString);// recall menu
}
}
if (menuChoiceInt == 2) if (bmi < 18.5) {
JOptionPane.showMessageDialog(null,
"Your BMI is " + bmi + ", You are underweight.\n" + "Your BMR is " + bmr);
} else if (bmi < 25) {
JOptionPane.showMessageDialog(null, "Your BMI is " + bmi
+ ", You are within the healthy weight range.\n" + "Your BMR is " + bmr);
} else if (bmi < 30) {
JOptionPane.showMessageDialog(null,
"Your bmi is " + bmi + ", You are overweight\n" + "Your BMR is " + bmr);
} else {
JOptionPane.showMessageDialog(null,
"Your bmi is " + bmi + ", You are Obese" + "Your BMR is " + bmr);
}
JOptionPane.showMessageDialog(null,
"This module is supposed to recall your BMI and BMR \n"
+ "and give general advice on health.");
{
menuChoiceInt = Integer.parseInt(menuChoiceString);
}
if (menuChoiceInt == 3) {
while (days > week) {
calories[week] = Integer.parseInt(JOptionPane.showInputDialog("Enter Calories for day"[days]);// employee salary
days = days + 1;
JOptionPane.showMessageDialog(null,
"This module is supposed to store data in an array over the course \n"
+ "of a week to show you your pattern of intake vs output.");
}
{
menuChoiceInt = Integer.parseInt(menuChoiceString);
}
} else if (menuChoiceInt == 4) {
}
}
}
}
}
I'm trying to get the calorie input to be saved over the course of 7 days so I can average it out, compare it to BMR and Activity level and give general advice on whether you are in surplus or deficit of calorie intake.
PS: Maybe if you have years of experience don't start your reply with "Well its obvious that..." and continue your mockery of someone who started programming less than a month ago as you people so often seem to on this website.
You have int days = 1, but you use it as [day] - this is incorrect in java:
calories[week] = Integer.parseInt(JOptionPane.showInputDialog("Enter Calories for day " + days));
I'm new to OOP and I've hit a brick wall. I have been trying and trying to get a program that enters price houses and adds fees and taxes. The code below is very hard to get to work, I keep running into "cannot find symbol errors" or "possible lossy conversion between double and int" etc. Can you please help me figure what is going wrong with the following class and tester code? This is only a part of the code that I am working on at the moment. Feel free to ask me questions on it and if needed I'll try my best to clarify what I'm trying to do. Thank you.
The class code;
// method that sets the number of houses
public void setAmountOfHouses(int amountOfHouses)
{
//System.out.println("Enter the amount of houses for sale: "); // prompt
if (amountOfHouses > 0) // if the number of houses is valid
this.amountOfHouses = amountOfHouses; // assign it to instance variable number of houses
else
System.out.println("Please add at least 1 house to database... ");
}
// method that returns the number of houses
public int getAmountOfHouses()
{
return amountOfHouses;
}
// method that set the house price
public void setHousePrice(int housePrice)
{
if(housePrice > 0)
{
this.housePrice = housePrice;
}
else
{
System.out.println("Enter a valid house price ");
}
}
// method that returns the house price
public double getHousePrice()
{
return housePrice;
}
//method to calculate fees and taxes
public void calculateFees(int housePrice)
{
if(housePrice > 0 && housePrice <= 100000)
{
housePriceWithTax = housePrice + tax0; // no tax
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
else if(housePrice >= 100001 && housePrice <= 200000)
{
housePriceWithTax = (((housePrice - 100000) * stampDuty1) + housePrice) + tax0;
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
else if(housePrice >= 200001 && housePrice <= 300000)
{
housePriceWithTax = (((housePrice - 200000) * stampDuty2) + housePrice) + tax0 + tax1;
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
else if(housePrice >= 300001 && housePrice <= 400000)
{
housePriceWithTax = ((housePrice - 300000) * stampDuty3) + housePrice + tax0 + tax1 + tax2;
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
else if(housePrice >= 400001)
{
housePriceWithTax = (((housePrice - 400000) * stampDuty4) + housePrice) + tax0 + tax1 + tax2 + tax3;
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
else
{
housePriceWithTax = (((housePrice - 400000) * stampDuty4) + housePrice) + tax0 + tax1 + tax2 + tax3 + tax4;
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
System.out.println("Base house price is: " + housePrice);
System.out.println("Cost of house with tax added is: " + housePriceWithTax);
System.out.println("Total auctioneer fee on this house is: " + totalAuctioneerFeeAmount);
System.out.println("Total cost of house with stamp duty and fees added is: " + totalHousePrice);
}
}
The tester code;
// Entering the amount of houses
System.out.println("\nEnter the amount of houses for sale: "); // prompt
int amountOfHouses = input.nextInt(); // obtain from input
client1.setAmountOfHouses(amountOfHouses);
if(client1.getAmountOfHouses() > 0)
System.out.println("Adding " + client1.getAmountOfHouses() + " houses to database..." );
else
System.out.println("Returning...");
// setting the price of each house
int counter = 0;
do
{
System.out.println("\nEnter the price of each house for sale: ");
int housePrice = input.nextInt();
client1.setHousePrice(housePrice);
System.out.println("\nAdding house price of " + client1.getHousePrice() + " to database");
counter++;
}
while(counter < amountOfHouses);
// calculating fees and taxes
counter = 0;
do
{
client1.calculateFees(client1.getHousePrice());
counter++;
}
while(counter < amountOfHouses);
}
}
How can I limit the user to spend a "Tag" on a different "Skill" on each inputFirstTag, inputSecondTag, inputThirdTag
Everything works fine until Round 2 rears its ugly head. inputSecondTag can become a duplicate of inputFirstTag, inputThirdTag can become a duplicate of inputSecondTag.
System.out.println("You have 3 Skills to Tag.");
System.out.println("What skill would you like to Tag? (+25)");
System.out.print("Small Guns, Big Guns, Energy Weapons, Unarmed, Melee Weapons, Throwing, ");
System.out.print("First Aid, Doctor, Sneak, Lockpick, Steal, Traps, Science, Repair, ");
System.out.println("Speech, Barter, Gambling or Outdoors?");
Scanner scanFirstTag = new Scanner(System.in);
String inputFirstTag = null;
while (scanFirstTag.hasNextLine()) {
inputFirstTag = scanFirstTag.nextLine();
if (inputFirstTag.equalsIgnoreCase("Small Guns") || inputFirstTag.equalsIgnoreCase("Big Guns") ||
inputFirstTag.equalsIgnoreCase("Energy Weapons") || inputFirstTag.equalsIgnoreCase("Unarmed") ||
inputFirstTag.equalsIgnoreCase("Melee Weapons") || inputFirstTag.equalsIgnoreCase("Throwing") ||
inputFirstTag.equalsIgnoreCase("First Aid") || inputFirstTag.equalsIgnoreCase("Doctor") ||
inputFirstTag.equalsIgnoreCase("Sneak") || inputFirstTag.equalsIgnoreCase("Lockpick") ||
inputFirstTag.equalsIgnoreCase("Steal") || inputFirstTag.equalsIgnoreCase("Traps") ||
inputFirstTag.equalsIgnoreCase("Science") || inputFirstTag.equalsIgnoreCase("Repair") ||
inputFirstTag.equalsIgnoreCase("Speech") || inputFirstTag.equalsIgnoreCase("Barter") ||
inputFirstTag.equalsIgnoreCase("Gambling") || inputFirstTag.equalsIgnoreCase("Outdoors"))
break;
else System.out.println("Please choose Small Guns, Big Guns, Energy Weapons, " +
"Unarmed, Melee Weapons, Throwing, First Aid, Doctor, " +
"Sneak, Lockpick, Steal, Traps, Science, Repair, " +
"Speech, Barter, Gambling or Outdoors?");
}
if (inputFirstTag.equalsIgnoreCase("Small Guns")) {
System.out.println("Small Guns Increased by 25!");
smallGuns = smallGuns + 25;
System.out.println("Small Guns: " + smallGuns);
} else if (inputFirstTag.equalsIgnoreCase("Big Guns")) {
System.out.println("Big Guns Increased by 25!");
bigGuns = bigGuns + 25;
System.out.println("Big Guns: " + bigGuns);
} else if (inputFirstTag.equalsIgnoreCase("Energy Weapons")) {
System.out.println("Energy Weapons Increased by 25!");
energyWeapons = energyWeapons + 25;
System.out.println("Energy Weapons: " + energyWeapons);
} else if (inputFirstTag.equalsIgnoreCase("Unarmed")) {
System.out.println("Unarmed Increased by 25!");
unarmed = unarmed + 25;
System.out.println("Unarmed: " + unarmed);
} else if (inputFirstTag.equalsIgnoreCase("Melee Weapons")) {
System.out.println("Melee Weapons Increased by 25!");
meleeWeapons = meleeWeapons+ 25;
System.out.println("Melee Weapons: " + meleeWeapons);
} else if (inputFirstTag.equalsIgnoreCase("Throwing")) {
System.out.println("Throwing Increased by 25!");
throwing = throwing + 25;
System.out.println("Throwing: " + throwing);
} else if (inputFirstTag.equalsIgnoreCase("First Aid")) {
System.out.println("First Aid Increased by 25!");
firstAid = firstAid+ 25;
System.out.println("First Aid: " + firstAid);
} else if (inputFirstTag.equalsIgnoreCase("Doctor")) {
System.out.println("Doctor Increased by 25!");
doctor = doctor + 25;
System.out.println("Doctor: " + doctor);
} else if (inputFirstTag.equalsIgnoreCase("Sneak")) {
System.out.println("Sneak Increased by 25!");
sneak = sneak + 25;
System.out.println("Sneak: " + sneak);
} else if (inputFirstTag.equalsIgnoreCase("Lockpick")) {
System.out.println("Lockpick Increased by 25!");
lockpick = lockpick + 25;
System.out.println("Lockpick: " + lockpick);
} else if (inputFirstTag.equalsIgnoreCase("Steal")) {
System.out.println("Steal Increased by 25!");
steal = steal + 25;
System.out.println("Steal: " + steal);
} else if (inputFirstTag.equalsIgnoreCase("Traps")) {
System.out.println("Traps Increased by 25!");
traps = traps + 25;
System.out.println("Traps: " + traps);
} else if (inputFirstTag.equalsIgnoreCase("Science")) {
System.out.println("Science Increased by 25!");
science = science + 25;
System.out.println("Science: " + science);
} else if (inputFirstTag.equalsIgnoreCase("Repair")) {
System.out.println("Repair Increased by 25!");
repair = repair + 25;
System.out.println("Repair: " + repair);
} else if (inputFirstTag.equalsIgnoreCase("Speech")) {
System.out.println("Speech Increased by 25!");
speech = speech + 25;
System.out.println("Speech: " + speech);
} else if (inputFirstTag.equalsIgnoreCase("Barter")) {
System.out.println("Barter Increased by 25!");
barter = barter + 25;
System.out.println("Barter: " + barter);
} else if (inputFirstTag.equalsIgnoreCase("Gambling")) {
System.out.println("Gambling Increased by 25!");
gambling = gambling + 25;
System.out.println("Gambling: " + gambling);
} else if (inputFirstTag.equalsIgnoreCase("Outdoors")) {
System.out.println("Outdoors Increased by 25!");
outdoors = outdoors + 25;
System.out.println("Outdoors: " + outdoors);
}
System.out.println();
System.out.println("You have 2 Skills to Tag.");
System.out.println("What skill would you like to Tag? (+20)");
System.out.print("Small Guns, Big Guns, Energy Weapons, Unarmed, Melee Weapons, Throwing, ");
System.out.print("First Aid, Doctor, Sneak, Lockpick, Steal, Traps, Science, Repair, ");
System.out.println("Speech, Barter, Gambling or Outdoors?");
Scanner scanSecondTag = new Scanner(System.in);
String inputSecondTag = null;
while (scanSecondTag.hasNextLine()) {
inputSecondTag = scanSecondTag.nextLine();
if (inputSecondTag.equalsIgnoreCase("Small Guns") || inputSecondTag.equalsIgnoreCase("Big Guns") || inputSecondTag.equalsIgnoreCase("Energy Weapons") || inputSecondTag.equalsIgnoreCase("Unarmed") || inputSecondTag.equalsIgnoreCase("Melee Weapons") || inputSecondTag.equalsIgnoreCase("Throwing") || inputSecondTag.equalsIgnoreCase("First Aid") || inputSecondTag.equalsIgnoreCase("Doctor") ||
inputSecondTag.equalsIgnoreCase("Sneak") || inputSecondTag.equalsIgnoreCase("Lockpick") ||
inputSecondTag.equalsIgnoreCase("Steal") || inputSecondTag.equalsIgnoreCase("Traps") ||
inputSecondTag.equalsIgnoreCase("Science") || inputSecondTag.equalsIgnoreCase("Repair") ||
inputSecondTag.equalsIgnoreCase("Speech") || inputSecondTag.equalsIgnoreCase("Barter") ||
inputSecondTag.equalsIgnoreCase("Gambling") || inputSecondTag.equalsIgnoreCase("Outdoors"))
break;
else System.out.println("Please choose Small Guns, Big Guns, Energy Weapons, " +
"Unarmed, Melee Weapons, Throwing, First Aid, Doctor, " +
"Sneak, Lockpick, Steal, Traps, Science, Repair, " +
"Speech, Barter, Gambling or Outdoors?");
}
if (inputSecondTag.equalsIgnoreCase("Small Guns")) {
System.out.println("Small Guns Increased by 20!");
smallGuns = smallGuns + 20;
System.out.println("Small Guns: " + smallGuns);
} else if (inputSecondTag.equalsIgnoreCase("Big Guns")) {
System.out.println("Big Guns Increased by 20!");
bigGuns = bigGuns + 20;
System.out.println("Big Guns: " + bigGuns);
} else if (inputSecondTag.equalsIgnoreCase("Energy Weapons")) {
System.out.println("Energy Weapons Increased by 20!");
energyWeapons = energyWeapons + 20;
System.out.println("Energy Weapons: " + energyWeapons);
} else if (inputSecondTag.equalsIgnoreCase("Unarmed")) {
System.out.println("Unarmed Increased by 20!");
unarmed = unarmed + 20;
System.out.println("Unarmed: " + unarmed);
} else if (inputSecondTag.equalsIgnoreCase("Melee Weapons")) {
System.out.println("Melee Weapons Increased by 20!");
meleeWeapons = meleeWeapons+ 20;
System.out.println("Melee Weapons: " + meleeWeapons);
} else if (inputSecondTag.equalsIgnoreCase("Throwing")) {
System.out.println("Throwing Increased by 20!");
throwing = throwing + 20;
System.out.println("Throwing: " + throwing);
} else if (inputSecondTag.equalsIgnoreCase("First Aid")) {
System.out.println("First Aid Increased by 20!");
firstAid = firstAid+ 20;
System.out.println("First Aid: " + firstAid);
} else if (inputSecondTag.equalsIgnoreCase("Doctor")) {
System.out.println("Doctor Increased by 20!");
doctor = doctor + 20;
System.out.println("Doctor: " + doctor);
} else if (inputSecondTag.equalsIgnoreCase("Sneak")) {
System.out.println("Sneak Increased by 20!");
sneak = sneak + 20;
System.out.println("Sneak: " + sneak);
} else if (inputSecondTag.equalsIgnoreCase("Lockpick")) {
System.out.println("Lockpick Increased by 20!");
lockpick = lockpick + 20;
System.out.println("Lockpick: " + lockpick);
} else if (inputSecondTag.equalsIgnoreCase("Steal")) {
System.out.println("Steal Increased by 20!");
steal = steal + 20;
System.out.println("Steal: " + steal);
} else if (inputSecondTag.equalsIgnoreCase("Traps")) {
System.out.println("Traps Increased by 20!");
traps = traps + 20;
System.out.println("Traps: " + traps);
} else if (inputSecondTag.equalsIgnoreCase("Science")) {
System.out.println("Science Increased by 20!");
science = science + 20;
System.out.println("Science: " + science);
} else if (inputSecondTag.equalsIgnoreCase("Repair")) {
System.out.println("Repair Increased by 20!");
repair = repair + 20;
System.out.println("Repair: " + repair);
} else if (inputSecondTag.equalsIgnoreCase("Speech")) {
System.out.println("Speech Increased by 20!");
speech = speech + 20;
System.out.println("Speech: " + speech);
} else if (inputSecondTag.equalsIgnoreCase("Barter")) {
System.out.println("Barter Increased by 20!");
barter = barter + 20;
System.out.println("Barter: " + barter);
} else if (inputSecondTag.equalsIgnoreCase("Gambling")) {
System.out.println("Gambling Increased by 20!");
gambling = gambling + 20;
System.out.println("Gambling: " + gambling);
} else if (inputSecondTag.equalsIgnoreCase("Outdoors")) {
System.out.println("Outdoors Increased by 20!");
outdoors = outdoors + 20;
System.out.println("Outdoors: " + outdoors);
}
System.out.println();
System.out.println("You have 1 Skill to Tag.");
System.out.println("What skill would you like to Tag? (+15)");
System.out.print("Small Guns, Big Guns, Energy Weapons, Unarmed, Melee Weapons, Throwing, ");
System.out.print("First Aid, Doctor, Sneak, Lockpick, Steal, Traps, Science, Repair, ");
System.out.println("Speech, Barter, Gambling or Outdoors?");
Scanner scanThirdTag = new Scanner(System.in);
String inputThirdTag = null;
while (scanThirdTag.hasNextLine()) {
inputThirdTag = scanThirdTag.nextLine();
if (inputThirdTag.equalsIgnoreCase("Small Guns") || inputThirdTag.equalsIgnoreCase("Big Guns") || inputThirdTag.equalsIgnoreCase("Energy Weapons") || inputThirdTag.equalsIgnoreCase("Unarmed") || inputThirdTag.equalsIgnoreCase("Melee Weapons") || inputThirdTag.equalsIgnoreCase("Throwing") || inputThirdTag.equalsIgnoreCase("First Aid") || inputThirdTag.equalsIgnoreCase("Doctor") ||
inputThirdTag.equalsIgnoreCase("Sneak") || inputThirdTag.equalsIgnoreCase("Lockpick") ||
inputThirdTag.equalsIgnoreCase("Steal") || inputThirdTag.equalsIgnoreCase("Traps") ||
inputThirdTag.equalsIgnoreCase("Science") || inputThirdTag.equalsIgnoreCase("Repair") ||
inputThirdTag.equalsIgnoreCase("Speech") || inputThirdTag.equalsIgnoreCase("Barter") ||
inputThirdTag.equalsIgnoreCase("Gambling") || inputThirdTag.equalsIgnoreCase("Outdoors"))
break;
else System.out.println("Please choose Small Guns, Big Guns, Energy Weapons, " +
"Unarmed, Melee Weapons, Throwing, First Aid, Doctor, " +
"Sneak, Lockpick, Steal, Traps, Science, Repair, " +
"Speech, Barter, Gambling or Outdoors?");
}
if (inputThirdTag.equalsIgnoreCase("Small Guns")) {
System.out.println("Small Guns Increased by 25!");
smallGuns = smallGuns + 25;
System.out.println("Small Guns: " + smallGuns);
} else if (inputThirdTag.equalsIgnoreCase("Big Guns")) {
System.out.println("Big Guns Increased by 25!");
bigGuns = bigGuns + 25;
System.out.println("Big Guns: " + bigGuns);
} else if (inputThirdTag.equalsIgnoreCase("Energy Weapons")) {
System.out.println("Energy Weapons Increased by 25!");
energyWeapons = energyWeapons + 25;
System.out.println("Energy Weapons: " + energyWeapons);
} else if (inputThirdTag.equalsIgnoreCase("Unarmed")) {
System.out.println("Unarmed Increased by 25!");
unarmed = unarmed + 25;
System.out.println("Unarmed: " + unarmed);
} else if (inputThirdTag.equalsIgnoreCase("Melee Weapons")) {
System.out.println("Melee Weapons Increased by 25!");
meleeWeapons = meleeWeapons+ 25;
System.out.println("Melee Weapons: " + meleeWeapons);
} else if (inputThirdTag.equalsIgnoreCase("Throwing")) {
System.out.println("Throwing Increased by 25!");
throwing = throwing + 25;
System.out.println("Throwing: " + throwing);
} else if (inputThirdTag.equalsIgnoreCase("First Aid")) {
System.out.println("First Aid Increased by 25!");
firstAid = firstAid+ 25;
System.out.println("First Aid: " + firstAid);
} else if (inputThirdTag.equalsIgnoreCase("Doctor")) {
System.out.println("Doctor Increased by 25!");
doctor = doctor + 25;
System.out.println("Doctor: " + doctor);
} else if (inputThirdTag.equalsIgnoreCase("Sneak")) {
System.out.println("Sneak Increased by 25!");
sneak = sneak + 25;
System.out.println("Sneak: " + sneak);
} else if (inputThirdTag.equalsIgnoreCase("Lockpick")) {
System.out.println("Lockpick Increased by 25!");
lockpick = lockpick + 25;
System.out.println("Lockpick: " + lockpick);
} else if (inputThirdTag.equalsIgnoreCase("Steal")) {
System.out.println("Steal Increased by 25!");
steal = steal + 25;
System.out.println("Steal: " + steal);
} else if (inputThirdTag.equalsIgnoreCase("Traps")) {
System.out.println("Traps Increased by 25!");
traps = traps + 25;
System.out.println("Traps: " + traps);
} else if (inputThirdTag.equalsIgnoreCase("Science")) {
System.out.println("Science Increased by 25!");
science = science + 25;
System.out.println("Science: " + science);
} else if (inputThirdTag.equalsIgnoreCase("Repair")) {
System.out.println("Repair Increased by 25!");
repair = repair + 25;
System.out.println("Repair: " + repair);
} else if (inputThirdTag.equalsIgnoreCase("Speech")) {
System.out.println("Speech Increased by 25!");
speech = speech + 25;
System.out.println("Speech: " + speech);
} else if (inputThirdTag.equalsIgnoreCase("Barter")) {
System.out.println("Barter Increased by 25!");
barter = barter + 25;
System.out.println("Barter: " + barter);
} else if (inputThirdTag.equalsIgnoreCase("Gambling")) {
System.out.println("Gambling Increased by 25!");
gambling = gambling + 25;
System.out.println("Gambling: " + gambling);
} else if (inputThirdTag.equalsIgnoreCase("Outdoors")) {
System.out.println("Outdoors Increased by 25!");
outdoors = outdoors + 25;
System.out.println("Outdoors: " + outdoors);
}
System.out.println();
System.out.println(enter);
pressEnter.nextLine();
System.out.println("Your new Skills are:");
System.out.println("Small Guns: " + smallGuns);
System.out.println("Big Guns: " + bigGuns);
System.out.println("Energy Weapons: " + energyWeapons);
System.out.println("Unarmed: " + unarmed);
System.out.println("Melee Weapons: " + meleeWeapons);
System.out.println("Throwing: " + throwing);
System.out.println("First Aid: " + firstAid);
System.out.println("Doctor: " + doctor);
System.out.println("Sneak: " + sneak);
System.out.println("Lockpick: " + lockpick);
System.out.println("Steal: " + steal);
System.out.println("Traps: " + traps);
System.out.println("Science: " + science);
System.out.println("Repair: " + repair);
System.out.println("Speech: " + speech);
System.out.println("Barter: " + barter);
System.out.println("Gambling: " + gambling);
System.out.println("Outdoors: " + outdoors);
System.out.println();
System.out.println(enter);
pressEnter.nextLine();
}
}
Without overwhelming you with a long block of code:
You can create a Set data structure of "taggedSkills." Initially, this Set is empty. If the user tags a skill, the skill is added to the set.
Then if the user's second tag is the same skill as the first tag, the program can check the Set and ask the user to try again with a new skill, instead of adding a duplicate tag. Same with the third skill.
This isn't perfect, and I didn't feel like typing all the choices, but this might help you.
import java.util.ArrayList;
import java.util.Scanner;
public class Meow {
public static void main(String[] args){
ArrayList<String> choices = new ArrayList<String>();
choices.add("Barter");
choices.add("Repair");
choices.add("Speech");
choices.add("Potato");
int[] numbers = new int[choices.size()];
System.out.println("You have 3 Skills to Tag.");
System.out.println("What skill would you like to Tag? (+25)");
System.out.println("Barter, Repair, Speech, Potato?");
Scanner scanner = new Scanner(System.in);
String inputFirstTag = "";
while (scanner.hasNextLine()) {
inputFirstTag = scanner.nextLine();
if ((choices.contains(inputFirstTag))){
if(numbers[choices.indexOf(inputFirstTag)] == 0){
numbers[choices.indexOf(inputFirstTag)] += 25;
break;
}
else {System.out.println("Please don't choose duplicates");}
}
else {System.out.println("Please choose Repair, Speech, Barter.");}
}
System.out.println(inputFirstTag + " + 25");
System.out.println();
System.out.println("You have 2 Skills to Tag.");
System.out.println("What skill would you like to Tag? (+25)");
System.out.println("Barter, Repair, Speech, Potato?");
String inputSecondTag = "";
while (scanner.hasNextLine()) {
inputSecondTag = scanner.nextLine();
if ((choices.contains(inputSecondTag))){
if(numbers[choices.indexOf(inputSecondTag)] == 0){
numbers[choices.indexOf(inputSecondTag)] += 25;
break;
}
else {System.out.println("Please don't choose duplicates");}
}
else {System.out.println("Please choose Repair, Speech, Barter.");}
}
System.out.println(inputSecondTag + " + 25");
System.out.println();
System.out.println();
System.out.println("You have 1 Skill to Tag.");
System.out.println("What skill would you like to Tag? (+25)");
System.out.println("Barter, Repair, Speech, Potato?");
String inputThirdTag = "";
while (scanner.hasNextLine()) {
inputThirdTag = scanner.nextLine();
if ((choices.contains(inputThirdTag))){
if(numbers[choices.indexOf(inputThirdTag)] == 0){
numbers[choices.indexOf(inputThirdTag)] += 25;
break;
}
else {System.out.println("Please don't choose duplicates");}
}
else {System.out.println("Please choose Repair, Speech, Barter.");}
}
System.out.println(inputThirdTag + " + 25");
System.out.println();
for (int i = 0; i < choices.size(); i++){
System.out.println(choices.get(i) + " : " + numbers[i]);
}
}
}
My version.
import java.util.*;
public class SOFQuestion {
private static Skill getSkillFromUser() {
final Scanner scanFirstTag = new Scanner(System.in);
while (scanFirstTag.hasNextLine()) {
final String inputFirstTag = scanFirstTag.nextLine();
for (Skill skill : Skill.values()) {
if (skill.getSkillName().equalsIgnoreCase(inputFirstTag)) {
if (Skill.alreadyAddedSkills.contains(skill)) {
System.out.println("Sorry, but you have are already increased this skill.");
break;
}
return skill;
}
}
System.out.println("Please choose " + Skill.getSkillsList() + "?");
}
return null;
}
enum Skill {
SMALL_GUNS("Small Guns"),
THROWING("Throwing"),
FIRST_AID("First Aid"),
DOCTOR("Doctor");
private final String skillName;
private int skillPower;
private static List<Skill> alreadyAddedSkills = new ArrayList<>();
Skill(String skillName) {
this.skillName = skillName;
}
public String getSkillName() {
return skillName;
}
public int getSkillPower() {
return skillPower;
}
public void increaseSkillPower(int skillPower) {
this.skillPower += skillPower;
alreadyAddedSkills.add(this);
System.out.println("You increase " + getSkillName() + " skill to " + getSkillPower());
}
public static String getSkillsList() {
final StringBuffer sb = new StringBuffer();
for (Skill s : Skill.values()) {
if (alreadyAddedSkills.contains(s)) continue;
sb.append(s.getSkillName()).append(", ");
}
if (sb.length() > 2) {
return sb.toString().substring(0, sb.length() - 2);
}
return "EMPTY SKILL LIST";
}
}
public static void main(String... args) {
int skillLimit = 3;
int defPower = 10;
while (skillLimit != 0) {
final int powerToAdd = skillLimit * 5 + defPower;
System.out.println("You have " + skillLimit-- + " Skills to Tag.");
System.out.println("What skill would you like to Tag? " + "(+" + powerToAdd + ")");
System.out.println(Skill.getSkillsList() + "?");
final Skill skill = getSkillFromUser();
if (skill == null) return;
skill.increaseSkillPower(powerToAdd);
}
System.out.println("Your new Skills are:");
for (Skill skill : Skill.values()) {
System.out.println(skill.getSkillName() + ": " + skill.getSkillPower());
}
}
}