tags:

views:

119

answers:

1
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?

A: 

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.

matt b
matt is right: move the two last statement inside the loop (and replace "y" by "j" since it's in german)
Maurice Perry
@Mauri German? Are you sure (j/n)?
bzlm
@bzlm (n) is the correct answer. [Beklager! Mitt tall er mindre. Prøv igjen.](http://translate.google.com/#auto|en|Beklager!%20Mitt%20tall%20er%20mindre.%20Pr%C3%B8v%20igjen.)
Andreas_D
Hmmm... Sweedish?
Maurice Perry
@Mauri Close, but no cigar. :)
bzlm