The problem in my code is that the first "if " statement and the "else if " statement are both not executed even though the conditions are fulfilled yet the "else " statement is executed. So despite the users input the output at all times is the same.
if condition:
if (
((user == "Rock") && (rand == 0)) ||
((user == "Paper") && (rand == 1)) ||
((user == "Scisors") && (rand == 2))) {
System.out.println("Draw");
}
else if (
((user == "Rock") && (rand == 2)) ||
((user == "Paper") && (rand == 0)) ||
((user == "Scisors") && (rand == 1))) {
System.out.println("You won!");
w = w + 1;
System.out.println("Win streak = "+w);
}
else {
System.out.println("You lost");
w = 0;
}
Whole code:
import java.util.Scanner;
class g1{
public static void main(String args[]){
Scanner obj = new Scanner(System.in);
String[] a = {"Rock","Paper","Scisors"};
int w = 0;
for(int i = 0;i >= -1/12; i++){
System.out.print(": ");
String user = obj.nextLine();
System.out.println();
int rand = (int)(Math.random()*3);
if (((user == "Rock") && (rand == 0)) || ((user == "Paper") && (rand == 1)) || ((user == "Scisors") && (rand == 2))){
System.out.println("Draw");
}
else if(((user == "Rock") && (rand == 2)) || ((user == "Paper") && (rand == 0)) || ((user == "Scisors") && (rand == 1))){
System.out.println("You won!");
w = w + 1;
System.out.println("Win streak = "+w);
}
else{
System.out.println("You lost");
w = 0;
}
System.out.println("Rand = "+rand+" "+"Choice = "+a[rand]);
}
}
}
I printed the values of the random integer and the value of the random integer in the array "a" just to be clear so, it seems to be like the if and else if statements are not executed.
Here are the outputs:
: Rock
You lost
Rand = 2 Choice = Scisors
: Rock
You lost
Rand = 2 Choice = Scisors
: Rock
You lost
Rand = 0 Choice = Rock
: Rock
Aucun commentaire:
Enregistrer un commentaire