I have a java switch function that is:
while(i < 1) {
switch(hi) {
case "Hi":
case "hi":
case "Hi!":
case "hi!":
case "Hello!":
case "hello!":
case "Hello":
case "hello":
System.out.println("Hi!");
break;
case "How are you?":
x = Math.random() * Emotion.length;
Feeling = (int)x;
System.out.println(Emotion[Feeling] + " How are you?");
break;
default:
break;
} Hi.nextLine();
}
There are no errors in debug, but the output is this:
"Me: Hi,
Computer: Hi!,
Me: How are you?,
Computer: Hi!,"
How do I make it so I can use switch loop many times without getting the same result every time?
All help would be appreciated.
The problem is that you assign the hi variable before the loop, and never re-assign it later. You should assign the value of Hi.nextLine(); to hi within the loop as follows:
hi = Hi.nextLine();
Related
Good Evening,
I created this method for a class. I used a switch/case to execute depending on the value of expression. I included an if-else method for each case. I do get an error on case 1-> switch rules are a preview feature and are disabled by default. I attempted to add a : after case 1 and case 2but my results reached high numbers for the sets. I changed the : to -> and it worked appropriately. Now I am wondering if this was a proper way to set the case statements or should it be written differently.
private void playGame()
{
double winCheck = Math.random();
switch (matchServer) {
case 1 ->{
if (winCheck <= player1WinProb)
player1GamesWon++;
else
player2GamesWon++;
matchServer = 2;
}
case 2 ->{
if (winCheck <= player2WinProb)
player2GamesWon++;
else
player1GamesWon++;
matchServer = 1;
A correct switch statement must use ':'
Also, 'break' is missing. This to avoid executing next cases.
You can add 'default' that means that case 1 and case 2 were not presented.
switch (matchServer) {
case 1:
if (winCheck <= player1WinProb)
player1GamesWon++;
else
player2GamesWon++;
matchServer = 2;
break;
case 2:
if (winCheck <= player2WinProb)
player2GamesWon++;
else
player1GamesWon++;
matchServer = 1;
break;
default:
//If it was not 1 or 2
//Printing the values can help
}
Is there a way to grab the resulting number from each iteration of this loop and compare it to the next?
This is a Slot Machine Sim in Java,
I'm trying to find a way to see how many of the results match.
so I thought I would capture the number that is resulted from each round of the For loop and compare it to the previous one.
but I have no idea how to write that?
is there a better way to do this?
what I have so far:
for (int count=1; count<= 3 ; ++count)
{
number = slotM.nextInt(6);
switch (number)
{
case 0:
System.out.print("-cherries-");
break;
case 1:
System.out.print("-Oranges-");
break;
case 2:
System.out.print("-Palms-");
break;
case 3:
System.out.print("-Bells-");
break;
case 4:
System.out.print("-Melones-");
break;
default:
System.out.print("-Bars-");
break;
}
System.out.print(number);
}
Yep there are several better ways. If you have a fixed number of options (6 in your case) an enum might be a good option:
enum Picture {
CHERRIES, ORANGES, PALMS, BELLS, MELONS, BARS;
public String getName() {
return "-" + name().substring(0, 1) + name().substring(1).toLowerCase() + "-";
}
That way you can store your numbers as pictures rather than numbers.
Picture pictures[3];
Random random = new Random();
for (int i = 0; i < pictures.length; i++)
picture[i] = Picture.values[random.nextInt(pictures.length)];
To get the printed version:
for (Picture picture: picture)
System.out.print(picture.getName());
You’ll need some kind of storage outside of the loop so that each iteration can reference it.
int[] results Look in to arrays - you can put the results of each round into a part of the array, and look up the value.
You are declaring your count variable in the for loop, just declare outside and make a comparison with it
switch (event.getCode()) {
case UP: Does Something; break;
case RIGHT: Does Something; break;
case DOWN: Does Something; break; break;
case LEFT: Does Something; break; break;
case NUMPAD1: Does Something; break;
This is probably a super simple answer but I just can't find it on the internet. The code works perfectly that's why I haven't shown it but I just want a Case Key for the number 1 not the NUMPAD 1 as shown in the code. 1 as in the key above Q.
Thanks in advance!
These seem to be enum constants from JavaFX's KeyCode.
According to these docs, the enum constant for key 1 is DIGIT1
Just add:
case 1: Does Something;break;
so here is my case to make a long code short.
Let's say, we have a JOptionPane with 3 buttons.
boolean loopGameInterface = true;
while(loopGameInterface){
int chooseGame = JOptionePane........
switch(chooseGame) {
case 0:
case 1:
case 2:
System.exit(0);
}
}
So the problem is, when I click (example) second button, it goes to case 1. That's fine. But when the code inside case 1 is executed, it goes directly to case 2 and exit my program, instead of just looping the gameInterface?
You need to add break; at the end of each case. This is true for all switch statements by the way, not just when you are using a JOptionPane
switch(chooseGame) {
case 0: /* Your code */
break;
case 1: /* Your code */
break;
case 2:
System.exit(0);
default : "Give some default case too"
}
Its because you haven't added the break statement. If you are not adding break all the cases below the case which matches will be executed. For eg:
switch(ch) {
case 1:
/* some code without break */
case 2:
/* some code without break */
case 3:
System.exit(0);
}
In the above example if ch=1 then all case 2 will also be executed and then case 3.
If ch=2 then only case 2 and case 3 will be executed since case 3 is below case 2. So you need to add break after each case.
I am writing a program which contains ComboBox where you choose text and this text will be converted to coefficient using switch statment in program. The problem is that I can't use varible outside switch statment. I really appreciate if someone could help.
Code sample:
double Cst1;
String s = Cst.getSelectedItem().toString();
switch (s) {
case "ABC":
Cst1 = -25;
break;
case "CBA":
Cst1 = -10;
break;
case "BCA":
Cst1 = 0;
break;
case "ACB":
Cst1 = 10;
break;
default:
answer.setText("ERROR");
break;
}
double C14 = 9 * Cst1;
If you get an error which says that the variable is not initialized, try this:
double Cst1 = 0.;
If you have a string value that isn't explicitly covered in your switch, Cst1 will never be initialized. For example, the string "AAA" will fall into the default case.
There are two possible fixes:
Initialize Cst1 like
double Cst1 = 0.
Set Cst1 in your default case like
default:
Cst1 = 0.;
// Display your error
break;