Hirdetés

Keresés

Új hozzászólás Aktív témák

  • Regirck
    senior tag

    Üdv!
    Konzolos menüt szeretnék készíteni, hülyegyerek biztosat, ami nem akad ki, ha nem int-et kap válaszként. Eddig az Angster Erzsébetes extra csomagot használtam, de szeretném nélküle megoldani, scanner-el. Egy ideje gondolkodok rajta, de nem jutok előbbre ,a netes keresés eredményeként a hasNextInt-ig jutottam el, de annak az else ágát nem tudom rendesen használni a megoldásra, mert pl. itt működik:

    public static void main(String[] arg) {
    Scanner in = new Scanner(System.in);
    System.out.println("How many are invading?");

    if (in.hasNextInt()) {
    int a = in.nextInt();
    System.out.println(a);
    } else {
    System.out.println("Sorry, couldn't understand you!");
    }
    }

    de én a saját kódomban nem tudom felhasználni:
    private static Scanner inputScanner = new Scanner(System.in);

    public static void main(String[] args) {
    System.out.println(" *** Welcome to the board game ! *** ");
    selectInMainMenu();
    }

    private static void selectInMainMenu() {
    int mainMenuChoice=0;
    do {
    showMainMenu();
    if (inputScanner.hasNextInt()) {
    mainMenuChoice = inputScanner.nextInt();
    } else {
    System.out.println("Wrong input! Please, select 1, 2 or 3! ");
    break;
    }
    } while (mainMenuChoice < 1 || mainMenuChoice > 3);
    switch (mainMenuChoice) {
    case 1:
    showHelp();
    selectInMainMenu();
    break;
    case 2:
    startAGame();
    selectInMainMenu();
    break;
    case 3:
    System.out.println("\nGoodbye!");
    break;
    }
    }

    private static void showMainMenu() {
    System.out.println("1 - Game info");
    System.out.println("2 - Start a game");
    System.out.println("3 - Exit");
    }

    private static void showHelp() {
    System.out.println("\nShowing help!\n");
    }

    private static void startAGame() {
    System.out.println("\nPlaying a game!\n");
    }

    Szia,

    Exception kezelés kell számodra ha "hülyegyerek" biztosat szeretnél (try-catch).

    Kis segítség!

  • Regirck
    senior tag

    Köszönöm, de így se jó!

    Ezzel a formulával minden esetben ismétlődik, itt pedig úgye csak a döntettlen esetén kellene ismétlés.

    Your dice:
    ---------
    | |
    | * |
    | |
    ---------

    Computer's dice:
    ---------
    | * * |
    | * * |
    | * * |
    ---------

    You lost!

    Vesztettél!
    Your dice:
    ---------
    | * * |
    | * * |
    | * * |
    ---------

    Computer's dice:
    ---------
    | * |
    | |
    | * |
    ---------

    You won!

    Nyertél!
    Your dice:
    ---------
    | * * |
    | * |
    | * * |
    ---------

    Computer's dice:
    ---------
    | * * |
    | * |
    | * * |
    ---------

    Draw!

    Döntettlen!
    Your dice:
    ---------
    | * |
    | * |
    | * |
    ---------

    Computer's dice:
    ---------
    | * |
    | |
    | * |
    ---------

    You won!

    Nyertél!
    Your dice:
    ---------
    | * * |
    | * * |
    | * * |
    ---------

    Computer's dice:
    ---------
    | * * |
    | |
    | * * |
    ---------

    You won!

    Nyertél!
    Your dice:
    ---------
    | * |
    | * |
    | * |
    ---------
    ---------BUILD STOPPED (total time: 2 seconds)

    Köszönöm, jó csak elírtam! :R

  • Regirck
    senior tag

    public static void main(String[] args) {

    boolean draw = true;
    do
    {
    Engine start = new Engine();
    start.Dice();

    System.out.println("");

    if ("You".equals(start.toString())) {
    System.out.println("Nyertél!");
    draw = false;
    } else if ("Computer".equals(start.toString())) {
    System.out.println("Vesztettél!");
    draw = false;
    } else {
    System.out.println("Döntettlen!");
    }
    }
    while (draw);
    }

    Köszönöm, de így se jó!

    Ezzel a formulával minden esetben ismétlődik, itt pedig úgye csak a döntettlen esetén kellene ismétlés.

    Your dice:
    ---------
    | |
    | * |
    | |
    ---------

    Computer's dice:
    ---------
    | * * |
    | * * |
    | * * |
    ---------

    You lost!

    Vesztettél!
    Your dice:
    ---------
    | * * |
    | * * |
    | * * |
    ---------

    Computer's dice:
    ---------
    | * |
    | |
    | * |
    ---------

    You won!

    Nyertél!
    Your dice:
    ---------
    | * * |
    | * |
    | * * |
    ---------

    Computer's dice:
    ---------
    | * * |
    | * |
    | * * |
    ---------

    Draw!

    Döntettlen!
    Your dice:
    ---------
    | * |
    | * |
    | * |
    ---------

    Computer's dice:
    ---------
    | * |
    | |
    | * |
    ---------

    You won!

    Nyertél!
    Your dice:
    ---------
    | * * |
    | * * |
    | * * |
    ---------

    Computer's dice:
    ---------
    | * * |
    | |
    | * * |
    ---------

    You won!

    Nyertél!
    Your dice:
    ---------
    | * |
    | * |
    | * |
    ---------
    ---------BUILD STOPPED (total time: 2 seconds)

  • Regirck
    senior tag

    Sziasztok!

    Kérnék szépen egy kis segítséget!

    Az alábbi program kódba..., tanulom a Jáva programnyelvet de most kicsit elakadtam.

    Azt szeretném elérni, hogy ha döntettlenre jön ki a dobás akkor kezdje előről a a programfutást. Sehogy se tudom "returnolni".

    Tudomnem egy "CleanCode", de az alapok megértésén vagyok.

    package tester;

    public class Tester {

    public static void main(String[] args) {

    Engine start = new Engine();
    start.Dice();

    System.out.println("");

    if ("You".equals(start.toString())) {
    System.out.println("Nyertél!");
    } else if ("Computer".equals(start.toString())) {
    System.out.println("Vesztettél!");
    } else {
    System.out.println("Döntettlen!");
    }

    }
    }

    package tester;

    import java.util.Random;

    public class Engine {

    @Override
    public String toString() {
    if (You > Computer) {
    return "You";
    } else if (Computer > You) {
    return "Computer";
    } else {
    return "Draw";
    }
    }


    int You = random();
    int Computer = random();

    public static void six() {
    System.out.print("---------\n| * * |\n| * * |\n| * * |\n---------");
    }

    public static void five() {
    System.out.print("---------\n| * * |\n| * |\n| * * |\n---------");
    }

    public static void four() {
    System.out.print("---------\n| * * |\n| |\n| * * |\n---------");
    }

    public static void three() {
    System.out.print("---------\n| * |\n| * |\n| * |\n---------");
    }

    public static void two() {
    System.out.print("---------\n| * |\n| |\n| * |\n---------");
    }

    public static void one() {
    System.out.print("---------\n| |\n| * |\n| |\n---------");
    }

    public static int random() {
    Random r = new Random();
    int a = r.nextInt(6) + 1;
    return a;
    }

    public void Dice() {

    System.out.println("Your dice: ");
    switch (You) {
    case 1:
    one();
    break;
    case 2:
    two();
    break;
    case 3:
    three();
    break;
    case 4:
    four();
    break;
    case 5:
    five();
    break;
    case 6:
    six();
    break;
    }
    System.out.println("\n");
    System.out.println("Computer's dice: ");
    switch (Computer) {
    case 1:
    one();
    break;
    case 2:
    two();
    break;
    case 3:
    three();
    break;
    case 4:
    four();
    break;
    case 5:
    five();
    break;
    case 6:
    six();
    break;
    }
    if (You > Computer) {
    System.out.println("\n");
    System.out.println("You won!");
    } else if (Computer > You) {
    System.out.println("\n");
    System.out.println("You lost!");
    } else {
    System.out.println("\n");
    System.out.println("Draw!");
    }
    }
    }

  • Regirck
    senior tag

    Tudtok ajánlani olyan oldalt ahol olyan gyakorló feladatok vannak amivel fel lehet eleveníti a tudásomat? Főleg a szintaktikát szeretném újra begyakorolni, mert most más nyelvben dolgoztam, de pár OOP-s feladat sem árt, amin gondolkozni is kell :) :R

    codingbat.com
    sololearn.com -> telefonos applikacioja is van

  • Regirck
    senior tag

    Nem biztos, hogy jól értem, de esetleg valami ilyesmi?

    private static double keplet(double num) {
    return num/2+4;
    }

    public static void main(String[] args) {
    double num = 16;
    int count = 10;
    for (int i = 0; i < count; i++) {
    num = keplet(num);
    }
    System.out.println("Result: " + num);
    }

    Count értelemszerűen annyi, ahányszor számolni akarsz. Num a kezdeti érték.

    Hálás köszönet! :R

  • Regirck
    senior tag

    Sziasztok!

    Kérnék szépen egy kis segítséget!

    Egy olyan "programot" szeretnék írni ami egy egyenletett lefutatt majd az ott kapott eredményel lefutatná mégegyszer az egyenletett. Próbálkoztam for ciklussal, de sajnos nemjött össze.

    Kezdő érték mondjuk 16(x) lenne és az egyenlet z=(x/2)+4
    Ismétlésnek köszönhetően ezek az értékek jönnének ki:

    16 (ez a kiindulási érték)
    12
    10
    9
    8,5
    8,25

    (Java programnyelvvel egyelőre csak ismerkedek...)

    Programot úgy szeretném megírni, hogy elég legyen a kiindulási értéket átírni, esetleg az egyenletett, majd a ciklusnál az ismétlési számot és utána a program annyiszor futan le azzal az egyenlettel ahányszor szeretném, mindezt úgy, hogy az előzőből számolna tovább.

    Például ha szeretném a W nagy-nagyon sok számadik értéket megkapni akkor ne így kelljen leírnom annyiszor. Persze akkor más szám, más egyenlettel aminek értelme is lenne.

    Előre is köszönöm a segítségeteket!

    public static void main(String[] args) {
    double W0 = 16;
    {
    System.out.println(W0);
    }
    double W1 = (W0 / 2) + 4;
    {
    System.out.println(W1);
    }
    double W2 = (W1 / 2) + 4;
    {
    System.out.println(W2);
    }
    double W3 = (W2 / 2) + 4;
    {
    System.out.println(W3);
    }
    double W4 = (W3 / 2) + 4;
    {
    System.out.println(W4);
    }
    double W5 = (W4 / 2) + 4;
    {
    System.out.println(W5);
    }
    }
    }

Új hozzászólás Aktív témák

Hirdetés