Business & Finance0 min ago
Java Programme
3 Answers
Im making a programme for my assignment which takes in user input via a JOptionPane inputbox. the programme should take the user input and give a diagalogue back.
for example for a deck of cards user types A. the program should return Ace, K - King, J - Jack, Q - Queen 2..10 should return two-ten etc
ive got it to recognise the numbers as ive converted the String into datatype int. and used the if statement e.g
if(input == 2)
System.out.println(''Two'');
but i dont know how i can get it to recognize Letters and return words... can anyone help?
for example for a deck of cards user types A. the program should return Ace, K - King, J - Jack, Q - Queen 2..10 should return two-ten etc
ive got it to recognise the numbers as ive converted the String into datatype int. and used the if statement e.g
if(input == 2)
System.out.println(''Two'');
but i dont know how i can get it to recognize Letters and return words... can anyone help?
Answers
Best Answer
No best answer has yet been selected by AutomaticGal. Once a best answer has been selected, it will be shown here.
For more on marking an answer as the "Best Answer", please visit our FAQ.tried that it doesnt work...... :-(
ive tried changing the datatype to char but as im using JOptionPane it doesnt recognize it. ive added a line afterwards to change the string input to an int but i have given the int another name. look
Cards()
{
input1 = JOptionPane.showInputDialog("Enter Character corisponding to card value:");
num1 = Integer.parseInt(input1);
if (num1 == 2)
System.out.println("Two");
if(input1 == "A")
if(input1 == "a")
System.out.println("ace")
ive also tried .= != tried initializing the String out of the constructor... im stuckkkkkkkkkkkk
ive tried changing the datatype to char but as im using JOptionPane it doesnt recognize it. ive added a line afterwards to change the string input to an int but i have given the int another name. look
Cards()
{
input1 = JOptionPane.showInputDialog("Enter Character corisponding to card value:");
num1 = Integer.parseInt(input1);
if (num1 == 2)
System.out.println("Two");
if(input1 == "A")
if(input1 == "a")
System.out.println("ace")
ive also tried .= != tried initializing the String out of the constructor... im stuckkkkkkkkkkkk
Try a switch statement:
switch input;
{
case "a": //if input ==a
doSomethingHere;
break;
case "b": //if input ==b
doSomethingHere;
break;
}
You can also do this:
switch input;
{
case "a", case "b": //same as if input == a or b
doSomethingHere;
break;
}
http://www.cafeaulait.org/course/week2/42.html
switch input;
{
case "a": //if input ==a
doSomethingHere;
break;
case "b": //if input ==b
doSomethingHere;
break;
}
You can also do this:
switch input;
{
case "a", case "b": //same as if input == a or b
doSomethingHere;
break;
}
http://www.cafeaulait.org/course/week2/42.html