- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码
import java.util.Scanner;
import java.text.DecimalFormat;
public class Calories
{
public static void main (String[] args)
{
int fat; //Grams of fat
int fcal; //Calories from fat
int total; //Number of Calories
long result; //Percent of calories from fat
System.out.println("This program finds the percent of calories from fat");
//Here the user inputs the numbers
Scanner keyboard = new Scanner (System.in);
System.out.print("Enter the total grams of fat " );
fat = keyboard.nextInt();
fcal = fat * 9;
System.out.print("Enter the total number of calories " );
total = keyboard.nextInt();
result = fcal / total * 100;
if(result <= 30)
{
System.out.println("Food is low in fat!");
}
DecimalFormat formatter = new DecimalFormat("#0");
System.out.println("Calories from fat : " + formatter.format(result) + "%");
}
}
无论我输入什么,我的输出总是为零。谁能告诉我我在这里做错了什么?我不确定是 DecimalFormat、数据类型还是 Keyboard.nextInt();是我问题的一部分,我已经多次检查过它并询问了另一个人,他说代码对他们来说运行良好。
最佳答案
您的fcal
和total
变量都是整数,因此您正在执行整数除法。随时fcal < total
,您将始终得到 0 作为答案。
在进行除法运算时,您需要将其转换为 double 。
从 result
开始,这也不太有效。声明为long
所以会损失精度。更改 result
的数据类型否则将结果转换回 long
.
关于java - 我试图找出脂肪中卡路里的百分比,但我的输出从来都不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8057884/
我使用 JarSplice 创建了一个 fat jar 文件,但是当我启动它时,我得到一个窗口(大小还可以),并在半秒后关闭...... 在“添加 jar”部分中,我添加了从 Eclipse 导出的
使用诸如 one-jar 或 sbt-assemble 之类的东西,从 fat jar 动态加载类的正确方法是什么?单 jar 示例: val loader = new URLClassLoader
我尝试在 Mac OSX Snowleopard 上安装 lxml 并不断收到错误消息: lipo: can't figure out the architecture type of: /var/f
我正在使用 EF 6 和 MVC 5 开发博客引擎。 我决定不使用 Repository 模式或 UoW,因为它已经在框架级别的 EF 6 中实现。 解决方案包含以下层。 DataModels 层:它
使用这个 repository当必须提供 oauth_token 时,我无法进行查询。我总是得到无效的签名。在代码中尝试了很多解决方案和调整,但没有任何效果。请帮忙。 这是来自提到的 git 的代码:
我正在开发基于交 key 服务器的嵌入式 Linux DLNA 服务器应用程序,我使用基于 openwrt 的定制 Linux 系统。 问题是 => 如果我在 DLNA 服务器构建媒体数据库时拔下 U
我是一名优秀的程序员,十分优秀!