java - Java的作业问题:我正在尝试运行我的程序,它没有显示错误但是当我尝试运行它时它不起作用?
问题描述
我的作业是创建一个程序,根据你吃的饼干数量告诉你消耗了多少卡路里。但是当我运行该程序时,它没有显示任何内容,我需要它来完成我的作业。
这是更多上下文的代码:
package cookie.calories;
import java.util.Scanner;
public class CookieCalories {
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
int TotalNumberOfCookiesPerBag = 40;
int CaloriesPerServing = 300;
int ServingsPerBag = 10;
//The next equation represent the number of cookies per servings
int NumberOfCookiesPerServings = TotalNumberOfCookiesPerBag/ServingsPerBag;
//number of calories per cookies
int CaloriesPerCookies =CaloriesPerServing/NumberOfCookiesPerServings;
Scanner ob2 = new Scanner(System.in);
System.out.println("This programm can determined how many calories you ate depending on the number of cookie you ate.");
//Below this sentence, the amount of calories will be determined
System.out.println("Enter the number of cookies you ate");
int Cookies = ob.nextInt(4);
int TotalCaloriesConsumed = CaloriesPerCookies*Cookies;
System.out.println(TotalCaloriesConsumed + "for the amount of cookies you ate");
}
}
该程序向我显示,我在编写程序的方式上没有犯任何错误,但是当我播放它时,它没有在输出中显示任何内容。为什么?
解决方案
Remove the '4' from the nextInt method params.
int Cookies = ob.nextInt();
Also you don't need another Scanner object, you can use the same one to get input from the user multiple times, so you can remove this from your code.
Scanner ob2 = new Scanner(System.in);
推荐阅读
- nativescript - 我可以创建游乐场应用程序的修改版本吗?
- html - 如何在表格中将两个按钮堆叠在一起
- eclipse - Eclipse 启动时缺少 javax.xml.bind 与 Java 11
- reactive-programming - WebFlux/Reactor:使用 doOnComplete 检查 Flux 执行前后的条件
- java - 带有状态栏的 Vaadin 14,6 AppLayout?
- c++ - 不能在类中创建 2 个数组,否则不允许不完整的类型 c++
- python - 获取嵌套字典中的键
- node.js - 使用 Puppeteer 刮取亚马逊价格
- reactjs - Next.js 如何在我的联系表单中实现 react-google-recaptcha
- arrays - 在矩阵中查找数组