【问题标题】:Why does my statement end in the middle for certain input为什么我的语句在某些输入的中间结束
【发布时间】:2018-02-14 07:58:39
【问题描述】:

我仍在处理这个项目,我已经花了无数个不眠之夜试图解决这个问题,但我再也无法解决了。所以问题是我应该为高于 10 的数字计算奖金值及以下10,声明如下。问题是如果我使用所有+ve 值(在这种情况下,如果输入低于10,则奖金将是-ve)。当我输入一个小于 10 (str<10) 的值时,它只打印 str 值并停止而不给出其他 6 个变量的输出。如果我在 10 (str>10) 上方输入一个数字,整个程序的工作方式与我希望它给出其他 6 个变量的相关输出的方式完全相同。

我认为str 有问题,因为我通过将10(奖金的输出是-ve)以下的值输入到str 之后的其他6 个变量来一一检查,保持str始终高于10 的值并且它有效。但是,当我在10 下方输入str 的值时,只打印前2 个打印语句并且程序结束。请帮忙。谢谢

import java.util.Scanner;
public class Please 
{
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) 
    {
        // TODO code application logic here
        int bonusstr = 0,bonusdex = 0,bonuscon=0,bonusintel=0,bonuswise=0,bonuschara=0;

        Scanner sc=new Scanner(System.in);

        System.out.print("Enter Level");
        int Level= sc.nextInt();

        System.out.print("Enter Str value");
        int str =sc.nextInt();

        System.out.print("Enter dex value");
        int dex =sc.nextInt();

        System.out.print("Enter con value");
        int con =sc.nextInt();

        System.out.print("Enter int value");
        int intel =sc.nextInt();

        System.out.print("Enter wise value");
        int wise =sc.nextInt();


        System.out.print("Enter cha value");
        int chara =sc.nextInt();

        if(str==10)
        {
            bonusstr=0;
        }
        else if(str%2==0 && str>10)
        {
            bonusstr=(str-10)/2;
        }
        else if(str%2==1 && str>10)
        {
            bonusstr=((str-10)-1)/2;
        }
        else if(str%2==0 && str<10)
        {
            bonusstr=(str-10)/2;
        }
        else if(str%2==1 && str<10)
        {
            bonusstr=((str-10)-1)/2;
        }

        if(dex==10)
        {
            bonusdex=0;
        }
        else if(dex%2==0 && dex>10)
        {
            bonusdex=(dex-10)/2;
        }
        else if(dex%2==1 && dex>10)
        {
            bonusdex=((dex-10)-1)/2;
        }
        else if(dex%2==0 && dex<10)
        {
            bonusdex=(dex-10)/2;
        }
        else if(dex%2==1 && dex<10)
        {
            bonusdex=((dex-10)-1)/2;
        }

        if(con==10)
        {
            bonuscon=0;
        }
        else if(con%2==0 && con>10)
        {
            bonuscon=(con-10)/2;
        }
        else if(con%2==1 && con>10)
        {
            bonuscon=((con-10)-1)/2;
        }
        else if(con%2==0 && con<10)
        {
            bonuscon=(con-10)/2;
        }
        else if(con%2==1 && con<10)
        {
            bonuscon=((con-10)-1)/2;
        }

        if(intel==10)
        {
            bonusintel=0;
        }
        else if(intel%2==0 && intel>10)
        {
            bonusintel=(intel-10)/2;
        }
        else if(intel%2==1 && intel>10)
        {
            bonusintel=((intel-10)-1)/2;
        }
        else if(intel%2==0 && intel<10)
        {
            bonusintel=(intel-10)/2;
        }
        else if(intel%2==1 && intel<10)
        {
            bonusintel=((intel-10)-1)/2;
        }

        if(wise==10)
        {
            bonuswise=0;
        }
        else if(wise%2==0 && wise>10)
        {
            bonuswise=(wise-10)/2;
        }
        else if(wise%2==1 && wise>10)
        {
            bonuswise=((wise-10)-1)/2;
        }
        else if(wise%2==0 && wise<10)
        {
            bonuswise=(wise-10)/2;
        }
        else if(wise%2==1 && wise<10)
        {
            bonuswise=((wise-10)-1)/2;
        }

        if(chara==10)
        {
            bonuschara=0;
        }
        else if(chara%2==0 && chara>10)
        {
            bonuschara=(chara-10)/2;
        }
        else if(chara%2==1 && chara>10)
        {
            bonuschara=((chara-10)-1)/2;
        }
        else if(chara%2==0 && chara<10)
        {
            bonuschara=(chara-10)/2;
        }
        else if(chara%2==1 && chara<10)
        {
            bonuschara=((chara-10)-1)/2;
        }

        System.out.print("\nLevel \t"+Level+"");
        if(str<10)
        {
            System.out.print("\nStr \t"+str+"["+bonusstr+"]");
        }
        else if(str>10)
        {
            System.out.print("\nStr \t"+str+"[+"+bonusstr+"]");

            if(dex<10)
            {
                System.out.print("\nDex \t"+dex+"["+bonusdex+"]");
            }
            else if(dex>10)
            {
                System.out.print("\nDex \t"+dex+"[+"+bonusdex+"]");
            }

            if(con<10)
            {
                System.out.print("\nCon \t"+con+"["+bonuscon+"]");
            }
            else if(con>10)
            {
                System.out.print("\nCon \t"+con+"[+"+bonuscon+"]");
            }

            if(intel<10)
            {
                System.out.print("\nIntel \t"+intel+"["+bonusintel+"]");
            }
            else if(intel>10)
            {
                System.out.print("\nIntel \t"+intel+"[+"+bonusintel+"]");
            }

            if(wise<10)
            {
                System.out.print("\nwise \t"+wise+"["+bonuswise+"]");
            }
            else if(wise>10)
            {
                System.out.print("\nWise \t"+wise+"[+"+bonuswise+"]");
            }

            if(chara<10)
            {
                System.out.print("\nChara \t"+chara+"["+bonuschara+"]");
            }
            else if(chara>10)
            {
                System.out.print("\nChara \t"+chara+"[+"+bonuschara+"]");
            }

            double hitpoints= Level*(Math.random()*1000 %6+1);
            System.out.print("\nhp \t"+hitpoints+" ");
        }            
    }

}

