Reputation: 99
Want it to loop through everything until the user puts in the right number. When the user puts in wrong number it should say "type in a different number". When the user puts in the right number it should say "congrats you won". But until then it will loop and say "type in a different number" and after 5 tries I want it to say "you failed this mission! do you want to try again?"
If they guess it on 1 try they will be giving 500 dollar and second try 400 dollar and so on until 5 tries.
import javax.swing.*;
import java.util.Random;
public class Projekt_1 {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "WELCOME TO GUESS GAME!" + "\nYou gonna guess a number between 1 and 20 " + "\nYou have 5 tries to guess the number! " + "\nYou gonna get more money on less tries, highest win are 500 dollar on one try! " + "\nHOPE YOU LIKE IT :)");
Random talet = new Random();
int secretnumber = talet.nextInt(20) + 1;
int tries = 0;
int money = 600;
String number;
int guess;
boolean win = false;
while (win == false) {
number = JOptionPane.showInputDialog("Guess a number between 1 and 20");
guess = Integer.parseInt(number);
tries++;
if (guess == secretnumber) {
win = true;
} else if (guess > secretnumber) {
JOptionPane.showInputDialog("Your number is to low :(" + "\nType in a higher number!");
guess = Integer.parseInt(number);
} else if (guess < secretnumber) {
JOptionPane.showInputDialog("Your number is to high :(" + "\nType ina a lower number!");
guess= Integer.parseInt(number);
}
}
JOptionPane.showMessageDialog(null, "Congrats you won!" + "\nYour number was " + secretnumber + "\nit took you " + tries + "tries");
}
}
Upvotes: 3
Views: 664
Reputation: 3332
Adding an additional condition in while
loop will work. If försök
is the number of tries which can have maxium value of 5
(in your case) then condition should be :
while (win == false && försök<5) //if försök starts from 0
{
//code
if(win==true)
break;
försök++;
}
To display the message and score you can just check the value of försök
after while
loop:
if(försök==5)
{
// display this message: "you failed this mission! do you want to try again?"
score=0;
}
else
{
//display: "congrats you won"
score=(5-försök)*100;
}
Basically your code should be like this:
import javax.swing.*;
import java.util.Random;
class Projekt_1
{
public static void main(String[] args) {
final int maxTries = 5;
JOptionPane.showMessageDialog(null, "Welcome... " + maxTries + " tries ... 500 krones ");
final Random rnd = new Random();
final int hemligtnummer = rnd.nextInt(20) + 1;
int tryCounter = 0;
final int pengar = 500;
String nummer;
int guess = -1;
while (guess != hemligtnummer && tryCounter < maxTries) {
if(tryCounter==0)
nummer = JOptionPane.showInputDialog("...a number 1 and 20");
else
nummer = JOptionPane.showInputDialog("Enter no...");
guess = Integer.parseInt(nummer);
if (guess == hemligtnummer) {
break;
} else if (guess > hemligtnummer) {
JOptionPane.showMessageDialog(null, "Try " + (tryCounter+1) + " was too big try a smaller one");
} else if (guess < hemligtnummer) {
JOptionPane.showMessageDialog(null, "Try " + (tryCounter+1) + " too small try a bigger one");
}
tryCounter++;
}
if(tryCounter==5){
JOptionPane.showMessageDialog(null,"Grattis du vann!" + "\nteh number was " + hemligtnummer + "\nDet tog dig " + tryCounter + " försök");
JOptionPane.showMessageDialog(null, "Your price is :" + (pengar - tryCounter * 100) + " Krones");
}
else
{
JOptionPane.showMessageDialog(null,"congrats you won"+"Your price is :" + (pengar - tryCounter * 100) + " Krones" );
}
}
}
Upvotes: 1
Reputation: 48258
It is really hard to help you if you post all in your native language... but here is my answer...
the issue with your code was in the hole logi, you never use the variable tries(försök) and If the user guess a wrong number then you read the try again but instead you should start the logic of the while again....
anyways....
public static void main(String[] args) {
final int maxTries = 5;
JOptionPane.showMessageDialog(null, "Welcome... " + maxTries + " tries ... 500 krones ");
final Random rnd = new Random();
final int hemligtnummer = rnd.nextInt(20) + 1;
int tryCounter = 0;
final int pengar = 600;
String nummer;
int guess = -1;
while (guess != hemligtnummer && tryCounter < maxTries) {
nummer = JOptionPane.showInputDialog("...a number 1 and 20");
guess = Integer.parseInt(nummer);
tryCounter++;
if (guess == hemligtnummer) {
break;
} else if (guess > hemligtnummer) {
JOptionPane.showMessageDialog(null, "Try " + tryCounter + " was too big try a smaller one");
} else if (guess < hemligtnummer) {
JOptionPane.showMessageDialog(null, "Try " + tryCounter + "too small try a bigger one");
}
}
JOptionPane.showMessageDialog(null,
"Grattis du vann!" + "\nteh number was " + hemligtnummer + "\nDet tog dig " + tryCounter + " försök");
JOptionPane.showMessageDialog(null, "Your price is :" + (pengar - tryCounter * 100) + " Krones");
}
Upvotes: 0
Reputation: 1338
you can have a control break while loop. which will be broken when the number of attempts become more then 5 OR when the correct number is inputted.
See the below for reference
Scanner s = new Scanner(System.in);
int numOfTries = 0;
int input;
int correctNumn = 15;
while (true) {
input = s.nextInt();
if (input != correctNumn && numOfTries <= 5) {
numOfTries++;
if (numOfTries == 5) {
System.out.println("Game Over");
break;
}
continue;
} else if (input == correctNumn) {
System.out.println("corect ans");
break;
}
}
Upvotes: 0