Java程序运行如下:
public class Test4{
public static void main(String[] args){
System.out.println("输出互换前手中的纸牌:");
String left="10";
String right="8";
String exchange;
System.out.println("左手中的纸牌:"+left);
System.out.println("右手中的纸牌:"+right);
System.out.println("输出互换后手中的纸牌:");
exchange=left;
left=right;
right=exchange;
System.out.println("左手中的纸牌:"+left);
System.out.println("右手中的纸牌:"+right);
}
}
java程序运行结果如下:
import java.util.Scanner;
public class Test5{
public static void main(String[] args){
int shirtprice=245;
int shoeprice=570;
int padprice=320;
int shirtNo=2;
int shoeNo=1;
int padNo=1;
System.out.println("********消费单*************");
System.out.println("商品\t单价\t个数\t金额");
System.out.println("T恤\t¥"+shirtprice+"\t"+shirtNo+"\t"+shirtprice*shirtNo);
System.out.println("网球鞋\t¥"+ shoeprice+"\t"+shoeNo+"\t"+shoeprice*shoeNo);
System.out.println("网球拍\t¥"+ padprice+"\t"+padNo+"\t"+padprice*padNo);
System.out.println("折扣:0.8");
int finalpay=shirtprice*shirtNo+shoeprice*shoeNo+ padprice*padNo;
System.out.println("消费总金额:"+finalpay);
System.out.println("实际交费:¥1500");
System.out.println("找钱:"+(1500-finalpay*0.8));
System.out.println("====本次购物所获积分(消费100元得3分):"+finalpay/100*3);
}
}
3. 从键盘上输入矩形的长和宽,计算并输出其周长和面积。
java程序运行结果如下:
import java.util.Scanner;
public class JuXing{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("输入矩形的长和宽,计算其周长和面积");
int length=scanner.nextInt();
int wide=scanner.nextInt();
System.out.println("周长="+(length+wide)*2);
System.out.println("面积="+(length*wide));
}
}
4.从键盘上输入一个天数,计算并输出其包含几周零几天。
import java.util.Scanner;
public class TianShu{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("从键盘上输入一个天数,计算并输出其包含几周零几天");
int a=scanner.nextInt();
int week=a/7;
int day=a%7;
System.out.println("周="+week);
System.out.println("天数="+day);
System.out.println("输入一个天数:"+a);
}
}
5.拆分一个四位整数。
java程序运行结果如下:
import java.util.Scanner;
public class Test3{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//计算百位数
int num=scanner.nextInt();
int bai=num/100;
int ge=num%10;
int shi=num%100/10;
System.out.println(num+"的百位数字是:"+bai);
System.out.println(num+"的十位数字是:"+shi);
System.out.println(num+"的个位数字是:"+ge);
//计算四位数
int num1=scanner.nextInt();
int qia1=num1/1000;
int bai1=num1%1000/100;
int shi1=num1%100/10;
int ge1=num1%10;
System.out.println(num1+"的千位数字是:"+qia1);
System.out.println(num1+"的百位数字是:"+bai1);
System.out.println(num1+"的十位数字是:"+shi1);
System.out.println(num1+"的个位数字是:"+ge1);
}
}