ロバート・ネルソン:
私は、0〜100のセットに必要なコードを持っており、ゼロで正しい答えも持っている可能性もあります。私はMAX(100)/ MIN(0)に私の最終的なint型を設定しています。しかし、0が正しい答えになることはありません。私はJavaで2年以上の経験をコーディングに4年だが、何の問題1-100または1-は、他のMAXは大丈夫と思われます。0が許さ選択ではない場合ヘルプは、または説明してください。
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random generator = new Random();
int MIN = 0;
int MAX = 100;
int answer;
int guess;
String another = "y";
boolean flag = false;
boolean anotherFlag = true;
while(anotherFlag){
answer = generator.nextInt(MAX) + 1;
System.out.println("I'm thinkin of a number between 0 and " + MAX );
System.out.println("Can you guess what it is? \n");
flag = false;
while(!flag) {
guess = scan.nextInt();
if(guess == answer){
System.out.println("You guessed correctly");
flag = true;
} else{
System.out.println("That was wrong, try again.");
}
}
System.out.println("Want to Play again?(y/n)");
another = scan.next();
if(another.equalsIgnoreCase("y") == true){
anotherFlag = true;
} else{
anotherFlag = false;
}
}
ご協力ありがとうございました。
Trivedin:
問題がでているようです
answer = generator.nextInt(MAX) + 1;
あなたの答えの範囲は1 - MAX。
MAX = 100あなたは書込み禁止すべきMAX、 - あなたは0の間で乱数を生成する場合
answer = generator.nextInt(MAX+1);
そのため、(JavaのV8)のJava APIあたりとして
public int nextInt(int bound)
擬似乱数、一様に分布int値0(含む)の間を返し、指定された値(排他的)