how to extract answers from for loop - java

I can't figure out how to correctly write my for loop statement that will give me the correct score. I bolded the code that is what I can't figure out how to write correctly. Anytime I run my program I end up with the first result of (rslt < 3) no matter what numbers I enter.
package module1.assignment;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String options[] = {
"mild or spicy",
"tea or coffee",
"breakfast or " +
"brunch",
"summer or winter",
"paper or plastic"
};
int answers[] = new int[options.length];
String result[] = new String[answers.length];
boolean bool = true;
while (true) {
for (int i = 0; i < options.length; i++) {
System.out.println("Enter 0 for the preference on the left\n" +
"Enter 1 for the preference on the right");
System.out.println("Do you prefer " + options[i] + "?");
answers[i] = scanner.nextInt();
System.out.println("Do you prefer " + options[i + 1] + "?");
answers[i] = scanner.nextInt();
System.out.println("Do you prefer " + options[i + 2] + "?");
answers[i] = scanner.nextInt();
System.out.println("Do you prefer " + options[i + 3] + "?");
answers[i] = scanner.nextInt();
System.out.println("Do you prefer " + options[i + 4] + "?");
answers[i] = scanner.nextInt();
break;
}
for (int i = 0; i < answers.length; i++) {
result[i] = [answers[i]];
}
int rslt = getScore(result);
if (rslt < 3)
System.out.println("You prefer life to be calm and organized");
else if (rslt > 3)
System.out.println("You prefer life to be spontaneous and active.");
else
System.out.println("You prefer a good balance in life");
System.out.println("Enter 0 to exit program or 1 to run again");
int out = scanner.nextInt();
if (out == 0)
bool = false;
if (!bool)
System.exit(0);
}
}
static int getScore(String[] result) {
int score = 0;
for (int i = 0; i < result.length; i++) {
switch (result[i]) {
case "spicy":
score++;
break;
case "coffee":
score++;
break;
case "breakfast":
score++;
break;
case "winter":
score++;
break;
case "paper":
score++;
break;
}
}
return score;
}
}

I have modified your code according to my understanding of the code.
It works just exactly like you may have wanted.
package module1.assignment;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[][] options = {
{"mild", "spicy"},
{"tea", "coffee"},
{"brunch", "breakfast"},
{"summer", "winter"},
{"plastic", "paper"}
};
int[] answers = new int[options.length];
do {
System.out.println("Enter 0 for the preference on the left\n" +
"Enter 1 for the preference on the right");
for (int i = 0; i < options.length; i++) {
System.out.println("Do you prefer " + options[i][0] +
" or " + options[i][1] + "?");
answers[i] = scanner.nextInt();
}
int result = getScore(answers);
if (result < 3)
System.out.println("You prefer life to be calm and organized");
else if (result > 3)
System.out.println("You prefer life to be spontaneous and active.");
else
System.out.println("You prefer a good balance in life");
System.out.println("Enter 0 to exit program or 1 to run again");
} while (scanner.nextInt() != 0);
}
static int getScore(int[] answers) {
int score = 0;
for (int answer : answers) if (answer == 1) score++;
return score;
}
}

To Fix Your Code
In the first for-loop, you are supposed to loop through the options array. But somehow you unfold the loop within the loop body. To prevent the whole thing loop again, you break the loop immediately. To fix the first loop, write it like this instead. Properly loop through each element, no need to unfold it, no need to break it.
System.out.println("Enter 0 for the preference on the left\n" + "Enter 1 for the preference on the right");
for (int i = 0; i < options.length; i++) {
System.out.println("Do you prefer " + options[i] + "?");
answers[i] = scanner.nextInt();
}
In the second loop, you are supposed to retrieve the selected string from answers and write the string to results. Without modifying your data structure, this can be achieved by using split(" or ") on the option, which gives you an array of string which you can use the answer as index to access. Note that this does not prevent array index out of bound exception if user enter anything other than 0 or 1, which you should totally do, but is out of scope of this question.
for (int i = 0; i < answers.length; i++) {
result[i] = options[i].split(" or ")[answers[i]] ;
}
And there you go.
To Solve Your Task
Alternatively, redesigning the data structure and logic to get rid of the unnecessary string manipulation and comparison is more ideal. You don't even need the result and answers array, simply add up the user input will do (if the user follows your instruction)
int rslt = 0;
System.out.println("Enter 0 for the preference on the left\n" +
"Enter 1 for the preference on the right");
for (int i = 0; i < options.length; i++) {
System.out.println("Do you prefer " + options[i] + "?");
rslt += scanner.nextInt();
}

