java程式碼實現猜數字遊戲
阿新 • • 發佈:2018-12-02
public class GuessingGame {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = (int)(Math.random()*1000+1); //藏起來的數
System.out.println(num); //作弊
int guess;
do{
System.out.println("猜吧!");
guess = scan.nextInt(); //1/3
if(guess==0){
break;
}else if(guess>num){
System.out.println("太大了");
}else if(guess<num){
System.out.println("太小了");
}
}while(guess!=num); //2.
if(guess==num){
System.out.println("恭喜你,猜對了!");
}else{
System.out.println("下次再來吧!");
}
/*
Scanner scan = new Scanner(System.in);
int num = (int)(Math.random()*1000+1); //藏起來的數
System.out.println(num); //作弊
System.out.println("猜吧!");
int guess = scan.nextInt(); //1.
while(guess!=num){ //2.
if(guess==0){
break;
}else if(guess>num){
System.out.println("太大了");
}else{
System.out.println("太小了");
}
System.out.println("猜吧!");
guess = scan.nextInt(); //3.
}
if(guess==num){
System.out.println("恭喜你,猜對了!");
}else{
System.out.println("下次再來吧!");
}
*/
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = (int)(Math.random()*1000+1); //藏起來的數
System.out.println(num); //作弊
int guess;
do{
System.out.println("猜吧!");
guess = scan.nextInt(); //1/3
if(guess==0){
break;
}else if(guess>num){
}else if(guess<num){
System.out.println("太小了");
}
}while(guess!=num); //2.
if(guess==num){
System.out.println("恭喜你,猜對了!");
}else{
System.out.println("下次再來吧!");
}
/*
Scanner scan = new Scanner(System.in);
int num = (int)(Math.random()*1000+1); //藏起來的數
System.out.println("猜吧!");
int guess = scan.nextInt(); //1.
while(guess!=num){ //2.
if(guess==0){
break;
}else if(guess>num){
System.out.println("太大了");
}else{
System.out.println("太小了");
}
System.out.println("猜吧!");
guess = scan.nextInt(); //3.
if(guess==num){
System.out.println("恭喜你,猜對了!");
}else{
System.out.println("下次再來吧!");
}
*/
}
}