System.out.println(5/2)以及System.out.println(5.0/2)

本文探讨了Java中整数除法导致的小数部分丢失问题,通过示例`System.out.println(5/2)`和`System.out.println(5.0/2)`说明了浮点数除法的必要性,以确保得到正确的2.5结果。了解这个细节对于避免编程中的精度错误至关重要。
System.out.println(5/2);

但是程序输出结果是:2

因为两个整数相除,就会是整数,小数点后面不会保留

在随便哪个整数加个小数点就解决了

System.out.println(5.0/2);

输出结果肯定是:2.5

这个对不对JAVA编写代码package gjj; import java.util.Scanner; public class eryc { // 手机类 static class MobilePhone { private String brand; // 品牌型号 private double size; // 尺寸 private double price; // 价格 private String configuration; // 配置 private int stock; // 库存 private double totalPrice; // 总价 // 构造函数 public MobilePhone(String brand, double size, double price, String configuration) { this.brand = brand; this.size = size; this.price = price; this.configuration = configuration; this.stock = 0; this.totalPrice = 0; } // 入库方法 public void addStock(int quantity) { this.stock += quantity; this.totalPrice = this.price * this.stock; } // getter方法 public String getBrand() { return brand; } public double getSize() { return size; } public double getPrice() { return price; } public String getConfiguration() { return configuration; } public int getStock() { return stock; } public double getTotalPrice() { return totalPrice; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("开始商品入库管理...\n"); // 创建华为手机对象 MobilePhone huawei = new MobilePhone("华为", 5.5, 3688.88, "8+128g 全面刘海屏"); // 华为手机入库 System.out.println("品牌型号:" + huawei.getBrand()); System.out.println("尺寸:" + huawei.getSize()); System.out.println("价格:" + huawei.getPrice()); System.out.println("配置:" + huawei.getConfiguration()); System.out.println("请输入华为手机的库存"); int huaweiStock = scanner.nextInt(); huawei.addStock(huaweiStock); System.out.println("库存华为手机的总金额:" + String.format("%.2f", huawei.getTotalPrice())); System.out.println(); // 创建小米手机对象 MobilePhone xiaomi = new MobilePhone("小米", 5.0, 2988.88, "4+64g 全面屏"); // 小米手机入库 System.out.println("品牌型号:" + xiaomi.getBrand()); System.out.println("尺寸:" + xiaomi.getSize()); System.out.println("价格:" + xiaomi.getPrice()); System.out.println("配置:" + xiaomi.getConfiguration()); System.out.println("请输入小米手机的库存"); int xiaomiStock = scanner.nextInt(); xiaomi.addStock(xiaomiStock); System.out.println("库存小米手机的总金额:" + String.format("%.2f", xiaomi.getTotalPrice())); System.out.println(); // 显示品牌型号和价格汇总 System.out.pr
最新发布
09-29
package students; import java.util.Scanner; public class StudentMain { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { // 创建第一个学生对象(使用无参构造函数和setter方法) Student student1 = new Student(); System.out.println("请输入第一个学生的学号:"); student1.setStu_ID(scanner.nextLine()); System.out.println("请输入第一个学生的姓名:"); student1.setName(scanner.nextLine()); // 添加年龄输入验证 int age1 = getValidAge(scanner); student1.setAge(age1); // 添加学分输入验证 double credit1 = getValidCredit(scanner); student1.setCredit(credit1); scanner.nextLine(); // 消费换行符 // 创建第二个学生对象(使用有参构造函数) System.out.println("请输入第二个学生的学号:"); String stuID = scanner.nextLine(); System.out.println("请输入第二个学生的姓名:"); String name = scanner.nextLine(); // 添加年龄输入验证 int age2 = getValidAge(scanner); // 添加学分输入验证 double credit2 = getValidCredit(scanner); Student student2 = new Student(stuID, name, age2, credit2); // 显示初始学分 System.out.println("第一个学生初始学分:" + student1.getCredit()); System.out.println("第二个学生初始学分:" + student2.getCredit()); // 更新学分 System.out.println("请输入第一个学生要增加的学分:"); double addCredit1 = getValidCredit(scanner); student1.updateCredit(addCredit1); System.out.println("请输入第二个学生要增加的学分:"); double addCredit2 = getValidCredit(scanner); student2.updateCredit(addCredit2); // 显示修改后的学分 System.out.println("第一个学生修改后的学分:" + student1.getCredit()); System.out.println("第二个学生修改后的学分:" + student2.getCredit()); // 显示学生详细信息 System.out.println("第一个学生详细信息:"); student1.displayStudent(); System.out.println("第二个学生详细信息:"); student2.displayStudent(); } catch (Exception e) { System.out.println("程序运行出错:" + e.getMessage()); } finally { scanner.close(); } } // 验证年龄输入的有效性 private static int getValidAge(Scanner scanner) { int age = 0; boolean valid = false; while (!valid) { try { System.out.println("请输入学生的年龄:"); age = scanner.nextInt(); if (age <= 0 || age > 120) { System.out.println("年龄必须在1到120之间,请重新输入。"); } else { valid = true; } } catch (Exception e) { System.out.println("请输入有效的年龄数值。"); scanner.nextLine(); // 清除无效输入 } } return age; } // 验证学分输入的有效性 private static double getValidCredit(Scanner scanner) { double credit = 0.0; boolean valid = false; while (!valid) { try { System.out.println("请输入学生的学分:"); credit = scanner.nextDouble(); if (credit < 0) { System.out.println("学分不能为负数,请重新输入。"); } else { valid = true; } } catch (Exception e) { System.out.println("请输入有效的学分数值。"); scanner.nextLine(); // 清除无效输入 } } return credit; } } package students; public class Student { private String stu_ID; private String name; private int age; private double credit; public Student() { } public Student(String stu_ID, String name, int age, double credit) { this.stu_ID = stu_ID; this.name = name; this.age = age; this.credit = credit; } public String getStu_ID() { return stu_ID; } public void setStu_ID(String stu_ID) { this.stu_ID = stu_ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getCredit() { return credit; } public void setCredit(double credit) { this.credit = credit; } public void updateCredit(double addCredit) { this.credit += addCredit; } public void displayStudent() { System.out.println("学生学号:" + stu_ID); System.out.println("学生姓名:" + name); System.out.println("学生年龄:" + age); System.out.println("学生学分:" + credit); } } 根据要求和代码完成实验报告
09-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值