I consistently get different output than I expected. However, the code very apparently is running correctly, providing the desired output elements, with some additional redundant elements like an appetizer and main course.
Please, see the attached code, and output when run on the Eclipse IDE.
//class FoodOrder
class Main {
// Method to display menu
static void display(String menu[]) {
for (int i = 0; i < menu.length; i++) {
System.out.println(" " + i + " - " + menu[i]);
}
System.out.print("Enter the number for your selection: ");
}
// main method
public static void main(String[] args) {
// menu lists:
String mainMenu[] = { "Nothing", "Appetizer", "Main Course", "Dessert" };
String dessertMenu[] = { "Nothing", "Baklava", "Rice Pudding", "Chocolate Cake" };
String appetizerMenu[] = { "Nothing", "Oysters", "Grilled Octopus", "Hummus" };
String mainCourseMenu[] = { "Nothing", "Steak", "Chicken", "Fish", "Vegetarian" };
String toppingsMenu[] = { "Nothing", "Olive Oil", "Paprika", "Olives" };
String list = "";
// create an instance of Scanner class
Scanner sc = new Scanner(System.in);
boolean valid;
System.out.println("Welcome to the food festival!");
String ans;
do {
System.out.print("Would you like to place an order? ");
ans = sc.next();
valid = ((ans.equals("YES")) || ans.equals("YEs") || ans.equals("Yes") || ans.equals("yES")
|| ans.equals("yeS") || ans.equals("yEs") || ans.equals("YeS") || ans.equals("yes")
|| (ans.equals("NO")) || ans.equals("No") || ans.equals("nO") || ans.equals("no"));
} while (!valid);
if ((ans.equals("NO")) || (ans.equals("No")) || (ans.equals("nO")) || (ans.equals("no"))) {
System.out.println("Thank you for stopping by, maybe next time you’ll sample our menu.");
return;
}
if (ans.equals("YES") || ans.equals("YEs") || ans.equals("Yes") || ans.equals("yES") || ans.equals("yeS")
|| ans.equals("yEs") || ans.equals("YeS") || ans.equals("yes")) {
System.out.print("What is your name for the order? ");
String name = sc.next();
System.out.println("");
int m, n, k;
while (true) {
Menu:
// proocess to select the menu items
System.out.println("Select from menu, " + name);
display(mainMenu);
n = sc.nextInt();
if (n == 0)
break;
if (n == 1) {
while (true) {
System.out.println("");
System.out.println("Appetizer Menu:");
display(appetizerMenu);
m = sc.nextInt();
list = list + "Appetizer:[" + appetizerMenu[m] + ": ";
if (m == 0) {
break;
}
while (true) {
System.out.println("");
System.out.println("Toppings Menu:");
display(toppingsMenu);
k = sc.nextInt();
if (k == 0)
break;
list = list + toppingsMenu[k] + " ";
}
list = list + "]\n";
}
}
if (n == 2) {
while (true) {
System.out.println("");
System.out.println("Main Course Menu:");
display(mainCourseMenu);
m = sc.nextInt();
list = list + "Main Course :[" + mainCourseMenu[m] + ": ";
if (m == 0) {
break;
}
while (true) {
System.out.println("");
System.out.println("Toppings Menu:");
display(toppingsMenu);
k = sc.nextInt();
if (k == 0)
break;
list = list + toppingsMenu[k] + "]\n";
}
}
}
if (n == 3) {
while (true) {
System.out.println("");
System.out.println("Dessert Menu:");
display(dessertMenu);
m = sc.nextInt();
list = list + "Main Course :[" + dessertMenu[m] + ": ";
if (m == 0) {
break;
}
while (true) {
System.out.println("");
System.out.println("Toppings Menu:");
display(toppingsMenu);
k = sc.nextInt();
if (k == 0)
break;
list = list + toppingsMenu[k] + "]\n";
}
}
}
} // end while loop
System.out.println("Here is your order " + name);
System.out.println(list);
System.out.println("Enjoy your meal!");
} // end of if
}// main method
}// end of class
This is the output I am supposed to get:
Here is your order Superman:
Appetizer: [ Hummus: Olives, Olive Oil ]
Main Course: [ Fish: Paprika ]
Dessert: [ Baklava: ]
Enjoy your meal!
Unfortunately, I am getting this output:
Here is your order Superman:
Appetizer:[Hummus: Olives Olive Oil ]
Appetizer:[Nothing: Main Course :[Fish: Paprika]
Main Course :[Nothing: Main Course :[Baklava:
Main Course :[Nothing:
Enjoy your meal!
I did some assumptions, and made the implementation.
There are some errors with your implementation:
You need to check if the list is empty or not to add ","
You are first adding the selection before checking if it was exit option
I think this could be the code that you are looking for:
public static String collectToppings(Scanner sc, String[] toppingsMenu) {
String list = "";
while (true) {
System.out.println("");
System.out.println("Toppings Menu:");
display(toppingsMenu);
int k = sc.nextInt();
if (k == 0)
break;
if (list.length() == 0) {
list = toppingsMenu[k];
} else {
list += ", " + toppingsMenu[k];
}
}
if (list.length() > 0) {
return list + " ]\r\n\r\n";
} else {
return "]\r\n\r\n";
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = "Superman";
int m, n;
String[] mainMenu = new String[] { "Exit", "Appetizer Menu", "Toppings Menu", "Main Course Menu" };
String[] appetizerMenu = new String[] {"Exit", "Hummus"};
String[] mainCourseMenu = new String[] {"Exit", "Fish"};
String[] dessertMenu = new String[] {"Exit", "Baklava"};
String list ="";
while (true) {
// proocess to select the menu items
System.out.println("Select from menu, " + name);
display(mainMenu);
n = sc.nextInt();
if (n == 0)
break;
if (n == 1) {
while (true) {
System.out.println("");
System.out.println("Appetizer Menu:");
display(appetizerMenu);
m = sc.nextInt();
if (m == 0) {
break;
}
list = list + "Appetizer: [ " + appetizerMenu[m] + ": ";
String[] toppingsMenu = new String[] {"Exit", "Olives", "Olive Oil"};
list += collectToppings(sc, toppingsMenu);
}
}
if (n == 2) {
while (true) {
display(mainCourseMenu);
m = sc.nextInt();
if (m == 0) {
break;
}
list = list + "Main Course: [ " + mainCourseMenu[m] + ": ";
String[] toppingsMenu = new String[] {"Exit", "Paprika"};
list += collectToppings(sc, toppingsMenu) ;
}
}
if (n == 3) {
while (true) {
display(dessertMenu);
m = sc.nextInt();
if (m == 0) {
break;
}
list = list + "Dessert: [ " + dessertMenu[m] + ": ";
String[] toppingsMenu = new String[] {"Exit", "...", "..."};
list += collectToppings(sc, toppingsMenu);
}
}
}
System.out.println("Here is your order " + name + ":\r\n");
System.out.println(list);
System.out.println("Enjoy your meal!");
}
private static void display(String[] menu) {
int i = 0;
for (String s : menu) {
System.out.println(i++ + ". " + s);
}
}
Related
I am trying to use two int variables from other classes in another class and then add them together into another variable and print the result. When I try this though, I always get a result of zero like the values are not being brought over into the new class and I can't figure out what the problem is.
Here is some example code:
class1
public static int finished = (match2.totalpoints + match3.iq);
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
System.out.println("THIS IS YOUR OWN EXCLUSIVE IQ TEST OR MEMORY QUIZ OR WHATEVER....");
System.out.println("");
System.out.println("When taking the quiztest you have only two seconds before making each guess");
match2 m = new match2();
System.out.println("THAT MEANS ACCORDING TO YOUR QUIZTEST YOU'VE GOT AN IQ OF " + finished + " POINTS");
}
}
EDIT: class2
public class match2 {
public static int totalpoints;
int a;
int b;
int c;
int d;
int e;
int f;
String guess;
String group;
String countdown[] = {
"3...",
"2...",
"1...",
""
};
String memorize[] = {
""
};
public match2() throws InterruptedException
{
int x = set2();
int y = set3();
int z = set4();
total(x, y, z);
System.out.println("For the next part of your IQ ASSesment\njust type back the words in CAPSLOCK in CAPSLOCK");
System.out.println("");
match3 n = new match3();
}
public void set1() throws InterruptedException
{
//Scanner s = new Scanner(System.in);
for (int i = 0; i < countdown.length; i++)
{
Thread.sleep(750);
System.out.println(countdown[i]);
}
}
public int set2() throws InterruptedException
{
Random r = new Random();
Scanner s = new Scanner(System.in);
System.out.println("press ENTER for your first set...");
s.nextLine();
set1();
int rv = 0;
a = r.nextInt(9) + 1;
b = r.nextInt(9) + 1;
c = r.nextInt(9) + 1;
d = r.nextInt(9) + 1;
group = "" + a + b + c + d;
System.out.println(group);
for (int i = 0; i < memorize.length; i++)
{
Thread.sleep(1500);
System.out.println(memorize[i]);
}
System.out.println("\n\n\n\n\n\n\n\n\n\n");
guess = "" + s.nextLine();
if(guess.equals(group))
{
System.out.println("nice +1 bruh");
rv = 1;
}
else if(!guess.equals(group))
{
System.out.println("almost");
}
return rv;
}
public int set3() throws InterruptedException
{
Random r = new Random();
Scanner s = new Scanner(System.in);
System.out.println("");
System.out.println("press ENTER for your next set...");
s.nextLine();
set1();
int rv = 0;
a = r.nextInt(9) + 1;
b = r.nextInt(9) + 1;
c = r.nextInt(9) + 1;
d = r.nextInt(9) + 1;
f = r.nextInt(9) + 1;
group = "" + a + b + c + d + f;
System.out.println(group);
for (int i = 0; i < memorize.length; i++)
{
Thread.sleep(1500);
System.out.println(memorize[i]);
}
System.out.println("\n\n\n\n\n\n\n\n\n\n");
guess = s.nextLine();
if(group.equals(guess))
{
rv = 1;
System.out.println("good");
}
else if(!guess.equals(group))
{
System.out.println("almost");
}
return rv;
}
public int set4() throws InterruptedException
{
Random r = new Random();
Scanner s = new Scanner(System.in);
System.out.println("");
System.out.println("press ENTER for your final set...");
s.nextLine();
set1();
int rv = 0;
a = r.nextInt(9) + 1;
b = r.nextInt(9) + 1;
c = r.nextInt(9) + 1;
d = r.nextInt(9) + 1;
e = r.nextInt(9) + 1;
f = r.nextInt(9) + 1;
group = "" + a + b + c + d + f + e;
System.out.println(group);
System.out.println("");
for (int i = 0; i < memorize.length; i++)
{
Thread.sleep(1500);
System.out.println(memorize[i]);
}
System.out.println("\n\n\n\n\n\n\n\n\n\n");
guess = "" + s.nextLine();
if(group.equals(guess))
{
rv = 1;
System.out.println("great");
}
else if(!group.equals(guess))
{
System.out.println("eeeh buzer sound");
}
return rv;
}
public int total(int x, int y, int z)
{
System.out.println("");
int totalpoints = (x + y + z);
if(totalpoints == 3)
{
System.out.println("YOU GOT THEM ALL");
}
if(totalpoints <= 2 && totalpoints >= 1)
{
System.out.println("YOU MISSED A TOTAL OF " + (3 - totalpoints));
}
if(totalpoints == 0)
{
System.out.println("HA! YOU MISSED THEM ALL");
}
return totalpoints;
}
}
EDIT: class3
public class match3 {
public static int iq;
String countupdown [] = {
"READY...",
"SET.....",
""
};
String memorize [] = {
""
};
public match3() throws InterruptedException
{
int mem1 = memory1();
int mem2 = memory2();
int mem3 = memory3();
totalMemory(mem1, mem2, mem3);
}
public void methodCountdown() throws InterruptedException
{
for(int i = 0; i < countupdown.length; i++)
{
Thread.sleep(1000);
System.out.println(countupdown[i]);
}
}
public int memory1() throws InterruptedException
{
int rv = 1;
Scanner s = new Scanner(System.in);
System.out.println("Press ENTER when ready");
s.nextLine();
methodCountdown();
String a = word1();
String b = word2();
System.out.println("The " + a + " ate the " + b);
String wordgroup = "" + a + " " + b;
for (int i = 0; i < memorize.length; i++)
{
Thread.sleep(1500);
System.out.println(memorize[i]);
}
System.out.println("\n\n\n\n\n\n\n\n\n\n");
String wordguess = "" + s.nextLine();
if(wordgroup.equals(wordguess))
{
System.out.println("awesome cock muncher a match");
rv = 1;
}
else if(!wordgroup.equals(wordguess))
{
System.out.println("nope");
}
return rv;
}
public int memory2() throws InterruptedException
{
int rv = 0;
Scanner s = new Scanner(System.in);
System.out.println("");
System.out.println("Press ENTER for your next set");
s.nextLine();
methodCountdown();
String a = word1();
String c = word3();
System.out.println("The " + a + " drove the " + c);
String wordgroup = "" + a + " " + c;
for (int i = 0; i < memorize.length; i++)
{
Thread.sleep(1500);
System.out.println(memorize[i]);
}
System.out.println("\n\n\n\n\n\n\n\n\n\n");
String wordguess = "" + s.nextLine();
if(wordgroup.equals(wordguess))
{
System.out.println("awesome cock muncher a match");
rv = 1;
}
else if(!wordgroup.equals(wordguess))
{
System.out.println("nope");
}
return rv;
}
public int memory3() throws InterruptedException
{
int rv = 0;
Scanner s = new Scanner(System.in);
System.out.println("");
System.out.println("Press ENTER for your next set");
s.nextLine();
methodCountdown();
String a = word1();
String d = word4();
System.out.println("The " + a + " visited the " + d);
String wordgroup = "" + a + " " + d;
for (int i = 0; i < memorize.length; i++)
{
Thread.sleep(1500);
System.out.println(memorize[i]);
}
System.out.println("\n\n\n\n\n\n\n\n\n\n");
String wordguess = "" + s.nextLine();
if(wordgroup.equals(wordguess))
{
System.out.println("awesome cock muncher a match");
rv = 1;
}
else if(!wordgroup.equals(wordguess))
{
System.out.println("nope");
}
return rv;
}
public static String word1()
{
String word = "";
Random r = new Random();
int cv = r.nextInt(3) + 1;
if(cv == 1)
{
word = "DOG";
}
else if(cv == 2)
{
word = "CAT";
}
else if(cv == 3)
{
word = "BIRD";
}
return word;
}
public static String word2()
{
String word = "";
Random r = new Random();
int cv = r.nextInt(3) + 1;
if(cv == 1)
{
word = "FOOD";
}
else if(cv == 2)
{
word = "MUD";
}
else if(cv == 3)
{
word = "GRAINS";
}
return word;
}
public static String word3()
{
String word = "";
Random r = new Random();
int cv = r.nextInt(3) + 1;
if(cv == 1)
{
word = "TRAM";
}
else if(cv == 2)
{
word = "BUS";
}
else if(cv == 3)
{
word = "BICYCLE";
}
return word;
}
public static String word4()
{
String word = "";
Random r = new Random();
int cv = r.nextInt(3) + 1;
if(cv == 1)
{
word = "MALL";
}
else if(cv == 2)
{
word = "PARK";
}
else if(cv == 3)
{
word = "POOL";
}
return word;
}
public void totalMemory(int mem1, int mem2, int mem3)
{
int iq = (mem1 + mem2 + mem3);
System.out.println("");
if(iq == 3)
{
System.out.println("YOU GOT THEM ALL");
}
else if(iq <= 2 || iq >= 1)
{
System.out.println("YOU MISSED A TOTAL OF " + (3 - iq));
}
else if(iq == 0)
{
System.out.println("HA! YOU MISSED THEM ALL");
}
}
}
total points is a variable from match2 class and iq from match3 class. Any help with any methods I could use to make this happen would be much appreciated. Thank You
Well ... besides the fact you are not following any code convention, Like class names should start with a capital letter and public static final fields (like totalpoints and iq) should be all Uppercase (code conventions, you are not sharring match2 and match3 codes, without it we can't understand what is happening inside those classes.
But you can do a simple test and assign a value to match2.totalpoints and match3.iq and you are going to see the summing of these two values being printed by the last system.out you put.
good luck and good Java studies!
Are the int variables you're trying to use inside child classes of your main parent class? Did you extend the child classes in your main parent class?
Class #1:
public int firstVar(int someNum) {
//code here
return someNum;
}
Class #2:
public int secondVar(int otherNum) {
//code here
return otherNum;
}
Class #3 Class with Main Method -
public class mainClass extends class#1; //etc
//code here and finally print out the finished number
You could try extending one of the classes that has an int you need into another one of the classes with the other int you need and then just extending that second class into your main, OR you could try completely redefining your classes and just placing all the ints you need into one separate class and then extending that single one into your main.
So I finally got it to work. The problem, I guess, was that I was trying to add together the variables from the other classes(match2 and match3) outside the main function within the class(match1) I was trying to add them together. All I did was move the expression adding the variables together from the top to inside the main function like this:
public static int finished;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
System.out.println("THIS IS YOUR OWN EXCLUSIVE IQ TEST OR MEMORY QUIZ OR WHATEVER....");
System.out.println("");
System.out.println("When taking the quiztest you have only two seconds before making each guess");
match2 m = new match2();
finished = (match2.totalpoints + match3.iq);
System.out.println("THAT MEANS ACCORDING TO YOUR QUIZTEST YOU'VE GOT AN IQ OF " + finished + " POINTS");
}
}
Thanks for all the help from everyone.
Smth. like this. You have lot's of code duplication
public class MatchRunner {
public static void main(String... args) throws InterruptedException {
new MatchRunner().start();
}
public void start() throws InterruptedException {
System.out.println("THIS IS YOUR OWN EXCLUSIVE IQ TEST OR MEMORY QUIZ OR WHATEVER....");
System.out.println();
System.out.println("When taking the quiztest you have only two seconds before making each guess");
int totalPoints = new Match2().getTotalPoints();
System.out.println("For the next part of your IQ Assesment");
System.out.println("just type back the words in CAPSLOCK in CAPSLOCK");
int iq = new Match3().getIQ();
System.out.println("THAT MEANS ACCORDING TO YOUR QUIZTEST YOU'VE GOT AN IQ OF " + (totalPoints + iq) + " POINTS");
}
}
public class Match2 {
public int getTotalPoints() throws InterruptedException {
try (Scanner scan = new Scanner(System.in)) {
int totalPoints = calc(scan, "first", "nice +1 try", "almost");
totalPoints += calc(scan, "next", "good", "almost");
totalPoints += calc(scan, "final", "great", "eeeh buzer sound");
printTotalPoints(totalPoints);
return totalPoints;
}
}
private static void countdown() throws InterruptedException {
for (int i = 5; i > 0; i--) {
Thread.sleep(750);
System.out.println(i + "...");
}
}
private static int calc(Scanner scan, String strSet, String strEqual, String strNotEqual) throws InterruptedException {
System.out.println("press ENTER for your " + strSet + " set...");
Random random = new Random();
scan.nextLine();
countdown();
int sum = 0;
for (int i = 0; i < 4; i++)
sum += random.nextInt(9) + 1;
System.out.println(sum);
for (int i = 0; i < 10; i++)
System.out.println();
int guess = scan.nextInt();
System.out.println(guess == sum ? strEqual : strNotEqual);
return guess == sum ? 1 : 0;
}
private static void printTotalPoints(int totalPoints) {
System.out.println();
if (totalPoints == 3)
System.out.println("YOU GOT THEM ALL");
else if (totalPoints <= 2 && totalPoints >= 1)
System.out.println("YOU MISSED A TOTAL OF " + (3 - totalPoints));
else if (totalPoints == 0)
System.out.println("HA! YOU MISSED THEM ALL");
}
}
public class Match3 {
public int getIQ() throws InterruptedException {
try (Scanner scan = new Scanner(System.in)) {
Random random = new Random();
Supplier<String> getWord1 = () -> getWord(random, "DOG", "CAT", "BIRD");
Supplier<String> getWord2 = () -> getWord(random, "FOOD", "MUD", "GRAINS");
Supplier<String> getWord3 = () -> getWord(random, "TRAM", "BUS", "BICYCLE");
Supplier<String> getWord4 = () -> getWord(random, "MALL", "PARK", "POOL");
int iq = calc(scan, getWord1, getWord2, "ate the");
iq += calc(scan, getWord1, getWord3, "drove the");
iq += calc(scan, getWord1, getWord4, "visited the");
printIq(iq);
return iq;
}
}
private static void countdown() throws InterruptedException {
System.out.println("READY...");
Thread.sleep(1000);
System.out.println("SET...");
Thread.sleep(1000);
}
public int calc(Scanner scan, Supplier<String> wordOne, Supplier<String> wordTwo, String strMsq) throws InterruptedException {
System.out.println("Press ENTER when ready");
scan.nextLine();
countdown();
String a = wordOne.get();
String b = wordTwo.get();
System.out.println("The " + a + ' ' + strMsq + ' ' + b);
String wordGroup = a + ' ' + b;
for (int i = 0; i <= 10; i++)
System.out.println();
String wordGuess = scan.nextLine();
System.out.println(wordGroup.equals(wordGuess) ? "awesome cock muncher a match" : "nope");
return wordGroup.equals(wordGuess) ? 1 : 0;
}
private static String getWord(Random random, String one, String two, String three) {
int cv = random.nextInt(3) + 1;
if (cv == 1)
return one;
if (cv == 2)
return two;
if (cv == 3)
return three;
return "";
}
public void printIq(int iq) {
System.out.println();
if (iq == 3)
System.out.println("YOU GOT THEM ALL");
else if (iq <= 2 || iq >= 1)
System.out.println("YOU MISSED A TOTAL OF " + (3 - iq));
else if (iq == 0)
System.out.println("HA! YOU MISSED THEM ALL");
}
}
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int Level, Str, Dex, Con, Int, Wis, Cha, HP, Bonus, bonusCounter;
System.out.print("Enter Level : ");
Level = sc.nextInt();
if (Level <= 0) {
System.err.println("Invalid Input!!");
System.exit(0);
}
System.out.println("Enter Str :");
Str = sc.nextInt();
if (Str <= 0) {
System.err.println("Invalid Input!!");
System.exit(0);
}
System.out.println("Enter Dex :");
Dex = sc.nextInt();
if (Dex <= 0) {
System.err.println("Invalid Input!!");
System.exit(0);
}
System.out.println("Enter Con :");
Con = sc.nextInt();
if (Con <= 0) {
System.err.println("Invalid Input!!");
System.exit(0);
}
System.out.println("Enter Int :");
Int = sc.nextInt();
if (Int <= 0) {
System.err.println("Invalid Input!!");
System.exit(0);
}
System.out.println("Enter Wis :");
Wis = sc.nextInt();
if (Wis <= 0) {
System.err.println("Invalid Input!!");
System.exit(0);
}
System.out.println("Enter Cha :");
Cha = sc.nextInt();
if (Cha <= 0) {
System.err.println("Invalid Input!!");
System.exit(0);
}
System.out.println("\nLevel : " + Level);
if (Str == 10) {
Bonus = 0;
System.out.println("Str : " + Str + "[" + Bonus + "]");
}
else if (Str < 10) {
Bonus = 0;
bonusCounter = Str;
while (bonusCounter <= 10) {
if (bonusCounter % 2 == 1) {
Bonus=+1;
}
bonusCounter=+1;
}
System.out.println("Str : " + Str + "[-" + Bonus + "]");
}
else {
Bonus = 0;
bonusCounter = 10;
while (bonusCounter <= Str) {
if (bonusCounter % 2 == 0) {
Bonus=+1;
}
bonusCounter=+1;
}
System.out.println("Str : " + Str + "[+" + Bonus + "]");
}
The code should calculate a bonus value for the 6 variables. Each Bonus should be 0 at 10, cumulative +1 for each even number above 10 and -1 for each odd number below 10. This is just the first part of my code. The same method applies for the 6 variables. There is logical error in the while condition so the code doesn't produce the output. What can i do to fix it?
Replace
bonusCounter = +1;
with
bonusCounter += 1;
Doing my AP Computer Science homework right now but I am stuck with run-time errors. Does anyone know what is wrong with my code?
The program was working fine on Dr.Java but it shows run-time error on my website tester in edhesive...
class Main{
public static void main (String str[]) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a tweet:");
String tweet = scan.nextLine();
int hash = 0;
int attr = 0;
int link = 0;
int ch = 0;
if(tweet.length()>140)
{
System.out.println("Excess Characters: " + (tweet.length() - 140 ));
}
else
{
tweet=tweet.toLowerCase();
System.out.println("Length Correct");
for(ch=0; ch<tweet.length(); ch++)
{
if(tweet.charAt(ch) == '#' && ((ch++)<=(tweet.length())) && (tweet.charAt(ch++)!=' ' && tweet.charAt(ch++)!='\t'))
{
hash++;
}
if(tweet.charAt(ch) == '#' && ((ch++)<=(tweet.length())) && (tweet.charAt(ch++)!=' ' && tweet.charAt(ch++)!='\t'))
{
attr++;
}
if(tweet.charAt(ch) == 'h' && ((ch + 7)<=(tweet.length())))
{
String a = new String("http://");
String sub = new String(tweet.substring(ch, ch + 7));
if (sub.equals(a))
{link++;}
}
}
System.out.println("Number of Hashtags: " + hash);
System.out.println("Number of Attributions: " + attr);
System.out.println("Number of Links: " + link);
}
}
}
Because of ch++ the value of ch is getting incremented after checking this condition (ch++)<=(tweet.length()).
Explanation :
if(tweet.charAt(ch) == '#' && ((ch++)<=(tweet.length())) && (tweet.charAt(ch++)!=' ' && tweet.charAt(ch++)!='\t'))
{
hash++;
}
For above code there are 4 conditions (for i=0):
tweet.charAt(ch) ch = 0
((ch++)<=(tweet.length())) ch = 0, but ch++ so the value will be increamented after the condition check.
(tweet.charAt(ch++) ch=1 (becuase of Point No. 2)
tweet.charAt(ch++) ch = 2 (for the same reason).
Try this:
class Main{
public static void main (String str[]) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a tweet:");
String tweet = scan.nextLine();
int hash = 0;
int attr = 0;
int link = 0;
int ch = 0;
if(tweet.length()>140)
{
System.out.println("Excess Characters: " + (tweet.length() - 140 ));
}
else
{
tweet=tweet.toLowerCase();
System.out.println("Length Correct");
for(ch=0; ch<tweet.length(); ch++)
{
if(tweet.charAt(ch) == '#' && ((ch+1)<(tweet.length())) && (tweet.charAt(ch+1)!=' ' && tweet.charAt(ch+1)!='\t'))
{
hash++;
}
if(tweet.charAt(ch) == '#' && ((ch+1)<(tweet.length())) && (tweet.charAt(ch+1)!=' ' && tweet.charAt(ch+1)!='\t'))
{
attr++;
}
if(tweet.charAt(ch) == 'h' && ((ch + 7)<(tweet.length())))
{
String a = new String("http://");
String sub = new String(tweet.substring(ch, ch + 7));
if (sub.equals(a))
{link++;}
}
}
System.out.println("Number of Hashtags: " + hash);
System.out.println("Number of Attributions: " + attr);
System.out.println("Number of Links: " + link);
}
}
}
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Hangman {
public static void main(String[] args) {
String playAgainMsg = "Would you like to play again?";
String pickCategoryMsg = "You've tried all the words in this category!\nWould you like to choose another category?";
int winCounter = 0, loseCounter = 0, score = 0;
String[] words;
int attempts = 0;
String wordToGuess;
boolean playCategory = true, playGame = true;
int totalCounter = 0, counter;
while (playCategory && playGame)
{
while (playCategory && playGame) {
words = getWords();
counter = 0;
while (playGame && counter < words.length) {
wordToGuess = words[counter++];
if (playHangman(wordToGuess)) {
winCounter++;
System.out.println("You win! You have won " + winCounter + " game(s)." + " You have lost " + loseCounter + " game(s).");
} else {
loseCounter++;
System.out.println("You lose! You have lost " + loseCounter + " game(s)." + " You have won " + winCounter + " game(s).");
}
if (counter < words.length) playGame = askYesNoQuestion(playAgainMsg);
}
if (playGame) playCategory = askYesNoQuestion(pickCategoryMsg);
}
}
}
public static boolean playHangman(String wordToGuess) {
String[] computerWord = new String[wordToGuess.length()];
String[] wordWithDashes = new String[wordToGuess.length()];
for (int i = 0; i < computerWord.length; i++) {
computerWord[i] = wordToGuess.substring(i, i+1);
wordWithDashes[i] = "_";
}
Scanner in = new Scanner(System.in);
int attempts = 0, maxAttempts = 7;
boolean won = false;
int points = 0;
while (attempts < maxAttempts && !won) {
String displayWord = "";
for (String s : wordWithDashes) displayWord += " " + s;
System.out.println("\nWord is:" + displayWord);
System.out.print("\nEnter a letter or guess the whole word: ");
String guess = in.nextLine().toLowerCase();
if (guess.length() > 1 && guess.equals(wordToGuess)) {
won = true;
} else if (wordToGuess.indexOf(guess) != -1) {
boolean dashes = false;
for (int i = 0; i < computerWord.length; i++) {
if (computerWord[i].equals(guess)) wordWithDashes[i] = guess;
else if (wordWithDashes[i].equals("_")) dashes = true;
}
won = !dashes; // If there are no dashes left, the whole word has been guessed
} else {
drawHangmanDiagram(attempts);
System.out.println("You've used " + ++attempts + " out of " + maxAttempts + " attempts.");
}
}
int score = 0;
score = scoreGame(attempts);
System.out.println("Your score is: " + score);
return won;
}
//should take in a failure int from the main method that increments after every failed attempt
public static void drawHangmanDiagram(int failure)
{
if (failure == 0)
System.out.println("\t+--+\n\t| |\n\t|\n\t|\n\t|\n\t|\n\t|\n\t|\n\t+--");
else if (failure == 1)
System.out.println("\t+--+\n\t| |\n\t| #\n\t|\n\t|\n\t|\n\t|\n\t|\n\t+--");
else if (failure == 2)
System.out.println("\t+--+\n\t| |\n\t| #\n\t| /\n\t|\n\t|\n\t|\n\t|\n\t+--");
else if (failure == 3)
System.out.println("\t+--+\n\t| |\n\t| #\n\t| / \\\n\t|\n\t|\n\t|\n\t|\n\t+--");
else if (failure == 4)
System.out.println("\t+--+\n\t| |\n\t| #\n\t| /|\\\n\t| |\n\t|\n\t|\n\t|\n\t+--");
else if (failure == 5)
System.out.println("\t+--+\n\t| |\n\t| #\n\t| /|\\\n\t| |\n\t| /\n\t|\n\t|\n\t+--");
else if (failure == 6)
System.out.println("\t+--+\n\t| |\n\t| #\n\t| /|\\\n\t| |\n\t| / \\\n\t|\n\t|\n\t+--");
}
// Asks user a yes/no question, ensures valid input
public static boolean askYesNoQuestion(String message) {
Scanner in = new Scanner(System.in);
boolean validAnswer = false;
String answer;
do {
System.out.println(message + " (Y/N)");
answer = in.nextLine().toLowerCase();
if (answer.matches("[yn]")) validAnswer = true;
else System.out.println("Invalid input! Enter 'Y' or 'N'.");
} while (!validAnswer);
return answer.equals("y");
}
public static boolean askForCategory(int category) {
Scanner in = new Scanner(System.in);
boolean validAnswer = false;
String answer;
do {
System.out.println("\nWould you like to play again? (Y/N)");
answer = in.nextLine().toLowerCase();
if (answer.matches("[yn]")) validAnswer = true;
else System.out.println("Invalid input! Enter 'Y' or 'N'.");
} while (!validAnswer);
return answer.equals("y");
}
// Asks the user to pick a category
public static String[] getWords() {
String[] programming = {"java", "pascal", "python", "javascript", "fortran", "cobol"};
String[] sports = {"gymnastics", "badminton", "athletics", "soccer", "curling", "snooker", "hurling", "gaelic", "football", "darts"};
String[] result = {""};
Scanner in = new Scanner(System.in);
boolean validAnswer = false;
String answer;
do {
System.out.println("Pick a category:\n1. Programming\n2. Sports");
answer = in.nextLine().toLowerCase();
if (answer.matches("[1-2]")) validAnswer = true;
else System.out.println("Invalid input! Enter the number of the category you want.");
} while (!validAnswer);
int selection = Integer.parseInt(answer);
switch (selection) {
case 1: result = randomOrder(programming); break;
case 2: result = randomOrder(sports); break;
}
return result;
}
// Sorts a String array in random order
public static String[] randomOrder(String[] array) {
int[] order = uniqueRandoms(array.length);
String[] result = new String[array.length];
for (int i = 0; i < order.length; i++) {
result[i] = array[order[i]];
}
return result;
}
// Generates an array of n random numbers from 0 to n-1
public static int[] uniqueRandoms(int n) {
int[] array = new int[n];
int random, duplicateIndex;
for (int i = 0; i < n; ) {
random = (int) (Math.random() * n);
array[i] = random;
for (duplicateIndex = 0; array[duplicateIndex] != random; duplicateIndex++);
if (duplicateIndex == i) i++;
}
return array;
}
public static int scoreGame(int attempts)
{
int score = 0;
switch (attempts)
{
case 0: score = 70; break;
case 1: score = 60; break;
case 2: score = 50; break;
case 3: score = 40; break;
case 4: score = 30; break;
case 5: score = 20; break;
case 6: score = 10; break;
case 7: score = 0; break;
}
return score;
}
}
I have got it working so that it keeps count of the games won and lost, as well as assigning a score based on the amount of attempts/lives saved but I haven't been able to find a way to get it to keep a total score for all of the games played. Each game unfortunately has a seperate score. If anyone can advise me on a way of doing this, it would be greatly appreciated.
Create an int totalScore variable where winCounter, loseCounter and score are defined. Then increment it after each call to scoreGame()
score = scoreGame(attempts);
totalScore += score;
System.out.println("Your score is: " + score);
If you want to permanently save statistics between sessions then it's a whole nother story. You would need to write your scores to a file after each round and then start your program by reading this score file. It's hardly impossible, but requires a bit more code.
I made a Java program that did some basic math for kids in primary. I wanted to make it a little more advanced, and remember the questions they got wrong, and then optionally present these questions again at the end and allow them to attempt them once more.
But the code for it got really complicated really quick. I thought I finished it, but it's having difficulty compiling (read: won't) and I'm at the point where I need some help. I'd greatly appreciate it.
import java.util.Scanner;
import java.util.Random;
public class ArithmeticTester0 {
public static void main(String[] arg) {
int level = 0;
String type = "";
String name = "";
Scanner keyboard = new Scanner(System.in);
System.out.println("Hi! I am your friendly Math Tutor.");
System.out.print("What is your name? ");
name = keyboard.nextLine();
System.out.print("Would you like to be tested on addition, subtraction or both? ");
type = keyboard.nextLine();
// Check if the type inputted is anything other than addition, subtraction or both
if (!type.equalsIgnoreCase("addition") && !type.equalsIgnoreCase("subtraction") && !type.equalsIgnoreCase("both") && !type.equalsIgnoreCase("add") && !type.equalsIgnoreCase("subtraction")) {
while (!type.equalsIgnoreCase("addition") && !type.equalsIgnoreCase("subtraction") && !type.equalsIgnoreCase("both") && !type.equalsIgnoreCase("add") && !type.equalsIgnoreCase("subtraction")) {
System.out.print("You must answer addition, subtraction or both. How about we try again? ");
type = keyboard.nextLine();
}
}
System.out.print("What level would you like to choose? " +
" Enter 1, 2 or 3: ");
level = keyboard.nextInt();
// Check if the level entered is not 1, 2 or 3
if (level > 3 || level < 1) {
while (level > 3 || level < 1) {
System.out.println("The number must be either 1, 2 or" +
" 3. Let's try again shall we?");
System.out.print("What level would you like to choose? ");
level = keyboard.nextInt();
}
}
System.out.println("\nOK " + name +
", here are 10 exercises for you at level " + level + ".");
System.out.println("Good luck!\n");
int a = 0, b = 0, c = 0;
int preva = 0, prevb = 0; //previous a and b value
int correct = 0;
if (type.equalsIgnoreCase("addition") || type.equalsIgnoreCase("add")) {
add(level, preva, prevb, a, b, c, type);
}
else if (type.equalsIgnoreCase("subtraction") || type.equalsIgnoreCase("subtract")) {
subtract(level, preva, prevb, a, b, c, type);
}
else {
both(level, preva, prevb, a, b, c, type);
}
}
/* The method below prints out a statement depending
on how well the child did */
public static void add(int level, int preva, int prevb, int a, int b, int c, String type) {
Random random = new Random();
Scanner keyboard = new Scanner(System.in);
List<Integer> wrongList = Arrays.asList(array);
int correct = 0;
int wrong = 0;
// Generate 10 questions
for (int i = 1; i <= 10; i++) {
/* Generate numbers depending on the difficulty.
The while loop ensures that the next question isn't the same
as the previous question that was just asked. */
if (level == 1) {
while (preva == a && prevb == b) {
a = random.nextInt(10);
b = random.nextInt(11-a);
}
}
else if (level == 2) {
while (preva == a && prevb == b) {
a = random.nextInt(10);
b = random.nextInt(10);
}
}
else if (level == 3) {
while (preva == a && prevb == b) {
a = random.nextInt(50);
b = random.nextInt(50);
}
}
preva = a;
prevb = b;
System.out.print(a + " + " + b + " = ");
c = keyboard.nextInt();
// Check if the user was correct
if (a + b == c) {
System.out.println("You are right!");
correct++;
}
if (a - b != c) {
wrongList.add(a);
wrongList.add(b);
wrong++;
}
}
// Conclusion
System.out.println("\nYou got " + correct + " right out of 10.");
if (wrong > 0) {
int[] wrongAnswers = wrongList.toArray(new int[wrongList.size()]);
System.out.print("You got " + wrong + " wrong, would you like to retry those questions?");
String response = keyboard.nextLine();
if (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea")) {
retry(wrongAnswers, type, wrongIndexArray);
}
else if (response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) {
System.out.println("OK! That's fine.");
}
else {
while (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea") || response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) {
System.out.println("Please put in an answer that is along the lines of \"yes\" or \"no\".");
response = keyboard.nextLine();
}
}
}
conclusion(correct, level);
System.out.println("See ya!");
}
public static void subtract(int level, int preva, int prevb, int a, int b, int c, String type) {
Random random = new Random();
Scanner keyboard = new Scanner(System.in);
List<Integer> wrongList = Arrays.asList(array);
// Generate 10 questions
int correct = 0;
int wrong = 0;
for (int i = 1; i <= 10; i++) {
/* Generate numbers depending on the difficulty.
The while loop ensures that the next question isn't the same
as the previous question that was just asked. */
if (level == 1) {
while (preva == a && prevb == b) {
a = random.nextInt(10);
b = random.nextInt(11-a);
while (b > a) {
a = random.nextInt(10);
b = random.nextInt(11-a);
}
}
}
else if (level == 2) {
while (preva == a && prevb == b) {
a = random.nextInt(10);
b = random.nextInt(10);
while (b > a) {
a = random.nextInt(10);
b = random.nextInt(11-a);
}
}
}
else if (level == 3) {
while (preva == a && prevb == b) {
a = random.nextInt(50);
b = random.nextInt(50);
}
}
preva = a;
prevb = b;
System.out.print(a + " - " + b + " = ");
c = keyboard.nextInt();
// Check if the user was correct
if (a - b == c) {
System.out.println("You are right!");
correct++;
}
if (a - b != c) {
wrongList.add(a);
wrongList.add(b);
wrong++;
}
}
// Conclusion
System.out.println("\nYou got " + correct + " right out of 10.");
if (wrong > 0) {
int[] wrongAnswers = wrongList.toArray(new int[wrongList.size()]);
System.out.print("You got " + wrong + " wrong, would you like to retry those questions?");
String response = keyboard.nextLine();
if (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea")) {
retry(wrongAnswers, type);
}
else if (response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) {
System.out.println("OK! That's fine.");
}
else {
while (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea") || response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) {
System.out.println("Please put in an answer that is along the lines of \"yes\" or \"no\".");
response = keyboard.nextLine();
}
}
}
conclusion(correct, level);
System.out.println("See ya!");
}
public static void both(int level, int preva, int prevb, int a, int b, int c, String type) {
Random random = new Random();
Scanner keyboard = new Scanner(System.in);
// Generate 10 questions
int correct = 0;
List<Integer> wrongIndexes = Arrays.asList(array);
for (int i = 1; i <= 5; i++) {
/* Generate numbers depending on the difficulty.
The while loop ensures that the next question isn't the same
as the previous question that was just asked. */
if (level == 1) {
while (preva == a && prevb == b) {
a = random.nextInt(10);
b = random.nextInt(11-a);
}
}
else if (level == 2) {
while (preva == a && prevb == b) {
a = random.nextInt(10);
b = random.nextInt(10);
}
}
else if (level == 3) {
while (preva == a && prevb == b) {
a = random.nextInt(50);
b = random.nextInt(50);
}
}
preva = a;
prevb = b;
System.out.print(a + " + " + b + " = ");
c = keyboard.nextInt();
// Check if the user was correct
if (a + b == c) {
System.out.println("You are right!");
correct++;
}
else {
System.out.println("Oops! You made a mistake. " + a + " + " + b + " = " + (a+b));
wrongIndexes += i;
}
}
for (int i = 6; i <= 10; i++) {
/* Generate numbers depending on the difficulty.
The while loop ensures that the next question isn't the same
as the previous question that was just asked. */
if (level == 1) {
while (preva == a && prevb == b) {
a = random.nextInt(10);
b = random.nextInt(11-a);
while (b > a) {
a = random.nextInt(10);
b = random.nextInt(11-a);
}
}
}
else if (level == 2) {
while (preva == a && prevb == b) {
a = random.nextInt(10);
b = random.nextInt(10);
while (b > a) {
a = random.nextInt(10);
b = random.nextInt(11-a);
}
}
}
else if (level == 3) {
while (preva == a && prevb == b) {
a = random.nextInt(50);
b = random.nextInt(50);
}
}
preva = a;
prevb = b;
System.out.print(a + " - " + b + " = ");
c = keyboard.nextInt();
// Check if the user was correct
if (a - b == c) {
System.out.println("You are right!");
correct++;
}
else {
System.out.println("Oops! You made a mistake. " + a + " - " + b + " = " + (a-b));
wrongIndexes += i;
}
}
int[] wrongIndexArray = wrongIndexes.toArray(new int[wrongIndexes.size()]);
// Conclusion
System.out.println("\nYou got " + correct + " right out of 10.");
if (wrong > 0) {
int[] wrongAnswers = wrongList.toArray(new int[wrongList.size()]);
System.out.print("You got " + wrong + " wrong, would you like to retry those questions?");
String response = keyboard.nextLine();
if (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea")) {
retry(wrongAnswers, type, wrongIndexArray);
}
else if (response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) {
System.out.println("OK! That's fine.");
}
else {
while (response.equalsIgnoreCase("Yes") || response.equalsIgnoreCase("Okay") || response.equalsIgnoreCase("OK") || response.equalsIgnoreCase("Sure") || response.equalsIgnoreCase("Yeah") || response.equalsIgnoreCase("Yea") || response.equalsIgnoreCase("No") || response.equalsIgnoreCase("Nope") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("No thanks") || response.equalsIgnoreCase("Nah") || response.equalsIgnoreCase("Do not want")) {
System.out.println("Please put in an answer that is along the lines of \"yes\" or \"no\".");
response = keyboard.nextLine();
}
}
}
conclusion(correct, level);
System.out.println("See ya!");
}
public static void retry(int[] wrongAnswers, String type, int[] wrongIndexArray) {
int c = 0;
if (type.equalsIgnoreCase("addition") || type.equalsIgnoreCase("add")) {
for (int i = 0; i < wrongAnswers.length; i+=2) {
System.out.print(wrongAnswers[i] + " + " + wrongAnswers[i+1] + " = ");
c = keyboard.nextInt();
if (wrongAnswers[i] + wrongAnswers[i+1] == c) {
System.out.println("You are right!");
}
else {
System.out.println("Oops! You made a mistake. " + wrongIndex[i] + " + " + wrongIndex[i+1] + " = " + (wrongIndex[i]+wrongIndex[i+1]));
}
}
}
else if (type.equalsIgnoreCase("subtraction") || type.equalsIgnoreCase("subtract")) {
for (int i = 0; i < wrongAnswers.length; i+=2) {
System.out.print(wrongAnswers[i] + " - " + wrongAnswers[i+1] + " = ");
c = keyboard.nextInt();
if (wrongAnswers[i] - wrongAnswers[i+1] == c) {
System.out.println("You are right!");
}
else {
System.out.println("Oops! You made a mistake. " + a + " - " + b + " = " + (a-b));
}
}
}
else {
for (int i = 0; i < wrongAnswers.length; i+=2) {
for (int i = 0; i < wrongIndexArray.length; i++) {
if (wrongIndexArray[i] < 6) {
System.out.print(wrongAnswers[i] + " + " + wrongAnswers[i+1] + " = ");
c = keyboard.nextInt();
if (wrongAnswers[i] + wrongAnswers[i+1] == c) {
System.out.println("You are right!");
}
else {
System.out.println("Oops! You made a mistake. " + wrongIndex[i] + " + " + wrongIndex[i+1] + " = " + (wrongIndex[i]+wrongIndex[i+1]));
}
}
else if (wrongIndexArray[i] > 5) {
System.out.print(wrongAnswers[i] + " - " + wrongAnswers[i+1] + " = ");
c = keyboard.nextInt();
if (wrongAnswers[i] - wrongAnswers[i+1] == c) {
System.out.println("You are right!");
}
else {
System.out.println("Oops! You made a mistake. " + wrongIndex[i] + " + " + wrongIndex[i+1] + " = " + (wrongIndex[i]+wrongIndex[i+1]));
}
}
}
}
}
}
public static void conclusion(int x, int lev) {
if (x >= 9) {
if (lev == 3) {
if (x == 9)
System.out.println("Please try the same difficulty again" +
", you almost scored 10 out of 10!");
else
System.out.println("You have mastered addition at this level" +
"! The system is not of any further use.");
}
else {
System.out.println("Please select a higher difficulty next time!");
}
}
else if (x >= 6) {
System.out.println("Please try the test again.");
}
else {
System.out.println("Please ask your teacher for extra lessons.");
}
}
}
The errors are:
You forgot to import java.util.List. Just go ahead and import java.util.*. You know you want to.
You didn't declare wrongIndexArray, array, wrong, wrongList, keyboard, a, or b anywhere. I may have missed a few.
In this line:
int[] wrongAnswers = wrongList.toArray(new int[wrongList.size()]);
wrongList is an List<Integer> so I think that you can't cast it to an int[]. You have to use Integer[].
Possibly others, but...
...based on the errors you have, are you using an IDE such as NetBeans? Using an IDE will go far in letting you identify the reasons for the errors you're getting, and they often include helpful hints with each error.
Some other helpful hints:
It looks like you're storing the operands in a list when the student gets the answer wrong. Consider instead wrapping the operands and the operation into a class such as SubtractionQuestion or AdditionQuestion. It might help later on when you want to retest the student on the ones they got wrong.
Store the "yes" and "no" text answers (Yeah, Yes, Yea, Yup) in an ArrayList<String> yesAnswers, then just check if yesAnswers.contains(answer). This makes it easy to maintain the list, and you also don't need to check against yes, no, or not yes or no -- just check against yes, else no, else bad answer.
Hope that makes sense....