如果我要定义只能猜五次,如果没猜中会结束,输出“对不起,你要给蔡洋当老婆”怎么做?
import java.util.Scanner;
class GameTest
{
public static void main(String[] args)
{
int number = (int)(Math.random()*100)+1;
System.out.println("请输入一个整数1-100:");
while(true)
{
Scanner sc = new Scanner(System.in);
int scanf = sc.nextInt();
if(number > scanf)
{
System.out.println("你猜的数小了:");
}
else if(number < scanf)
{
System.out.println("你猜的数大了");
}
else
{
System.out.println("恭喜你,你猜中了,蔡洋送给你做老婆");
break;
}
}
}
}
|
|