1、猜数字
* 1)随机数 2)猜价格(键盘输入)
1.猜:3次机会 机会用完/猜对 是否继续下一轮游戏
public static void guessNum(){
Scanner scanner = new Scanner(System.in);
Random random =new Random();
int Price,guesNum,i=0;
boolean judge = true;
while(judge == true){
Price = random.nextInt(5)+1;
while (i < 3) {
guesNum = scanner.nextInt();
if (Price ==guesNum) {
System.out.println("you are right,true num is "+Price);
break;
}
else {
System.out.println("you are wrong");
i++;
}
}
System.out.println("do you want to go on? true:yes false:not");
judge = scanner.nextBoolean();
i = 0;
}
}
2 是否素数
判断输入的数是否是素数
public static boolean isSuShu(int num){
int j = (int)Math.sqrt(num);
for (int i =2;i<=j;i++){
if (num%i ==0&&num !=2 )
return false;
}
return true;
}
3 输出100内素数
public static void outPut100suShu(){
for (int i=0;i<=100;i++){
if (isSuShu(i)==true){
System.out.println(i);
}
}
}
4 求和 1/1 + (-1/2) + (1/3) + (-1/4)+......(1/n)
public static double cac1c(){
double res = 1;
for (int i=2;i<=100;i++){
if (i%2==0)
res += Math.pow(i,-1);
else
res -=Math.pow(i,-1);
}
return res;
}
5 判断是否为闰年
public static boolean isRunNian(int year){
if ((year%4==0 && year%100!=0)|| year%400==0){
return true;
}
else
return false;
}
6 switch语句,数组简单应用
//int id;
//请输入:1.登录功能 2.注册功能3.退出
//登录:
// id 登录成功:当前数组中存在id
//失败
//注册;
//id 1)账号存在 注册失败
// 2)不存在 登录成功
public static void register(){
boolean judge =true;
Scanner scanner = new Scanner(System.in);
int user[]= new int[9];
int size =0;
while(judge==true){
System.out.println("1.登录功能 2.注册功能3.退出4、查看");
int choice = scanner.nextInt();
switch (choice){
case 1:
{
System.out.println("登录:输入id");
int id = scanner.nextInt();
for (int i =0;i<user.length;i++){
if (id==user[i]){
System.out.println("登陆成功");
break;
}else if(id!=user[i] && i==user.length-1){
System.out.println("登录失败");
}
}
break;
}
case 2:
{
System.out.println("注册:输入id");
size ++;
Boolean isExist = false;
int id = scanner.nextInt();
for (int val :user) {
if (val == id) {
isExist = true;
break;
}
}
if (isExist==false) {
user[size-1] = id;
System.out.println("注册ok");
}else {
System.out.println("注册失败");
size--;
}
break;
}
case 3:{
System.out.println("退出");
judge = false;
break;
}
case 4:{
System.out.println("现在有如下用户");
System.out.println(Arrays.toString(user));
break;
}
}
}
}
此处,可以封装成方法 ,把菜单写成单独逻辑