Inside the loop, you continue assigning the result to the same index in the answers list, rather than assigning the result to another index for each input. Because you are not iterating anything, you don't even need the loop. Replace the entire while loop with the code below. Please upvote and accept answer if it solves/helps you with your problem.
System.out.println("Enter 0 for the preference on the left\n" +
"Enter 1 for the preference on the right");
System.out.println("Do you prefer " + options[0] + "?");
answers[0] = scanner.nextInt();
System.out.println("Do you prefer " + options[1] + "?");
answers[1] = scanner.nextInt();
System.out.println("Do you prefer " + options[2] + "?");
answers[2] = scanner.nextInt();
System.out.println("Do you prefer " + options[3] + "?");
answers[3] = scanner.nextInt();
System.out.println("Do you prefer " + options[4] + "?");
answers[4] = scanner.nextInt();
Also, please note this:
for (int i = 0; i < answers.length; i++) {
result[i] = [answers[i]];
}
won't work. Instead of result[i] = [answers[i]];, do result[i] = Integer.parseInt(answers.get(i)).

This is part causing the unexpected behaviour: result[i] = [answers[i]];
From what I understood, you want to implement this:
For each option, store the user choice like 0 or 1, 0 for left, 1 for right
For each user choice, store the string value of the choice, i.e., for the 1st question if the user inputs 0, mild should be captured
Calculate scores by comparing string value of user input against a branching construct, switch case here
The problem is in step 2, the current code result[i] = [answers[i]]; does not express this operation properly.
answers[i] stores the user choice 0 or 1, step 1 operation. So to convert it to the corresponding choice in string step 2 operation, something like this should be done
(options[i].split(" or "))[answers[i]]
Explanation:
Pick up the complete string for each answer
Divide the string into two parts(array with 2 indexes 0 and 1), left and right, using a delimiter, " or " in this case
pick up the left or right based on the value stored in answers[i](the user input)
This should let the code behave as expected :)
There are other aspects that can be improved in the code though, as others have already suggested.

Related

I don't know why the loop doesn't stop! Java

