Made a program that prints if a number is weird or not. If the number is odd, it's weird. If the number is even and inclusively between 2 and 5, it is not weird. If it's even and inclusively between 6 and 20, it's weird, and if it's even and greater than 20, it's not weird. The problem I'm having here is that instead of the output displaying "This number is weird/not weird", I get "Weird" or "Not Weird" on one line, followed by "This number is 0" if it's even, or "This number is 1" if it's odd.
public Weird(int num)
{
n = num;
}
public int EvenOrOdd()
{
int check = n % 2;
int answer = n / 2;
if (check == 0 && answer >= 2 && answer <= 5)
{
System.out.println("Not Weird");
}
else if (check == 0 && answer >= 6 && answer <= 20)
{
System.out.println("Weird");
}
else if (check == 0 && answer > 20)
{
System.out.println("Not Weird");
}
else if (check != 0)
{
System.out.println("Weird");
}
return check;
}
public static void main(String[] args)
{
Weird w = new Weird(32);
Weird a = new Weird(21);
System.out.println("This number is " + w.EvenOrOdd());
System.out.println("This number is " + a.EvenOrOdd());
}
You actually return int which is value of check field. This is either 1 or 0.
When you call this line-
System.out.println("This number is " + w.EvenOrOdd());
System.out.println("This number is " + a.EvenOrOdd());
It prints either This number is 0 or This number is 1.
You can get desired output by two ways-
Way 1-
Change return type of method to void EvenOrOdd() like-
public void EvenOrOdd()
{
int check = n % 2;
int answer = n / 2;
if (check == 0 && n >= 2 && n<= 5)
{
System.out.println("Not Weird");
}
else if (check == 0 && n>= 6 && n<= 20)
{
System.out.println("Weird");
}
else if (check == 0 && n> 20)
{
System.out.println("Not Weird");
}
else if (check != 0)
{
System.out.println("Weird");
}
}
and call method in main as-
public static void main(String[] args)
{
Weird w = new Weird(32);
Weird a = new Weird(21);
w.EvenOrOdd();
a.EvenOrOdd();
}
Way 2- Change return type of method to String as-
public String EvenOrOdd()
{
int check = n % 2;
int answer = n / 2;
if (check == 0 && n>= 2 && n<= 5)
{
return "Not Weird";
}
else if (check == 0 && n>= 6 && n<= 20)
{
return "Weird";
}
else if (check == 0 && n> 20)
{
return "Not Weird";
}
else
{
return "Weird";
}
}
And main method remains same-
public static void main(String[] args)
{
Weird w = new Weird(32);
Weird a = new Weird(21);
System.out.println("This number is " + w.EvenOrOdd());
System.out.println("This number is " + a.EvenOrOdd());
}
You should return that string rather than printing it. In your function, you are returning an int but rather you should write a String. Besides that everything looks good but you can decrease the number of check like below:
public Weird(int num)
{
n = num;
}
public String EvenOrOdd()
{
int check = n % 2;
if (check == 1 || (check == 0 && n >= 6 && n <= 20)) {
return "Weird";
}else{
return "Not Weird";
}
}
public static void main(String[] args)
{
Weird w = new Weird(32);
Weird a = new Weird(21);
System.out.println("This number is " + w.EvenOrOdd());
System.out.println("This number is " + a.EvenOrOdd());
}
The value you return from a method will kind of "replace" the method call. Because you returned check:
return check;
The calls:
System.out.println("This number is " + w.EvenOrOdd());
System.out.println("This number is " + a.EvenOrOdd());
are basically:
System.out.println("This number is " + 0);
System.out.println("This number is " + 1);
You seem to be confused about returning and printing. Your method should look like this:
public String EvenOrOdd()
{
int check = n % 2;
int answer = n / 2;
if (check == 0 && n >= 2 && n <= 5)
{
return "Not Weird";
}
else if (check == 0 && n >= 6 && n <= 20)
{
return "Weird";
}
else if (check == 0 && n > 20)
{
return "Not Weird";
}
else
{
return "Weird";
}
}
Now the two calls is basically:
System.out.println("This number is " + "Weird");
System.out.println("This number is " + "Not Weird");
Related
I'm trying to write a program that asks the user to enter a number, then I need to create a method called isPrime to create the calculation and print out the result in main. I'm sure it's something small that I'm missing, but I can't get it to produce an accurate result.
public static void main(String[] args) {
System.out.print("Enter number: ");
int num = s.nextInt();
if (isPrime(num) == true) {
System.out.println("Number is prime");
} else if (isPrime(num) == false) {
System.out.println("Number is not prime");
}
}
public static boolean isPrime(int num){
for(int i = 2; i <= num/2; i++) {
if (num%i != 0) {
return true;
}
}
return false;
}
Use an if and else (don't retest your boolean condition in the first case). And don't test for == true in an if. This
if(isPrime(num) == true)
{
System.out.println("Number is prime");
}
else if(isPrime(num) == false)
{
System.out.println("Number is not prime");
}
Should just be
if (isPrime(num)) {
System.out.println("Number is prime");
} else {
System.out.pritnln("Number is not prime");
}
or even something like
System.out.print("Number is ");
if (!isPrime(num)) {
System.out.print("not ");
}
System.out.println("prime");
If you want to put the braces on their own lines go ahead. As for your isPrime method; you have your return conditions reversed (and the test as well). Also we can optimize it a bit. Unroll the first even test, because then we can skip every other element. Also we only need to test to the square root of the input number. Like,
public static boolean isPrime(int num) {
if (num == 2) {
return true; // two is prime.
}
if (num < 1 || num % 2 == 0) {
return false; // all other even numbers are not prime.
}
for(int i = 3; i <= Math.sqrt(num); i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
The isPrime(int num) method may need to check if num % i == 0 instead of if num % i != 0 because many non-prime numbers will pass the condition and return true.
As an example, if isPrime(9) is called, the conditional will check if 9 % 2 != 0, which is true, and the method will say that 9 is prime when it's not.
As a result, you could try changing the method to something like this:
public static boolean isPrime(int num)
{
for(int i = 2; i <= num/2; i++)
{
if (num%i==0)
{
return false;
}
}
return true;
}
You should change your "if (num%i!=0)" condition to if(num % i == 0)
See the following code:
public class Prime {
public static void main(String[] args) {
int num = 29;
boolean flag = false;
for(int i = 2; i <= num/2; ++i)
{
// condition for nonprime number
if(num % i == 0)
{
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
Very amateur coder here,
I'm in the process of making a simple program to tell whether a number is positive, negative, odd or even. So far everything works apart from positive odd numbers, but cant figure out why ?
Also, how would i loop it ? would i use a for or while loop ?
Thanks
package weekeleven;
import java.util.Scanner;
public class week111 {
static int number;
static String pE;
static String pO;
static String nE;
static String nO;
static String eR;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
number = -input.nextInt();
pE = ("Positive Even");
pO = ("Positive Odd");
nE = ("Negative Even");
nO = ("Negative Odd");
eR = ("Error");
Object x = null;
x=getSignAndParity(x);
System.out.println(x);
}
public static String getSignAndParity(Object x) {
if ((number > 0) && (number % 2 == 0)) { //negative and even
return nE;
} else if((number < 0) && (number % 2 == 1)) { // positive and odd
return pO;
} else if((number < 0) && (number % 2 == 0)) { // positive and even
return pE;
} else if((number > 0) && (number % 2 == 1)) { // negative and odd
return nO;
} else { // other cases
return eR;
}
}
}
There are multiple issues with your code causing it not to work as expected.
1) You are using mod 2 on negative numbers to check if a negative number is even or odd. But that will not work. For e.g., -7 mod 2 = -1 (which is neither 1 nor 0)
2) You are negating the user's input :
number = -input.nextInt();
and you flipped the signs >, < which is unnecessary
3) You should not be passing the int input as Object. You could just simply:
public static String getSignAndParity(int x)
Removing all the superfluous and unnecessary declarations and code, you can have this:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = input.nextInt();
System.out.println(getSignAndParity(number));
}
public static String getSignAndParity(int num) {
if(num == 0)
return "Error";
String sign = num>0?"Positive":"Negative";
String parity = Math.abs(num)%2==0?"Even":"Odd";
return sign + " " + parity;
}
Change with this
else if((number > 0) && (number % 2 == 1)) { // positive and odd
return pO;
}
For positive odd numbers and for looping you need either existing the code or have a condition that is taken from scanner and then close the scanner.
scanner : https://www.javatpoint.com/Scanner-class
You have to change positive odd like this:
else if((number < 0) && (number % 2 == -1)) { // positive and odd}
If there are numbers in which some are in sequence and some are random number, then how the consecutive numbers can be replaced with the range of number and random number should be as it is?
for eg: 1,2,3,4,5,6, 458,243
output: 1-6,458,243
I have just solved this problem..Its super simple just some if-else condition, however, I don't think about efficient way, so please follow the solution and try to efficient it.
public static void main(String[] args) {
int [] numberList = {1,2,3,4,5,6,458,243};
int initialSequence = -1;
int endSequence = -5;
for (int num = 0; num<numberList.length;num++) {
if(num<numberList.length-1) {
if(initialSequence == -1 && numberList[num] == numberList[num+1]-1) {
initialSequence = endSequence = numberList[num];
}else if(initialSequence == -1 && numberList[num] != numberList[num+1]-1){
System.out.print(numberList[num] + " ");
}else if(initialSequence != -1 && numberList[num] == numberList[num+1]-1) {
endSequence = numberList[num];
}
else {
System.out.print(initialSequence + "-" + (endSequence+1) + " ");
initialSequence = -1;
endSequence = -5;
}
}else {
if(initialSequence == -1) System.out.print(numberList[num] + " ");
else {
System.out.print(initialSequence + "-" + (endSequence+1) + " ");
}
}
}
}
I am trying to create a piece of code for the game 'fizzbuzz', if n|3 => n=Fizz, if n|5 => n= Buzz and if n|3 and n|5 then n=Fizzbuzz.
For some reason my code only displays 46 lines of code, can someone help me out? Thanks.
Here is my code:
import static java.lang.Math.*;
import java.io.*;
public class P2InventedExercise
{
static void FizzBuzz(int n)
{
/** Welcome Message **/
System.out.println("+----------------------------+");
System.out.println("| WELCOME TO FIZZ BUZZ |");
System.out.println("+----------------------------+");
/** Creating Strings to Print & Defines integer 'k'. **/
String Fizz = "Fizz";
String Buzz = "Buzz";
String FizzBuzz = "FizzBuzz";
int k = 0;
/** Printing Strings **/
while (k <= n)
{
/** Boolean Tests **/
boolean FizzTest = (k%3 == 0);
boolean BuzzTest = (k%5 == 0);
boolean FizzBuzzTest = (k%3 == 0 && k%5 == 0);
/** If Tests **/
if (FizzBuzzTest)
{
System.out.println(k+"= " + FizzBuzz);
k=k+1;
continue;
}
if (FizzTest)
{
System.out.println(k + "= " + Fizz);
k=k+1;
continue;
}
else if (BuzzTest)
{
System.out.println(k + "= " + Buzz);
k=k+1;
continue;
}
else
{
System.out.println(k + "= " + k);
k=k+1;
continue;
}
}
}
}
The code looks almost OK, check what is the n.
Also, pay attention you're missing else in the second if statement. It should be:
else if (FizzTest)
GentleCynic your solution is very close, please checkout my video for a detailed explanation located on youtube
This particular module will run 60 times
package java_basics;
public class FizzBuzz {
public static void main(String[] args) {
printFizzBuzz(60); // shorthand for initializing the runtimes
}
public static void printFizzBuzz(int runtimes) { //internal method using the "100 times to run" with declaration of n as integer
for (int number = 0; number <= runtimes; number++) { // create the number of times it should run
if ((number % 3 == 0) && (number % 5 == 0)) { //control flow using modulus operator "%" which means divisible by 3 and "&&" 5
System.out.println("FizzBuzz because the number is " + number + " which is divisible by both 3 and 5"); //prints out the actual number and fizzbuzz
}
else if (number % 3 == 0) { //otherwise if number is only divisible by 3 print "fizz"
System.out.println("Fizz because th number is " + number + " which is only divisible by 3 which is"); //prints fizz
}
else if (number % 5 == 0) {
System.out.println("Buzz because the number is " + number + " which is only divisbe by 5"); //prints buzz
}
else {
System.out.println(number + " This number is not divisible by 3 or 5, therfore there is no fizz or buzz condition met"); // this number is printed without a fizzbuzz
}
}
}
}
function fizzBuzz(n) {
//'n' is integer to know the limit
for(let i =1; i <= n;i++){
//if i multiple of 3 and 5 print 'FizzBuzz'
if(i % 3==0 && i%5==0){
console.log('FizzBuzz')}
//if i multiple of 3 but not 5 print 'Fizz'
else if(i % 3==0 && i%5!==0){
console.log('Fizz')}
//if i not multiple of 3 and multiple 5 print 'Buzz'
else if(i % 3!==0 && i%5==0){
console.log('Buzz')}
//if i not multiple of 3 and not multiple 5 print 'Buzz'
else if(i % 3!==0 && i%5!==0)
{
console.log(i);}
}
}
/*
if n = 15
output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
undefined
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....