Javada boshlang'ich raqamni aniqlash o'yini


Download 18.12 Kb.
Sana22.04.2020
Hajmi18.12 Kb.
#100740
Bog'liq
Java lab O'riboyev


Muxammad Al-Xorazmiy Nomidagi Toshkent Axborot Texnologiyalari Universiteti

220-16 guruh talabasi O’riboyev Javohirning «Java tilida dasturlash» fanidan amaliy ishlari.


Javada boshlang'ich raqamni aniqlash o'yini
Men 4-kurs talabasi bo'ldim. Hozirda biz Java dasturini o'rganmoqdamiz va so'nggi topshiriq bu o'yinni yaratishga to'g'ri keldi. Men kelajakda nima qilmasligimni bilish uchun va shuning uchun boshidan yomon odatlardan qochishim uchun kod tarzi va boshqalar haqida ba'zi fikrlarni olish umididaman. Bularning barchasini bilmayman, shuning uchun hech qanday tushuntirishlar maslahatlar umuman ajoyib bo'ladi!

import java.util.Random;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

boolean win = false;

boolean running = true;

System.out.println("Please enter the upper limit: ");

Game game = new Game(scan.nextInt());

while (running) {

System.out.println("Please enter a guess ");

while (!win) {

if (game.guess(scan.nextInt())) {

win = true;

}

}

System.out.println("Would you like to play again y/n?");

String playAgain = scan.next();

if (playAgain.equalsIgnoreCase("y")) {

game.reset();

running = true;

win = false;

} else {

running = false;

System.out.println("Game over");

}

}

}

}

O'yin sinf:



import java.util.Random;

public class Game {

private int randomNumber;

private int tries = 0;

private int range;

public Game(int range) {

this.range = range;

generateRandomNumber();

}

private void generateRandomNumber() {

Random rand = new Random();

this.randomNumber = rand.nextInt(range) + 1;

}

public boolean guess(int guess) {

tries++;

if (guess == randomNumber) {

if (tries > 1) {

System.out.println("You got the number in " + tries + " tries.");

return true;

} else {

System.out.println("You got the number in " + tries + " try.");

return true;

}

} else if (guess > randomNumber) {

System.out.println("Too high try again");

return false;

} else if (guess < randomNumber) {

System.out.println("Too low try again ");

return false;

}

return false;

}

public void reset() {

this.tries = 0;

generateRandomNumber();

}

}
}}

}
Download 18.12 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling