package myProg;
import java.util.开发者_如何学运维Scanner;
public class oving4a {
/**
*Øving 4a. Aleksander Pettersen
*/
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int i;
int count = 0;
String another = "y";
double symbol =(1+Math.random()*100);
int svar = (int) symbol;
System.out.println("Tipp et tall:");
i = scan.nextInt();
while (another.equalsIgnoreCase("y")){
while (i != svar){
if (i < 1 | i > 100)
System.out.println("Vennligst velg et tall mellom 1 - 100");
else
{
count++;
if(i > svar)
System.out.println("Beklager! Mitt tall er mindre. Prøv igjen.");
else
System.out.println("Beklager! Mitt tall er større. Prøv igjen.");
i = scan.nextInt();
}
}
}
System.out.println("Gratulerer! \n Du klarte det på " +count + " forsøk. Vil du prøve en gang til(j/n)?");
another = scan.nextLine();
}
}
If I run it with the equalsIgnoreCase
, it'll just freeze when I guess the right question.
How to implement "try again (j/n)" in this program?
You have code like this
String another = "y";
...
while (another.equalsIgnoreCase("y")) {
...
}
another = scan.nextLine();
It's only possible for another
to be set to a value other than "y" outside of the while loop. There is nothing inside the while loop that will change the condition another.equalsIgnoreCase("y")
to false
.
精彩评论