Hello - I am a brand new at Java and I am having a hard time with a program I have to turn in tomorrow. I can not get the 'Q' option to work and the loop goes on forever. I've tried to go over the tutorials but when I change it from here I then get compiling issues... Can someone explain what I've done wrong? -
import javax.swing.JOptionPane;
-
public class LloydPage_Assignment5 {
-
public static void main (String [] args) {
-
-
// Enter Y to vote yes
-
// Enter N to vote no
-
// Enter Q to quit voting
-
int quitVote = 0;
-
int yesVote = 0;
-
int noVote = 0;
-
-
// charAt looks at number in parenthesis, returns char at that location
-
// within string (the first character is at location 0)
-
// firstChar is going to be 'Y' or 'N' or 'Q' etc.
-
// Essentially it is a way to make a string into a char so that you may use string function
-
-
String reply1 = JOptionPane.showInputDialog
-
(null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
-
char userVote = reply1.charAt(0);
-
-
// If the user enters Y or y then add 1 to the number of yes votes
-
-
do {
-
yesVote = yesVote + 1;
-
reply1 = JOptionPane.showInputDialog
-
(null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
-
userVote = reply1.charAt(0);
-
} while ((userVote == 'Y') || (userVote == 'y'));
-
-
// If the user enters N or n then add 1 to the number of no votes
-
-
do {
-
noVote = noVote + 1;
-
reply1 = JOptionPane.showInputDialog
-
(null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
-
userVote = reply1.charAt(0);
-
} while ((userVote == 'N') || (userVote == 'n'));
-
-
// If the user enters any variable other than Y||y, N||n, Q||q then ignore vote
-
-
do {
-
reply1 = JOptionPane.showInputDialog
-
(null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
-
userVote = reply1.charAt(0);
-
} while ((userVote != 'Y') || (userVote != 'y') || (userVote != 'N') || (userVote != 'n') || (userVote != 'Q') || (userVote != 'q'));
-
-
// If the user enters Q or q then
-
// use a showConfirmDialog box
-
// to ask if they really want to quit
-
-
do {
-
quitVote = JOptionPane.showConfirmDialog
-
(null, "Are you sure you wish to Quit?");
-
} while ((userVote == 'Q') || (userVote == 'q'));
-
-
// Otherwise the loop ends and the totals must be shown in a
-
// showMessageDialog box
-
-
do {
-
JOptionPane.showMessageDialog(null, "The total is number of Yes votes is " + yesVote + "\n The total is number of No votes is " + noVote);
-
} while (quitVote == JOptionPane.YES_OPTION);
-
-
}
-
}
1 4547
Your logic is incorrect; you want to collect Y or N votes until the user wants to
quit; then you want an additional confirmation from the user. If the user really
wants to quit you display the totals and quit. Otherwise you just continue.
Check your text book for the switch() statement and don't be afraid to add a
few simple methods for sub-tasks. Here's a first idea: -
for (char vote= 'A'; !quitVote(vote); ) {
-
vote= getVote();
-
switch (vote) {
-
case 'y': case 'Y': yesVote++; break;
-
case 'n': case 'N': noVote++; break;
-
case 'q': case 'Q': quitVote++; break;
-
default: illegalVote(vote);
-
}
-
}
-
The getVote() and illegalVote() methods handle what they have to handle.
The quitVote() method checks whether or not the user really wants to quit.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sybren Stuvel |
last post by:
Hi there,
Is it possible to use an assignment in a while-loop? I'd like to do
something like "loop while there is still something to be read, and if
there is, put it in this variable". I've been...
|
by: Wayne Folta |
last post by:
Two observations about PEP-315:
1. It's clever, addresses a definite "wart", and is syntactically
similar to try/except. But it's syntax seems like an acquired taste to
me.
2. It is a very...
|
by: John Pass |
last post by:
What is the difference between a While and Do While/Loop repetition structure.
If they is no difference (as it seems) why do both exist?
|
by: morpheus |
last post by:
Hi Group,
When I run this:
# include <stdio.h>
int main(){
int c=0;
while ( (c=getchar()) != EOF && c != ' ' && c != '\t' )
printf("foo");
if (c == '8')
putchar(c);
|
by: aprilmae36 |
last post by:
Nice day to all... Here I am again, having another problem with loops... lol... Can you please help me make a while loop program that the output will be like this:
1
121
12321
1234321...
|
by: Jan Schmidt |
last post by:
Hi,
in a nested do-while-loop structure I would like to "continue" the outer
loop. With goto this should be no problem in while-loops. However, for
do-while I cannot get it to work (without a...
|
by: archeryguru2000 |
last post by:
Hello, I've been stumped for several weeks on this particlar program. At work here, we have 30 machines that we record scrap data on. I take this data and create spreadsheets (boring) that can be...
|
by: recordlovelife |
last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But...
|
by: Kerem Gümrükcü |
last post by:
Hi All,
i am too tired now to find this out by my own, but why does
my while loop "while(readline==Console.ReadLine())" needs
twice return hit to read the line, after setting...
|
by: cokofreedom |
last post by:
First off let me state that I really enjoy using Python. I am a 3rd
year student and have been using python for 3 months, (thanks to
trac!). I do not consider myself an experienced or clever...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |