以下需求:
判断一个数是奇数还是偶数;
猜数字游戏(1-100),输入值不在范围内报错,并不计入总次数;
函数部分利用,判断两值是否相等,输入长宽值画矩形;
package com.imooc;
import java.util.Scanner;
import java.util.Random;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
int i=78;
char A='B'+1;
System.out.println(A);//C
System.out.println("i是"+(i%2==0?"偶数":"奇数"));
if(i%2==0)
System.out.println("i是偶数");
else
System.out.println("i是奇数");
int d=6;
System.out.println("计算十天之后是星期"+((d+3)%7));
int x=1;
int y=2;
int a=3;
int b=4;
int c=5;
int ab=(3+4*x)/5+(a+b+c)*(y-5)*10/x+(4/x+(9+x)/y)*9;
System.out.println(ab);
*/
int a1=(int) (Math.random()*100+1);
int count=0;
int a2=0;
while(a1!=a2){
System.out.println("猜数字游戏开始,请输入一个1-100的数字");
Scanner sc=new Scanner(System.in);
a2=sc.nextInt();
if(a2<1||a2>100){
System.out.println("输入数字无效");
continue;
}
if(a1==a2)
System.out.println("猜对啦");
else if(a1>a2)
System.out.println("猜小了");
else System.out.println("猜大了");
count++;
}
System.out.println("猜数字游戏次数"+count);
//函数画矩形
System.out.println("请输入长度值");
Scanner one=new Scanner(System.in);
System.out.println("请输入宽度值");
Scanner two=new Scanner(System.in);
//System.out.println("请输入宽度值");
int width=(int)one.nextInt();
int height=(int)two.nextInt();
draw(width,height);
System.out.println(equla(4,4));
}
public static void draw(int width ,int weight){
for (int i=1;i<=width;i++){
for(int j=1;j<=weight;j++){
System.out.print("*");
}
System.out.println("");
}
}
public static boolean equla(int a1,int b1){
return((a1==b1)?true:false);
}
}