作业:
public class HomeWork {
public static void main(String[] args)
{
HomeWork.absolutlyMethod();
HomeWork.calculateMethod();
HomeWork.changeMethod();
HomeWork.chargeMethod();
}
/**
* 计算分数
*/
public static void calculateMethod()
{
int score = 0; //总分数
int num1 = (int)(Math.random()*90+10);
int num2 = (int)(Math.random()*90+10);
System.out.println("题目是:"+num1+"+"+num2+",请输入答案");
Scanner sc = new Scanner(System.in);
int result = sc.nextInt();
score +=result == (num1+num2)?10 :-10;
/*
if(result == (num1+num2))
{
score+=10;
}
else
{
score-=10;
}
*/
System.out.println("得分是:"+score);
}
/**
* 判断绝对值
*/
public static void absolutlyMethod()
{
Scanner sc = new Scanner(System.in);
int value= sc.nextInt();
if(value>=0)
{
System.out.println(value);
}
else
{
System.out.println((value)*(-1));
}
}
/**
* 判断闰年平年
*/
public static void yearMethod()
{
Scanner sc = new Scanner(System.in);
int year= sc.nextInt();
if(year<=0)
{
System.out.println("不能回到公元前");
}
else if((year%4==0 && year%100!=0) || year%400 == 0)
{
System.out.println("是闰年");
}
else
{
System.out.println("是平年");
}
}
/**
* 判断正负数
*/
public static void chargeMethod()
{
int num = (int)(Math.random()*21-10);
String str = num>=0?"正数":"负数";
System.out.println(str);
}
/**
* 三种交换方法
*/
public static void changeMethod()
{
int x =20;
int y =30;
/*
int i = x+y;
x = i - x;
y = i - y;
int tmp = x;
x = y;
y = tmp;
x = x ^ y;
y = x ^ y;
x = x ^ y;
*/
}
}
Test01:
public class Test01 {
public static void main(String[] args)
{
int money = 99;
switch(money)
{
case 1:
System.out.println("吃油条");
break;
case 5:
System.out.println("吃煎饼");
break;
case 10:
System.out.println("套餐");
break;
case 10000:
System.out.println("土豪");
break;
default:
System.out.println("没钱");
break;
}
}
}
Test02:
public class Test02 {
public static void main(String[] args)
{
/*
for( int i=4 ;i<5 ; ++i)
{
//i++ 先使用 后加1
System.out.println(i);
System.out.println("奥运会");
}
*/
/*
int score = 0;
for(int i = 0; i < 3; i++)
{
Scanner sc = new Scanner(System.in);
int num1 = (int)(Math.random()*90+10);
int num2 = (int)(Math.random()*90+10);
System.out.println("题目是:"+num1+"+"+num2+",请输入答案:");
int result = sc.nextInt();
if(result == (num1+num2))
{
score+=10;
}
else
{
score-=10;
}
System.out.println("成绩是:"+score);
}
*/
Test02.calcMethod();
}
/**
* 1+2+3+4+....99
*/
public static void calcMethod()
{
/*
int sum = 0;
for(int i=1;i<100 ; i++)
{
sum+=i;
}
System.out.println(sum);
*/
int sum = 0;
int i = 0;
int j = 0;
for(;i < 7; i++)
{
j++;
if(i == 5)
{
sum+=50;
break; //结束本次循环体 执行下次循环
}
else
{
sum+=100;
}
System.out.println(i);
}
//System.out.println(sum);
//System.out.println(j);
}
}
Test03:
public class Test03 {
public static void main(String[] args)
{
//break
//continue
for(int i = 0; i< 3; i++)
{
for(int j = 0; j< 3; j++)
{
System.out.println("j="+j);
continue;
}
System.out.println("i="+i);
}
}
}
Test04:
public class Test04 {
public static void main(String[] args)
{
// int i = 0;
// while(i<10)
// {
// i++;
// System.out.println(i);
// break;
// }
// calcTableMethod();
// someMethod();
printMethod();
/**
* 假设1+2+3+4+......n>3000
* 求n的最小值
*/
/**
* 输出2000到2500间所有的闰年
*/
}
/**
* 打印九九乘法口诀表
* 1*1=1
* 2*1=2 2*2=4
* 3*1=3 3*2= 6 3*3=9
* ...........
*
*/
public static void calcTableMethod()
{
for(int i = 1; i<= 9 ;i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(i+"*"+j+"="+(i*j)+" ");
}
System.out.println();
}
}
/**
* 求3到20中所有的质数
* 质数:只能被1和自己整除的数
*/
public static void someMethod()
{
for(int i=3; i<=20; i++)
{
for(int j = 2; j<=i ; j++)
{
if(i%j==0 && i!=j)
{
break;
}
if(i%j==0 && i==j)
{
System.out.println(i);
}
}
}
}
/**
* 键盘输入矩形的宽和高
* 打印出对应的矩形 用“*”表示
* 例如
* *****
* *****
* *****
* *****
*/
public static void printMethod()
{
Scanner f = new Scanner(System.in);
System.out.println("请输入矩形的宽:");
Scanner gf = new Scanner(System.in);
System.out.println("请输入矩形的高:");
int k = f.nextInt(); //矩形的宽
int g = gf.nextInt(); //矩形的高
for(int i=0;i<k; i++)
{
for(int j = 0 ; j < g; j++)
{
System.out.print(" *");
}
System.out.println();
}
}
}
Test05:
public class Test05 {
public static void main(String[] args) {
for(int i = 1; i<= 9 ;i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(i+""+j+"="+(ij)+" ");
}
System.out.println();
}
}
}