今日课后习题与解答
1.提示用户输入籍贯,当用户输入籍贯后,向用户显示"欢迎您来到某某" ,某某是用户输入的籍贯地。
package exercise;
public class Exe04 {
public static void main(String[] args) {
int a = 22;
int b = 33;
int c = 44;
int d = 55;
int t;
t = a;
a = b;
b = t;
t = c;
c = d;
d = t;
System.out.println("a = " + a +" , b = " + b + " , c = " + c + " , d = " + d);
}
}
package exercise;
import java.util.Scanner;
public class Exe01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入籍贯:");
String nativePlace = scanner.nextLine();
System.out.printf("欢迎来到%s", nativePlace);
}
}
2.定义整形的变量 a=1,字符串类型lang="java"
package exercise;
public class Exe02 {
public static void main(String[] args) {
int a = 1;
String lang = "java";
}
}
3.定义矩形的边长a=20,b=30,计算并打印矩形的周长l和面积s,并打印结果
package exercise;
public class Exe03 {
public static void main(String[] args) {
int a = 22;
int b = 30;
int l = 2 * a + 2 * b;
int s = a * b;
System.out.println("矩形的周长为:" + l);
System.out.println("矩形的面积为:" + s);
}
}
4.定义四个变量,a=22; b=33,c=44,d=55;现在编程实现a与d变量中的值交换,b与c中的值交换。最后输出a、b、c、d中的值
package exercise;
public class Exe04 {
public static void main(String[] args) {
int a = 22;
int b = 33;
int c = 44;
int d = 55;
int t;
t = a;
a = b;
b = t;
t = c;
c = d;
d = t;
System.out.println("a = " + a +" , b = " + b + " , c = " + c + " , d = " + d);
}
}
5.通过键盘输入姓名、性别、年龄,身高、体重,定义变量保存接受的值
package exercise;
import java.util.Scanner;
public class Exe05 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = sc.next();
System.out.print("请输入性别:");
String sex = sc.next();
System.out.print("请输入身高:");
float height = sc.nextFloat();
System.out.print("请输入体重:");
float weight = sc.nextFloat();
}
}
6.计算 1234 的二进制、八进制、十六进制 表示法,并用程序以不同的方式进行定义
package exercise;
public class Exe06 {
public static void main(String[] args) {
int x = 1234;
System.out.println("1234的二进制为:" + Integer.toBinaryString(x));
System.out.println("1234的八进制为:" + Integer.toOctalString(x));
System.out.println("1234的十六进制为:" + Integer.toHexString(x));
}
}
7.计算 0xfefe 对应的 十进制、二进制
package exercise;
public class Exe07 {
public static void main(String[] args) {
int x = 0xfefe;
System.out.println("0xfefe的十进制为:" + x);
System.out.println("0xfefe的二进制为:" + Integer.toBinaryString(x));
}
}
8.小明买了一双鞋,价值58元,买了3件衣服,每件30元,买了5个包,每个包55.8元。小明共交了500元,问还要找回多少元。
package exercise;
public class Exe08 {
public static void main(String[] args) {
float shoesPrice = 58;
float clothePrice = 30;
float packagePrice = 55.8f;
float sumPrice = 500;
float findPrice = sumPrice - 5 * packagePrice - 3 * clothePrice - shoesPrice;
System.out.println("找回" + findPrice + "元");
}
}
9.输入一个天数,分别计算相当于多少小时,多少分钟,多少秒
package exercise;
import java.util.Scanner;
public class Exe09 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入天数:");
int dayNum = sc.nextInt();
int hours = 24 * dayNum;
int minutes = hours * 60;
long seconds = minutes * 60;
System.out.printf("%d相当于%d小时,相当于%d分钟,相当于%d秒", dayNum, hours, minutes, seconds);
}
}