【问题讨论】:

  • 无数个不眠之夜?您的代码执行您编写的程序。在您的最后一个“mega-if-block”中,如果 (str 10) 时打印。一个提示:如果你在 if-else 块的两个部分都做同样的事情,只需不带条件地执行内部语句。您的最后一个“mega-if-block”没有意义,如果变量正好等于 10,您就不会打印它们。
  • 有很多扫描仪..不确定每个扫描仪的用途。你能帮我们解释一下每个变量的意义是什么,或者只是给出程序成功和失败的控制台
  • @Jayanth 。正如您所说,如果任何变量的输入值==10,则需要打印的奖励为 0 ,因为我没有打印语句,如果我输入 10,程序会跳过它。我对编程很陌生.它实际上是龙之地下城的基本游戏。我听说我可以在一个方法中进行所有计算并调用它,我试图阅读它,但没有找到任何适合或理解我的基本知识的东西。这就是为什么我必须编写这么多扫描仪、打印行和 if 语句的原因。我不知道该怎么做,我想弄清楚

标签: java if-statement printing


【解决方案1】:

if 语句中你还没有写代码。

它只有以下代码:

    System.out.print("\nLevel \t"+Level+"");
    if(str<10)
    {
        System.out.print("\nStr \t"+str+"["+bonusstr+"]");
    }

在此 if 语句中添加所需的代码。

注意:

  • 您没有在代码中的多个位置考虑== 条件,例如在下面 如果dex is 10

    应该执行什么代码部分
       if(dex<10)
        {
            System.out.print("\nDex \t"+dex+"["+bonusdex+"]");
        }
        else if(dex>10)
        {
            System.out.print("\nDex \t"+dex+"[+"+bonusdex+"]");
        }
    
  • 您不必要地使用了if-else 语句。上面的代码是一个例子。你在这两个部分都在做同样的事情(你在 if 做什么你在 else 做同样的事情)

【讨论】:

    【解决方案2】:
    if(str<10){
        System.out.print("\nStr \t"+str+"["+bonusstr+"]");
    }else if(str>10){
        System.out.print("\nStr \t"+str+"[+"+bonusstr+"]");"
    

    你忘了关闭 else if

    【讨论】:

    • 还有,我可以在一个中编写多个这样的 if 语句
    • 为什么不使用名为 printStat(String statName, int value) 的方法。它会执行 System.out.println(statname + value) ,然后您可以用 switch() 替换所有 if-else 并使用适当的参数调用该方法。您还可以创建一个读取键盘值的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多