package concrete;
import java.util.Scanner;
public class Concrete {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int a;
int amount = 0;
int number = (int)(Math.random()*100+1); //本质上是[0,1)的数字 还有强制类型的转换。
do //
{
a = in.nextInt();
amount = amount+1;
if (a>number)
{
System.out.println("偏大");
}else if(a<number)
{
System.out.println("偏小");
}
}while(a!=number);
System.out.println("恭喜你猜对了"+"猜了"+amount+"次");
}
import java.util.Scanner;
public class Concrete {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int a;
int amount = 0;
int number = (int)(Math.random()*100+1); //本质上是[0,1)的数字 还有强制类型的转换。
do //
{
a = in.nextInt();
amount = amount+1;
if (a>number)
{
System.out.println("偏大");
}else if(a<number)
{
System.out.println("偏小");
}
}while(a!=number);
System.out.println("恭喜你猜对了"+"猜了"+amount+"次");
}
}
这个程序是随机抽取一个属[1,100]来比较大小,知道相等为止。有效的运用了 while 和if 语句。