0

まず第一に、私はこのすべてに本当に慣れていないと言いたいです...私はできる限り多くのことを学ぼうとしました。 、しかし、始めるにはどこかが必要でした。(ちなみに、このコードのベース部分は CrossCoastGaming の功績によるものです: http://tinyurl.com/kktyq4e )。

では本題に入ります。私は、いくつかの異なるフレーズを追加し、変数を利用し、try カウンターを追加することにより、ビデオの男性が示すコーディングを (より適切な言葉がないため) 改善しました。これが私のコードです:

import java.util.Random;
import java.util.Scanner;

public class Main {

public static int number, guess, tryCount, replay;
public static int maxValue =1;
public static Scanner scan;
public static Random rand;

public static void main(String args[]) {
    scan = new Scanner(System.in);
    rand = new Random();
    System.out.print("Enter a maximum number: ");
    while(maxValue < 2)
        maxValue = scan.nextInt();
    number = rand.nextInt(maxValue);
    System.out.print("Guess a number from 1 to " + maxValue + ": ");
    while (guess != number) {
        guess = scan.nextInt();
        tryCount++;
        if (guess < 1) {
            System.out.print("Guess is not positive. Try again: ");
        }else if (guess < number) {
            System.out.print("Too low! Try again: ");
        }
        if (guess > maxValue) {
            System.out.println("Guess is higher than " + maxValue + ". Try again: ");
        }else if (guess > number) {
            System.out.print("Too high! Try again: ");
        }
    }
    if (tryCount == 1) {
        System.out.println("Nailed it! It only took you 1 try!");
    }
    else {
        System.out.println("Nailed it! It took you " + tryCount + " tries.");
    }
    System.out.println("Type 0 to play again. Type 1 to quit.");
    if (replay == 1) {
        replay = scan.nextInt();

    }
}

}

わかりました、うまくいけば、彼らが何をしているのかを知っている人なら誰でも、私の目標のアイデアを得ることができます. さて、この行でわかるように:

if (replay == 1) {
        replay = scan.nextInt();

    }

ファイルを再起動せずにゲームをリプレイできる方法を書きたいと思います。何をしたいのかはすでにわかっていますが、あらゆる場所を検索しましたが、この時点以降に何を続ければよいかわかりません。私は何かが欠けていると確信しています。どんな助けでも大歓迎です。

4

1 に答える 1

0

これを実現するには、do-while ループを使用できます。

import java.util.Random;
import java.util.Scanner;

public class Main {

    public static int number, guess, tryCount, replay;
    public static int maxValue = 1;
    public static Scanner scan;
    public static Random rand;

    public static void main(String args[]) {
        scan = new Scanner(System.in);
        rand = new Random();
        do { // start of do-while loop
            tryCount = 0; // reset tryCount
            System.out.print("Enter a maximum number: ");
            while(maxValue < 2) {
                maxValue = scan.nextInt();
            }
            number = rand.nextInt(maxValue);
            System.out.print("Guess a number from 1 to " + maxValue + ": ");
            while (guess != number) {
                guess = scan.nextInt();
                tryCount++;
                if (guess < 1) {
                    System.out.print("Guess is not positive. Try again: ");
                } else if (guess < number) {
                    System.out.print("Too low! Try again: ");
                }
                if (guess > maxValue) {
                    System.out.println("Guess is higher than " + maxValue + ". Try again: ");
                } else if (guess > number) {
                    System.out.print("Too high! Try again: ");
                }
            }
            if (tryCount == 1) {
                System.out.println("Nailed it! It only took you 1 try!");
            } else {
                System.out.println("Nailed it! It took you " + tryCount + " tries.");
            }

            do { // check the user's input
                System.out.println("Type 0 to play again. Type 1 to quit.");
                replay = scan.nextInt();
                if (replay != 0 && replay != 1) {
                    System.out.println("Input not recognized.");
                }
            } while (replay != 0 && replay != 1);
        } while (replay == 0); // end of do-while loop
    }
}

do-while ループは、常に少なくとも 1 回実行されます。条件はループの最後でチェックされます。

于 2014-06-16T10:14:41.443 に答える