![guess.PNG](https://sanemtel.wordpress.com/wp-content/uploads/2018/10/guess1.png)
Merhabalar, bugün bir tahmin oyunu örneğinin kod paylaşımını yapmak istiyorum.
Bir sayının doğru tahmin edilip edilmediğini gösteren bir çıktı yazalım.
Giriş (Input):
Öncelikle girişte tahmin edilecek sayı aralığı MAX olarak tanımlanacak. Oyuncunun her seferinde bir sayı tahmin etmesi istenecek. Eğer sayıyı doğru tahmin ederse 9 lira alacak, yanlış tahmin ederse 1 lira kaybedecek. Her bir tahmin sonunda sahip olduğu TL miktarı güncellenecek. Oyunun sonunda toplamda kaç lirası olduğu hesaplanacak.
I’m thinking of a number between 1 and 10. Guess what it is: 1
Çıktı (Output):
Tahmin edilen sayının doğru olup olmadığı bilgisi, her tahmin sonunda oyuncunun toplamda kaç lira kaldığı, tahmin edilen sayı ve tahmin edilmesi gereken sayı.
You got it! Good guessing! You earn 9 liras! – Total is:9
The number was 1
Kısıtlamalar (Constraints):
1 <= MAX <= 10
1 seferlik kazanç= 9 tl
1 seferli kayıp= 1 tl
Örnek1:
Giriş(Input):
I’m thinking of a number between 1 and 10. Guess what it is:
1
2
6
4
7
8
9
3
9
2
Çıktı (Output):
You got it! Good guessing! You earn 9 liras! – Total is:9
The number was 1
That is not correct, sorry. You lost 1 lira… – Total is:8
The number was 8
That is not correct, sorry. You lost 1 lira… – Total is:7
The number was 7
That is not correct, sorry. You lost 1 lira… – Total is:6
The number was 1
That is not correct, sorry. You lost 1 lira… – Total is:5
The number was 2
That is not correct, sorry. You lost 1 lira… – Total is:4
The number was 7
That is not correct, sorry. You lost 1 lira… – Total is:3
The number was 8
That is not correct, sorry. You lost 1 lira… – Total is:2
The number was 5
That is not correct, sorry. You lost 1 lira… – Total is:1
The number was 1
That is not correct, sorry. You lost 1 lira… – Total is:0
The number was 10
Örnek2:
I’m thinking of a number between 1 and 10. Guess what it is: 1
That is not correct, sorry. You lost 1 lira… – Total is:-1
The number was 5
I’m thinking of a number between 1 and 10. Guess what it is: 2
You got it! Good guessing! You earn 9 liras! – Total is:8
The number was 2
I’m thinking of a number between 1 and 10. Guess what it is: 3
You got it! Good guessing! You earn 9 liras! – Total is:17
The number was 3
I’m thinking of a number between 1 and 10. Guess what it is: 4
That is not correct, sorry. You lost 1 lira… – Total is:16
The number was 5
I’m thinking of a number between 1 and 10. Guess what it is: 5
You got it! Good guessing! You earn 9 liras! – Total is:25
The number was 5
//kod———————————————————————-
import java.util.*;
public class Guessing
{
public static void main (String[] args)
{
final int MAX = 10;
int answer, guess, payment = 0;
Scanner scan = new Scanner (System.in);
Random generator = new Random();
for(int a=0; a<MAX; a++)
{
answer = generator.nextInt(MAX) + 1;
System.out.print ("I'm thinking of a number between 1 and "
+ MAX + ". Guess what it is: ");
guess = scan.nextInt();
if (guess == answer )
{
payment= 9+payment;
System.out.println ("You got it! Good guessing! You earn 9 liras! - Total is:" + payment);
System.out.println ("The number was " + answer);
}
else
{
payment= payment - 1;
System.out.println ("That is not correct, sorry. You lost 1 lira... - Total is:" + payment );
System.out.println ("The number was " + answer);
}
}
}
}
Cevapla