【问题标题】:why output statement not showing integer output为什么输出语句不显示整数输出
【发布时间】:2018-02-27 21:15:49
【问题描述】:

问题:单掷 2 个公平(均匀加权)骰子,求每个骰子掷出的点数不同且总和为 6 的概率。

我的代码有什么问题 最后 3 个打印语句不起作用

public class Test{

    public static void main(String[] args){
        System.out.println("Enter");

        int count=0;
        int deno=36;

        for(int i=1; i<7; i++){
            for(int j=1; i<7; j++){
            if((i+j)==6 && i!=j){
                count++;
                }
            }

        }

        for(int i = 2; i<=10; i++){
            if(count%i==0 && deno%i==0){
                count=count/i;
                deno=deno/i;
            }

        }

        System.out.println(count); 
        System.out.println("/"); 
        System.out.println(deno); 
    }
}

【问题讨论】:

  • 错字导致无限循环 - for(int j=1; i&lt;7; j++){ -> for(int j=1; j&lt;7; j++){
  • 最后 3 个打印输出语句在控制台上没有给出任何 noutput
  • 请贴出修改后的代码
  • 谢谢你的朋友,非常感谢你

标签: java probability


【解决方案1】:

您通常可以通过在循环中打印 ij 来轻松找到此类错误,然后您就会发现问题所在。

for(int i=1; i<7; i++){
    for(int j=1; i<7; j++){  // <--- TYPO
        if((i+j)==6 && i!=j){
            System.out.println("i " + i);
            System.out.println("j " + j);
            count++;
        }
    }
}

您应该将第二个 for 循环更改为:

 for(int j=1; j<7; j++){

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多