henuz java ya yeni adim attim.sinifta bu hafta yaptigimiz calismada bazi ufak sorunlar var.artik ufakmidir buyukmudur orasi tartisilir:}
programin ozellikleri asagidakidadir.
1) sayi tahmin etme programi
2) kullanici maximum bir sayi sececek ve program 1 ile kullanicinin sectigi maximum sayi arasindan sayi tutacak.
3) daha sonra kullanici aklindaki sayiyi girecek.eger sayi programin olusturdugu sayidan buyukse "too high", kucukse "too low" tarzinda kullaniciyi bilgilendirecek.
4) kullanici random sayiyi bulduktan sonra.program kullanicinin sayiyi bulmak icin kac defa tahmin ettigini gosterecek.
5) Ve son olarak program kullaniciya soru soracak.tekrar oynamak istermisiniz?
kullanici 'y' basarsa tekrardan program baslayacak.'n'basarsa program kapanacak.
not: 4. ve 5.siklarda sorun yasiyorum. kendi kodlarimi paylasiyorum.suan icin 2 class var.
metotlar icin olan sinif asagidadir.
/**
* This is the class description that has method names printguestion and testcondition
* @author Diyar Polat
*
*/
import java.util.Random;
import java.util.Scanner;
class Game {
private int randomNumber; // random number that will be created
private int guessedNumber; // number that we guess
private int maxNumber; // maximum number sets 1 to max
/* Prints question*/
public void printQuestion()
{
System.out.println("We are going to play a number guessing game.");
System.out.println("You are going to guess a number between 1 and what number?");
}
/* Test the condition if the user enters to high or too low */
public void testCondition()
{
Scanner input = new Scanner(System.in);
maxNumber = input.nextInt(); // number that you guessed
System.out.println("Type a number between 1 and " + maxNumber);
Random r = new Random(); //Creates random object
randomNumber = r.nextInt(maxNumber);
while(guessedNumber != randomNumber){
guessedNumber = input.nextInt();
if (guessedNumber < randomNumber)
System.out.println("Too low. Try again: ");
if (guessedNumber > randomNumber)
System.out.println("Too high. Try again:");
if (guessedNumber == randomNumber)
System.out.println("Right: You got it");
}
}
}metotlari test eden sinif asagidadir./**
* This program creates random number and let asks user to guess a number
* @author Diyar Polat
*
*/
import java.util.*;
public class GameTest {
public static void main(String[] args) {
String playAgain; // variable name
Scanner input = new Scanner(System.in); // Gets the input from user
Game myGame = new Game(); //Creates mygame object
myGame.printQuestion(); //calls printquestion method
myGame.testCondition(); // calls testCondition method
System.out.println("Would you like to play again?");
/* The loop body */
do {
playAgain = input.next();
Game myotherGame = new Game();
myotherGame.printQuestion();
myotherGame.testCondition();
System.out.println("Would you like to play again?");
playAgain = input.next();
} while (playAgain == "y" || playAgain == "Y");
}
}
/*
We are going to play a number guessing game.
You are going to guess a number between 1 and what number?
70
Type a number between 1 and 70
56
Too low. Try again:
65
Too high. Try again:
60
Too low. Try again:
63
Too high. Try again:
62
Too high. Try again:
61
Right: You got it
Would you like to play again?
y
We are going to play a number guessing game.
You are going to guess a number between 1 and what number?
*/Programdaki sorunlara gelince tekrar oynamak istermisiniz?(would you like to play again?) sorusunda kullanici 'y' girerse tekrardan basliyor program fakat bir daha ayni islemi yapmiyor.yani..
Alıntı