【问题标题】:calling function or returning back to main method调用函数或返回主方法
【发布时间】:2017-02-27 17:02:30
【问题描述】:

在一次迭代后输入为'y'后调用演示方法后,它应该再次要求继续吗? (是/否)请帮助!提前谢谢

public void demo()
        {
            System.out.println("Withdrawal or deposit? (w/d) :");
            char choice = s.next().charAt(0);
            System.out.println("Checking or Savings? (c/s) :");
            char choicetwo = s.next().charAt(0);
            System.out.println("Amount? : ");
            int amt = s.nextInt();
        }

  public static void main(String[] args)
    {
        Scanner  s = new Scanner(System.in);
        Child c = new Child();
        c.display();
        c.demo();
        System.out.println("Continue? : ");
        char input = s.next().charAt(0);
        while(input == 'y')
        {
            c.demo();
        }

【问题讨论】:

    标签: java inheritance methods


    【解决方案1】:
        System.out.println("Continue? : ");
        char input = s.next().charAt(0);
        while(input == 'y')
        {
        c.demo();
        System.out.println("Continue? : ");
        input = s.next().charAt(0);
        }
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    【解决方案2】:

    您需要将变量赋值放入 while 循环中:

            System.out.println("Continue? : ");
            char input = s.next().charAt(0);
            while(input == 'y')
            {
                c.demo();
                System.out.println("Continue? : ");
                input = s.next().charAt(0);                
            }
    

    【讨论】:

      猜你喜欢
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 2016-06-10
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多