Click to See Complete Forum and Search --> : java help needed


dj1anddt2
03-22-2003, 05:42 PM
i need help finding out how to make this a program.. ive made some of it but am not sure about making the switch statments expression accurate help me if you can please....
i am supposed to write a java program that asks the user to type A, B C or Q. when the user types Q the program ends. when the user types A,B, or C, the program displays the message "good job!" and asks for another input. anything else typed, it prints an error message. then asks for another input of a, b, c, or d. the program should have while loop or the do while loop

import javax.swing.JOptionPane;
class exp
{
public static void main(String args[])
{
String response;
response = JOptionPane.showInputDialog(null, "Type a letter A,B,C or Q!" );

if (response == null)
{
JOptionPane.showMessageDialog(null, "*** Error *** You clicked on the cancel button");
JOptionPane.showInputDialog(null, "This time type A,B,C, or Q");
}
else
if (response.equals(""))
{
JOptionPane.showMessageDialog(null, "*** Error ***\n You have to make an entry in the Input Box");
JOptionPane.showInputDialog(null, "Try ggain,\n this time enter A,B,C, or Q!");
}

switch (response)
{
// after user typed a letter


case 1:
(response.equals ("A"));
JOptionPane.showMessageDialog(null, "GOOD JOB!");
break;

case 2:
(response.equals("B"));
JOptionPane.showMessageDialog(null, "GOOD JOB!");

break;
case 3:
(response.equals("C"));
JOptionPane.showMessageDialog(null, "GOOD JOB!");

break;
case 4:
(response.equals("Q"));
System.exit(0);

break;
default:
JOptionPane.showInputDialog(null, "Try again\n Type a letter A, B, C or Q");
System.exit(0);
}
}
}

jce_step
03-22-2003, 06:15 PM
Hi,
as i could understand from your post your trying to do several questions to the user and depending on the user answers, you ask another question to the user or end the program. For this part of the progrm you should create a cycle to repeat the statments you want to repeat, for that you can use the FOR or While statment's.

About the Switch statment i can tell you how it works,

switch (x){

case 1: doSomething1; // case x == 1 executes this line...

case 2: doSomething2; // case x == 2 executes this line...

...
}

!!! In C++ the switch statment only works if you are working with INTEGERS, or for any inteager value assumed on 'x' variable. I'm not sure if this also aplyies in JavaScript !!!

AnyWay you told that you want to work with chars (A,b,C and Q), if so i suggest you use the classic IF statment.

jce_step
03-23-2003, 06:25 PM
Ok, but in C++ it was disigned to work with Inteagers, i was not sure about JavaScript.

khalidali63
03-23-2003, 06:51 PM
Originally posted by jce_step
Ok, but in C++ it was disigned to work with Inteagers, i was not sure about JavaScript. ..

Well for the original post,the question is about Java.And yes in c/c++ and Java swithc statement parameter could only be a char or an integer value.

Javascript on the other hand can cpmpare to char.string as well as numner...nice right?

This thread, to be the least should be in general sec..
:-)

Oh by the way the above program in java would not do what the thread owner indended it to.

:p

Cheers

Khalid