包括几个作业:计算恩格尔系数,计算方形边长,将一个正整数倒序输出
(话说代码片有点麻烦……)
package work.w0220;
// package work.w0220;
//import java.util.Scanner;
//import java.sql.*;
public class caculateid {
/**
* @param args
* 算数运算符 + - * %(取模) /
* 关系运算符== >= <= !=
* 逻辑运算符
* & | ^ (按位操作,按位与或和亦或,long型不可用)
* ! (取反,仅用于二进制)
* && || (逻辑与或,两边都是boolean)
*
* >><
<二进制移位操作 * 左移或者右移,右移一位相当于原值除以2 有各种码的问题所以用处不大 条件运算符?: 表达式?(true):(false); string sth="(19">
12)?"yes":"no";
*
* 赋值运算符= *= /= += -=
*
* 自增自减++ --
*
* a=30,b=0
* b=a++
* 在右得到b=30,a=31,先取出a,运算完后+1,与括号无关
* 在左得到b=31,a=31先算后加
* a---b同 a-- -b
*
*
* i=i++,所得不变
* 但是i++所得正常
*
* 整数前加0表示8进制,0x表示16进制
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
{
int i1=40,i2=25;
int ic;
ic=i1%i2;
System.out.println("ic="+ic);
}
/**
* 取一个多位数的数的个位数
* */
{
int i1=6,i2=10;
int ic;
ic=i1%i2;
System.out.println("ic="+ic);
}
{
int i1=563,i2=600,i3=200;
boolean b1;
b1=(i1>i2)&&(i1<=i2);
System.out.println("b1="+b1);
b1=((i3-i1)>100)||((i2%i3)==0);
System.out.println("b1="+b1);
}
{
int year=2100;
for(int i=0;i<12;i++)
{
double year4=0,year100=0,year400=0;
year4=year/4.0;
year100=year/100.0;
year400=year/400.0;
boolean b1=(year4==(int)year4);
boolean b2=(year100!=(int)year100);
boolean b3=(year400==(int)year400);
if((b1&&b3)||(b2&&b1))
System.out.println(year400+"闰年"+year+" b1="+b1+" b2="+b2+" b3="+b3);
year-=21;
}
}
{
// 按位运算
//较短的数在前方补齐
int id=070;
int ie=0xff;
int iff;
iff=id&ie;
System.out.println(iff);//&为按位与,转为二进制后判定,有0出0
iff=ie|id;
System.out.println(iff);//|为按位或,转为二进制后判定,有1出1
iff=id^ie;
System.out.println(iff);//^为按位亦或,转为二进制后判定,相同出0,相异出1
//亦或的正确用法之:免变量交换数据
int a=15,b=18;
a=a^b;
b=a^b;
a=a^b;
System.out.println(a+" "+b);
}
{
int ia=-105;
System.out.println(ia++);
System.out.println(ia);
}
//条件运算
/*
{
//男女
Scanner scan=new Scanner(System.in);
boolean sex=true;
System.out.println("输入性别,男为1,0为女");
int i=(int)scan.nextDouble();
if(i==1)sex=true;
else sex=false;
String sexjud=sex?"男":"女";
System.out.println(sexjud);
}*/
//自运算
{
int a;
a=256;
a%=5;
System.out.println(a);
}
//test
{
// Scanner scan=new Scanner(System.in);
int aaa=1,bbb=2,ccc=3;
// float f1,f2,f3;
/*
System.out.println("input aaa");
aaa=scan.nextInt();
System.out.println("input bbb");
bbb=scan.nextInt();
System.out.println("input ccc");
ccc=scan.nextInt();*/
ccc=(aaa++>2)?(aaa+++--bbb):(aaa-ccc);
System.out.println("aaa:"+aaa);
System.out.println("bbb:"+bbb);
System.out.println("ccc:"+ccc);
}
}
}
package work.w0219;
// package work.w0219;
public class dataA {
/**
* @param args
*/
public static void main(String[] args) {
{
// TODO Auto-generated method stub
byte b=10;
short s=20;
int i=30;
long l=40;
char c=65;
i=b+s+c;
l=b+s+c+i+l;
double d=4.2;
float f=5f;
d=d+i+b+s+l+c+f;
}
/**对表达式,最终结果以最大兼容的输出为主*/
{
boolean i=true,j=false;
boolean or=i|j,and=i&&j;
boolean not=!j;
System.out.println(or);
System.out.println(and);
System.out.println(not);
}
{
int a=-12;
int b=5;
int c=a%b;
System.out.println(c);
}
}
}
package work.w0219;
// package work.w0219;
import java.io.*;
public class homework0218 {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
String name="申杰雄";
boolean sex=true;
byte age=20;
byte weight=105;
int recin=0,foodout=0;
double t=0,engel=0;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("姓名:"+" "+name);
if(sex)
System.out.println("性别:"+" "+"男");
else
System.out.println("性别:"+" "+"女");
System.out.println("年龄:"+" "+age);
System.out.println("体重:"+" "+weight+" "+"kg");
for(;recin<=0;)
{
System.out.println("输入收入:");
t=Double.parseDouble(in.readLine());
recin=(int)t;
if(recin<=0)System.out.println("输入有误,收入不得等于0");
}
for(;foodout>recin||foodout<=0;)
{
System.out.println("输入食品开销:");
t=Double.parseDouble(in.readLine());
foodout=(int)t;
if(foodout>recin||foodout<=0)System.out.println("输入有误,食品开销不得大于收入或小于等于0");
}
{
float cac;
int err;
cac=(float)foodout/recin;
err=(int)(cac*10000);
engel=(float)err/10000;
}
System.out.printf("恩格尔系数:"+" "+"%.4f\r\n",engel);
}
}
package work.w0219;
// package work.w0219;
import java.util.Scanner;
public class homework2019 {
@SuppressWarnings("resource")
public static void main(String[] args)
{
double l=0.0,w=0.0;
double s,lon;
System.out.println("输入方形的长宽,中间用回车隔开");
Scanner scan=new Scanner(System.in);
l=scan.nextDouble();
w=scan.nextDouble();
lon=2*(l+w);
s=l*w;
if (l==w)
{
System.out.print("正方形周长为"+lon);
System.out.print("正方形面积为"+s);
}
else
{
System.out.print("长方形周长为"+lon);
System.out.print("长方形面积为"+s);
}
}
}
package work.w0219;
// package work.w0219;
import java.util.Scanner;
//import java.math.*;
public class homework2019B {
/**
* @param args
*/
@SuppressWarnings("resource")
public static void main(String[] args) {
// TODO Auto-generated method stub
int num,tem,lon;
int sph[];
int tem2;
int ecc=0;
num=91599;
System.out.println("输入数字");
Scanner scan=new Scanner(System.in);
num=scan.nextInt();
tem=num;
lon=0;
for(;tem!=0;)
{
tem=tem/10;
lon++;
}
// System.out.println(lon);
sph=new int[lon+1];
tem=num;
tem2=num;
for (int i=lon;i>0;i--)
{
tem=tem2/(int)Math.pow(10, i-1);
tem2=tem2-tem*(int)Math.pow(10, i-1);
// System.out.print(i+"\t");
sph[i]=tem;
// System.out.print(sph[i]+"\n\r");
}
for(int i=0;i<=lon/2;i++)
{
if(sph[i+1]!=sph[lon-i]) ecc++;
// System.out.println(sph[i+1]+"vs"+sph[lon-i]);
}
// System.out.println(ecc);
boolean err=(ecc==0);
System.out.println(err);
}
}
package work.w0219;
// package work.w0219;
import java.util.Scanner;
public class io {
/**
* @param args
*/
@SuppressWarnings("resource")
public static void main(String[] args) {
// TODO Auto-generated method stub
int a;
System.out.println("输入");
Scanner scan=new Scanner(System.in);
a=scan.nextInt();
System.out.println(a);
a=2;
a=a/10;
System.out.println(a);
}
}