SAMERグラム:
私は、ユーザーが0から9までの乱数を入力し、番号を推測する必要があり、このコードを持っているが、彼は間違って4回を推測したら、私は4にユーザートライアルを制限するように、システムが停止し、表示されたメッセージは「あなたはあなたの許可を超え裁判」これはこれまでのところ私のコードです:
public static void main(String[] args) {
// TODO Auto-generated method stub
int nb,x;
int count= 0;
Scanner inp= new Scanner(System.in);
nb= (int)(Math.random()*10);
System.out.print("Guess the number between 0 and 9");
x=inp.nextInt();
while(x!= nb) {
if(x>nb) System.out.println("Lesser");
else if (x<nb) System.out.println("Bigger");
System.out.println("Try another Number");
x=inp.nextInt();
count++;
}
System.out.println("Found!!");
System.out.println("Number of Guesses:" +count);
inp.close();
}
シェリ:
また、あなたが使用することができますcount
あなたが破壊してプログラムを終了することができます制限して、ユーザーが終了したら、試みを追跡するために変数while
使用してループをbreak
文を。
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
// TODO Auto-generated method stub
int nb,x;
int count= 1;
Scanner inp= new Scanner(System.in);
nb= (int)(Math.random()*10);
System.out.print(nb);
System.out.print("Guess the number between 0 and 9:");
x=inp.nextInt();
while(x!= nb) {
if (count <= 3){
if(x>nb) System.out.println("Lesser");
else if (x<nb) System.out.println("Bigger");
System.out.println("Try another Number");
x=inp.nextInt();
}
else{
System.out.print("you exceeded your allowed trials");
break;
}
count++;
}
System.out.println("Found!!");
System.out.println("Number of Guesses:" +count);
inp.close();
}
}