This is my first doubt here, i'm really noob on this and my english isn't the best, so first of all sorry if there's a dummie mistake.
The thing is that I want to do a for loop to save every user input from console and add it to a list named "Order". This action have to be done if the user type correctly the order and it have to be checked to know if that exists on the menu. If it doesn't we have to let the user know the situation and ask him/her if he/she wants something we have on the menu. Also after every input saved on the listed we want to ask the user if he/she wants to order something more, so if is YES the loop have to be initialized and if is NO we have to get out the loop.
The problem with my code is that no matter what I type in the console, the loop is done from start to finish every cicle.
Where is the error?
Thanks!!!
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Fase2_final {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] menu = { "chicken", "steak", "hamburger", "spaghettis", "pizza" };
int[] price = { 10, 15, 20, 5, 12 };
boolean order = true;
for (int i = 0; i < menu.length; i++) {
System.out.println("We have " + menu[i] + " for " + price[i] + "€");
}
int yes = 1;
int no = 0;
Scanner yesOrNo = new Scanner(System.in);
System.out.println("What do you want to eat?");
List<String> Order = new ArrayList<String>();
do {
Scanner inputOrder = new Scanner(System.in);
String userOrder = inputOrder.next().toLowerCase();
for (int j = 0; j < menu.length; j++) {
if (userOrder.equals(menu[j])) {
Order.add(userOrder);
System.out.println("You ordered" + Order + "\nSomething more?" + "\nSay YES with 1 and NO with 0");
} else if (userOrder.equals(menu[j]) == false) {
System.out.println("We don't have this on menu!"
+ "\nDo you want to order another thing that we do have in menu?" + "\nSay YES with 1 and NO with 0");
} else if (yesOrNo.nextInt() == no) {
System.out.println("Order finished!");
}
}
} while (yesOrNo.nextInt() == yes);
}
}```
There is no need to re-declare the Scanner object every iteration, or to have more than one Scanner object that will read from the same source. Also, there is no need to print out "I didn't find it!" over every comparison.
Consider the following block of code:
.
.
.
// there is no need to re-declare the Scanner object every iteration, or two scanners
Scanner keyboard = new Scanner(System.in);
do {
String userOrder = keyboard.next().toLowerCase();
// we haven't found the item
boolean found = false;
// integer option (yes or no)
int option;
// do the search
for (int j = 0; j < menu.length; j++) {
// do this if found
if (userOrder.equals(menu[j])) {
// set flag to indicate we found the item
found = true;
// add the order
Order.add(userOrder);
// print out, then read the user's option
System.out.println("You ordered" + userOrder + "\nSomething more?" + "\nSay YES with 1 and NO with 0");
option = keyboard.nextInt();
// consume the NL leftover from the nextInt() call
keyboard.nextLine();
// break out of the for loop since you already found what you're looking for
break;
}
}
// check if you didn't find the item
if (!found) {
// print out
System.out.println("We don't have this on menu!"
+ "\nDo you want to order another thing that we do have in menu?" + "\nSay YES with 1 and NO with 0");
// read if user wants to continue
option = keyboard.nextInt();
// consume the NL leftover from the nextInt() call
keyboard.nextLine();
}
} while (option == yes);
System.out.println("Order finished!");
.
.
.
// close the scanner when you're done
keyboard.close();
.
.
.
I have refactored your code . try to see in comparing tool .
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Fase2_final {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] menu = { "chicken", "steak", "hamburger", "spaghettis", "pizza" };
int[] price = { 10, 15, 20, 5, 12 };
boolean order = true;
for (int i = 0; i < menu.length; i++) {
System.out.println("We have " + menu[i] + " for " + price[i] + "€");
}
int decision = 0;
int yes = 1;
int no = 0;
Scanner yesOrNo = new Scanner(System.in);
System.out.println("What do you want to eat?");
List<String> Order = new ArrayList<String>();
do {
Scanner inputOrder = new Scanner(System.in);
String userOrder = inputOrder.next().toLowerCase();
for (int j = 0; j < menu.length; j++) {
if (userOrder.equals(menu[j])) {
Order.add(userOrder);
System.out.println("You ordered" + Order + "\nSomething more?" + "\nSay YES with 1 and NO with 0");
} else if (userOrder.equals(menu[j]) == false) {
System.out.println("We don't have this on menu!"
+ "\nDo you want to order another thing that we do have in menu?" + "\nSay YES with 1 and NO with 0");
}
}
decision = yesOrNo.nextInt();
if (decision == no) {
System.out.println("Order finished!");
}
} while (decision == yes);
}
}
Always close the scanner when you are done .
the nextInt() method returns the current input one time, if you call it twice it needs a second input. It could should look like this:
do {
Scanner inputOrder = new Scanner(System.in);
String userOrder = inputOrder.next().toLowerCase();
int answer = 0;
for (int j = 0; j < menu.length; j++) {
if (userOrder.equals(menu[j])) {
Order.add(userOrder);
System.out.println("You ordered" + Order + "\nSomething more?" + "\nSay YES with 1 and NO with 0");
} else if (userOrder.equals(menu[j]) == false) {
System.out.println("We don't have this on menu!"
+ "\nDo you want to order another thing that we do have in menu?" + "\nSay YES with 1 and NO with 0");
} else if ((answer = yesOrNo.nextInt()) == no) {
System.out.println("Order finished!");
}
}
} while (yesOrNo.nextInt() == answer);
I hope I answered your question.

Repeated letters logic not working in java

I am working on a version of hangman, and I need to include a condition that checks if a letter guess was already used. My repeated letters if statement is not working correctly. Any advice?
NOT FULL CODE. ONLY A PIECE IS SHOWN
char[] repeatedLetters = new char[26];
int incorrect = 0;
while (incorrect < 7)
{
System.out.println("\nGuess a letter: ");
char guess = kb.next().toUpperCase().charAt(0); // case insensitive
for (int i = 0; i < repeatedLetters.length; i++)
{
if (repeatedLetters[i] == guess) {
System.out.println("You already guessed " + guess + ".");
System.out.println("Guess a letter: ");
guess = kb.next().toUpperCase().charAt(0);
}
else
repeatedLetters[i] = guess;
}
Personally, I would suggest using a List instead of array.
List<Character> repeatedLetters = new ArrayList<>();
int incorrect = 0;
while (incorrect < 7)
{
System.out.println("\nGuess a letter: ");
char guess = kb.next().toUpperCase().charAt(0); // case insensitive
if (validateCharacter(guess) && repeatedLetters.contains(guess)) {
System.out.println("You already guessed " + guess + ".");
continue;
}
else {
repeatedLetters.add(guess);
}
// Other things
}
If you are not allowed to use a list, then you need to move the else block outside of the for loop, use a labelled while loop, and also manually count the number of repeated characters.
int repeatedCount = 0;
getInput : while (incorrect < 7) {
// ......
for (int i = 0; i < repeatedCount; i++) {
if (repeatedLetters[i] == guess) {
System.out.println("You already guessed " + guess + ".");
continue getInput;
}
}
repeatedLetters[repeatedCount] = guess;
repeatedCount++;

Java loop for multiple projects

I am working in java and I want my loop to run for as long as there are projects as specified by the user. As of now, the code runs through for one iteration, and reads the results of the one loop, but it won't continue any further.
If the user says there are 3 projects, I want the code to run through the loop 3 times and tell me the total of each project. Right now, it tells me the total of 1 project whether I specify the number of projects are 1 or 5.
double projectBoardFootage = 1.0;
double projectBoardFootageTotal = 0.0;
int i = 0;
System.out.println("How many projects do you want to estimate?");
int numberOfProjects = scan.nextInt();
for(i = 0; i < numberOfProjects; ++i) {
while (projectBoardFootage > 0) {
System.out.println("Enter your board footage for Project #" + (i + 1) + " (0 to exit)");
projectBoardFootage = scan.nextDouble();
projectBoardFootageTotal += projectBoardFootage;
}
System.out.println("The raw board footage for Project #" + (i + 1) + " is: " + projectBoardFootageTotal);
}
Perhaps you want to reinitialize the total board footage to clear the previous iteration count?
for(i = 0; i < numberOfProjects; ++i) {
projectBoardFootageTotal = 0.0;
while (projectBoardFootage > 0) {
System.out.println("Enter your board footage for Project #" + (i + 1) + " (0 to exit)");
projectBoardFootage = scan.nextDouble();
projectBoardFootageTotal += projectBoardFootage;
}
System.out.println("The raw board footage for Project #" + (i + 1) + " is: " + projectBoardFootageTotal);
}
The problem in your code is that you're using the variable projectBoardFootage to terminate the loop.
Think about what happens when this value is set to 0.
We exit the while-loop, and the value never gets changed again. So from that point on, every iteration will end immediately.
The better solution is to do something like this:
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("How many projects do you want to estimate?");
int numberOfProjects = scan.nextInt();
for(int i = 0; i < numberOfProjects; ++i) {
foobar(scan, i+1);
}
}
private static void foobar(Scanner scan, int projectNr)
{
boolean wantsToQuit = false;
double projectBoardFootageTotal = 0;
while (!wantsToQuit) {
System.out.println("Enter your board footage for Project #" + projectNr + " (q to exit)");
String tmp = scan.next();
if(tmp.equalsIgnoreCase("q"))
wantsToQuit = true;
else
projectBoardFootageTotal += Double.parseDouble(tmp);
}
System.out.println("The raw board footage for Project #" + projectNr + " is: " + projectBoardFootageTotal);
}
This code also ensures the user enters a more sensible keycode than '0' to quit.

A "Stick Game" program in Java not working correctly?

I've recently decided that I want to make a program that plays a game called "Nim," which is a game in which you start with a predetermined amount of "sticks" and each player takes turns removing between 1 and 3 sticks. Whoever removes the last stick loses.
Anyway, I have written my program and it compiles and runs almost flawlessly. There's only one small problem. After the game is over, it shows the "good game" screen twice, with the game's very first line appearing in the middle (I'll post screenshots at the end here). It's very strange, and I was just wondering if you guys could give it a look.
I'm cutting a chunk of the program out (only one class, named Cup()), because it's somewhat long, so if you see a class you don't recognize then just ignore it. It's pretty self explanatory what the class does in the program, and it's not where the error is occurring. Here's the code.
class SticksGame
{
public static void main(String[] args) throws InputMismatchException
{
Random r = new Random();
int score1 = 0, score2 = 0;
Cup c = new Cup();
int j = 0, d = 0, i = 0, k = 0;
boolean b = true;
String exit = "default";
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Sticks Game! Last Stick loses! Must pick 1 - 3 sticks.");
System.out.println();
do
{
i = r.nextInt(15) + 9;
System.out.println("We begin with " + i + " sticks");
System.out.println();
while (b == true)
{
System.out.println("Your move");
k = input.nextInt();
if (k > 3)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else if (k < 1)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else
{
j = i;
i = i - k;
if (i <= 0)
{
System.out.println("Computer wins!");
score2 = (score2 + 1);
b = false;
}
else
{
System.out.println("We now have " + i + " sticks.");
}
d = c.select();
System.out.println("Computer removes " + d + " sticks");
i = i - d;
System.out.println("We now have " + i + " sticks");
if (i <= 0)
{
System.out.println("You Win!");
score1 = (score1 + 1);
b = false;
}
}
}
System.out.println();
System.out.println("Good game!");
System.out.println("Your score: " + score1 + " Computer's Score: " + score2);
System.out.println("Press enter if you'd like to play again. Otherwise, type \"quit\"");
exit = input.nextLine();
b = true;
}
while(!"quit".equals(exit));
}
}
Any helps are appreciated! Thanks :)
~Andrew
CODE EDITED FOR JANOS
A little late, I know, but here is the FULL GAME for anyone who wants to play! feel free to copy and paste it into your notepad and execute using cmd(YOU MUST KEEP MY NAME AS A COMMENT ON TOP!) :)
//Andrew Mancinelli: 2015
import java.util.*;
import java.io.*;
class Cup
{
private ArrayList<Integer> c = new ArrayList<Integer>();
public Cup()
{
c.add(1);
c.add(2);
c.add(3);
}
public int count()
{
return c.size();
}
public int select()
{
int index = (int)(c.size() * Math.random());
return c.get(index);
}
public void remove(Integer move)
{
c.remove(move);
}
}
class SticksGame
{
public static void help()
{
System.out.println();
System.out.println("Okay, so here's how it works... The object of the game is to NOT have the last stick. Whoever ends up with the very last stick loses.");
System.out.println();
System.out.println("Rule 1: You will each take turns removing sticks. you may only remove 1, 2, or 3 sticks in a turn");
System.out.println();
System.out.println("Rule 2: The beginning number of sticks is always random between 9 and 24 sticks");
System.out.println();
System.out.println("Rule 3: Whoever chooses the last stick, LOSES!");
System.out.println();
System.out.println("And that's it! Simple, right?");
}
public static void main(String[] args) throws InputMismatchException
{
Random r = new Random();
int score1 = 0, score2 = 0;
Cup c = new Cup();
int j = 0, d = 0, i = 0, k = 0;
boolean b = true;
String exit = "default", inst = "default";
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Sticks Game! Last Stick loses!");
System.out.println();
System.out.println("Need some instructions? Type \"help\" now to see the instructions. Otherwise, press enter to play!");
inst = input.nextLine();
if (inst.equals("help"))
{
help();
System.out.println();
System.out.println("press \"enter\" to begin!");
inst = input.nextLine();
}
do
{
i = r.nextInt(15) + 9;
System.out.println();
System.out.println("We begin with " + i + " sticks");
System.out.println();
while (b == true)
{
System.out.println("Your move");
k = input.nextInt();
if (k > 3)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else if (k < 1)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else
{
j = i;
i = i - k;
if (i <= 0)
{
System.out.println("Computer wins!");
score2 = (score2 + 1);
b = false;
break;
}
else
{
System.out.println("We now have " + i + " sticks.");
}
d = c.select();
i = i - d;
if (i >= 0)
{
System.out.println("Computer removes " + d + " sticks");
System.out.println("We now have " + i + " sticks");
}
if (i <= 0)
{
System.out.println("You Win!");
score1 = (score1 + 1);
b = false;
break;
}
}
}
System.out.println();
System.out.println("Good game!");
System.out.println("Your score: " + score1 + " Computer's Score: " + score2);
System.out.println("Press enter if you'd like to play again. Otherwise, type \"quit\"");
input.nextLine();
exit = input.nextLine();
b = true;
}
while(!"quit".equals(exit));
}
}
The problem is that this condition is always true:
while (exit != "quit");
Because != means "not identical",
and the exit variable and "quit" are not identical.
Use the equals method for checking logical equality.
In this example, change the loop condition to this instead:
while (!"quit".equals(exit));
For your other problem of not properly starting a second game,
you need to reinitialize the state variables,
for example reset b = true.
Lastly, note that input.nextInt() doesn't read the newline character that you pressed when entering a number. So when exit = input.nextLine() runs, it reads that newline character, and doesn't actually give you a chance to type "quit". To solve this, add input.nextLine(); right before exit = input.nextLine();
The unexpected retry was because of the use of input.nextLine(); the program assumed that you already pressed [enter].
From previous work, the two options is to insert one more input.nextline();
input.nextLine();
exit = input.nextLine();
Or use input.next(); instead, although enter will not work for this method so you may need to enter any key or "quit" to exit;
exit = input.next();

While loop in Java with Multiple conditions

Can someone help me figure out why the while statement isn't working? The loop does stop after i = 3 but won't stop if continueSurvey = 0. It runs but it won't quit the loop if I change continueSurvey to O. Even if I step into the processes and I can see that the variable is 0, the loop continues.
import java.util.Scanner;
public class SurveyConductor
{
public static void main(String[] args)
{
Survey a = new Survey();
a.display();
a.enterQuestions();
int continueSurvey = 1;
int i = 0;
while ((continueSurvey != 0) && (i < 3))
{
for (int row = a.getRespID(); row < 3; row++)
{
System.out.println("Respondent " + (row+1) + " Please tell us how you would rate our: ");
for (int col = 0; col < 3; col++)
{
Scanner input = new Scanner(System.in);
System.out.println(a.presentQuestion(col) + ": ");
System.out.println("Enter your response (1-Strongly Disagree, 2-Disagree, 3-Neutral, 4-Agree, 5-Strongly Agree): ");
int response = input.nextInt();
if ((response < 1) || (response >5))
{
while ((response < 1) || (response > 5))
{
System.out.println("Your response must be between 1 and 5. Please try again.");
System.out.println(a.presentQuestion(col) + ": ");
System.out.println("Enter your response (1-Strongly Disagree, 2-Disagree, 3-Neutral, 4-Agree, 5-Strongly Agree): ");
response = input.nextInt();
}
}
a.logResponse(row,col,response);
System.out.println();
}
a.displaySurveyResults();
System.out.println();
System.out.println("The top rated question is Question #" + a.topRatedQuestion() + ".");
System.out.println("The bottom rated question is Question #" + a.bottomRatedQuestion() + ".");
System.out.println();
Scanner input2 = new Scanner(System.in);
System.out.println("Are there any more repondents (0 - No, 1 - Yes): ");
continueSurvey = input2.nextInt();
a.generateRespondentID();
i++;
}
}
}
}
You need to add a break inside your for loop. IE,
if(continueSurvey == 0)
break;
This will exit the for loop and allow the while loop to exit.
The part where you ask if the user wants to continue is inside this for loop
for (int row = a.getRespID(); row < 3; row++)
not just your while loop. This means it will keep asking until the for loop is done, only quitting when it finally gets back around to the while loop condition.
Your condition in the while loop is:
((continueSurvey != 0) && (i < 3))
which means that the inner block of the while loop will be executed if and only if continuSurvey != 0 and i < 3 in the same time. You have inner loops which have different conditions. I would search for the problem in the inner loops using a debugger. If this answer is not enough for you, then please specify what would you want to achieve.
if you want to exit the loop if either continueSurvey is 0 OR i=3
you have to write the while loop like this:
while((continueSurvey != 0) || (i < 3)) {
...
}
the && (and) operator symbolises that both conditions have to be true in order for the loop to exit not one of them (|| or).

Categories