【问题标题】:Methods Java Arguments方法 Java 参数
【发布时间】:2015-02-10 16:04:30
【问题描述】:

老实说,这是我使用方法制作的首批程序之一,我希望在 B 修改部分得到一些帮助。我不明白我应该对方法中的参数使用什么语法,或者为什么我应该使用它。方法 showAll() 我已经做了,这是它的语法,但还没有走得太远,对此的任何帮助将不胜感激!

private static void showAll(String[] s, int i) { //there is an error with this
  JOptionPane.showMessageDialog(null, "ShowAll");
  System.out.println("Begin");
  System.out.println("End");
}

showAll() 需要什么:

/** create a method named showAll() that:
*   - returns no value
*   - accepts 2 arguments **** (B modification)
*       - an array of type String as an argument
*       - a variable of type int to accept the iMax variable as an argument
*   - contains 
*       - a MessageDialog that displays the message, "ShowAll"
*         **** (B modification) comment out or delete
*       more (B modification)
*       - a loop that outputs all records from 0 to the max i populated
*       - output statements before and after the loop designating begin and end of All
*/

其他相关语法: 案例陈述

switch(strArg) {
      case "A":      
      strArg.equals("A");
      addRec(strRecords, iMax);
      loopQuery();
      break;

      case "F": 
      strArg.equals("F");
      findRec(strRecords, iMax);
      loopQuery();
      break;

      case "S":
      strArg.equals("S");
      showAll(strRecords, iMax);
      loopQuery();
      break;

      default:
      errMessage();
}

我真的只需要使方法类似于指示,如果有人能引导我走上那将不胜感激的道路! :D

【问题讨论】:

    标签: java methods arguments switch-statement


    【解决方案1】:

    尝试使用 for 循环

    private static void showAll(String[] s, int i) { //there is an error with this
      JOptionPane.showMessageDialog(null, "ShowAll");
      System.out.println("Begin");
    
      for (int x = 0; x < s.length && x < i; x++) {
         System.out.println (s[x]);
      }
    
      System.out.println("End");
    }
    

    【讨论】:

    • 这看起来不像是正确的 for 循环。指令似乎说要打印从索引 0 到 iMax(这是参数 i)的值,但这不是您的代码所做的。
    • @ajb - 需要更多咖啡。我认为已修复。
    【解决方案2】:

    我有一个建议

    没必要

       strArg.equals("A");
    

    因为开关做到了

    PD:对不起,我正在努力学习英语

    【讨论】:

    • 什么都不做
    猜你喜欢
    • 1970-01-01
    • 2011-02-13
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    相关资源
    最近更新 更多