首页 > 解决方案 > 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");    

    }

}

该程序向我显示,我在编写程序的方式上没有犯任何错误,但是当我播放它时,它没有在输出中显示任何内容。为什么?

标签: javanetbeansjava.util.scanner

解决方案


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);

推荐阅读