猜数字游戏:
随机生成数字n(1-100), 等待用户输入猜测数据, 根据用户的输入比较输出猜大了,猜小了,猜对了, 如果用户猜对了就结束游戏 。
public class ForGuessTest {
public static void main(String[] args) {
//1. 随机生成1~100的数字
Random ra=new Random();
int temp=ra.nextInt(100)+1;
//5. 声明一个变量统计猜测的次数
int count=0;
//4. 使用for死循环不限制猜测次数
for(;;) {
//2. 等待用户输入猜测的数据
System.out.println("请输入猜测的数据:");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
count++;
//3. 进行比较并给出相应的提示
if(tempnum) {
System.out.println("猜小了");
}else {
System.out.println("猜对了");
break;
}
}
//6.参照猜测的次数评级
if(count<3){
System.out.println("牛");
}else if(count<=6) {
System.out.println("还行");
}else{
System.out.println("多加练习");
}
}
}
使用双重for循环打印2~100之间的所有素数。
• 当一个数只能被1和它本身整除时,这个数就叫做素数或质数。
public class PrintTest {
public static void main(String[] args) {
//1. 打印所有2~100之间的整数
for(int i=2;i<=100;i++){
//3. 声明一个boolean类型的数据来标记该数是否是一个素数
boolean flag=ture;
//2. 判断i是否是一个素数
//判断该数是否是一个素数的方法是:素数不能被2到它本身减1所有的数整除
//内层for来判断该数是否是一个素数
for(j=2;j


