Java上的作业问题:我正在尝试运行我的程序,但没有显示错误,但是当我尝试运行它时,它不起作用了?

时间:2019-09-06 20:59:10

标签: java netbeans java.util.scanner

我的作业是创建一个程序,该程序根据您吃的饼干的数量来告诉您消耗了多少卡路里。但是当我运行该程序时,它什么也没有显示,我需要它来完成我的作业。

以下是更多上下文的代码:

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

    }

}

程序显示出编写程序的方式没有发生任何错误,但是在播放程序时,输出中未显示任何内容。为什么?

2 个答案:

答案 0 :(得分:0)

从nextInt方法参数中删除“ 4”。

int Cookies = ob.nextInt();

此外,您不需要另一个Scanner对象,可以多次使用同一对象来获取用户输入,因此可以从代码中删除该对象。

Scanner ob2 = new Scanner(System.in);

答案 1 :(得分:0)

当我运行您的程序时,我将得到此输出

This programm can determined how many calories you ate depending on the number of 
cookie you ate.
Enter the number of cookies you ate

例如,当我输入4并按Enter时,我会收到此错误

Exception in thread "main" java.util.InputMismatchException: For input string: "4" 
under radix 4
    at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
    at testasdf.Testasdf.main(Testasdf.java:38)

不太确定为什么要使用“ ob.nextInt(4);”。而不是“ ob.nextInt();”应该将您的代码更改为nextInt();

所以我不确定为什么什么都没给您显示,确定要设置输出屏幕吗? 您正在使用什么IDE?