【问题标题】:method that accepts two integers, and prints the sequence of numbers between them接受两个整数并打印它们之间的数字序列的方法
【发布时间】:2014-04-09 02:41:28
【问题描述】:

我是 java 的初学者,如果有人可以帮助我完成这个程序,我真的很感激。 我现在正在研究这个,我相信我需要使用扫描仪、int、循环和 if/else 语句。程序说明如下:

编写一个名为 printRange 的方法,该方法接受两个整数作为参数,并打印两个参数之间的数字序列,用方括号括起来。如果第一个参数小于第二个参数,则打印一个递增序列;否则,打印一个递减序列。如果两个数字相同,则该数字应打印在方括号之间。以下是对 printRange 的一些示例调用:

printRange(2, 7);

printRange(19, 11);

printRange(5, 5);

产生的输出应该如下:

[2, 3, 4, 5, 6, 7]

[19、18、17、16、15、14、13、12、11]

[5]

我写了一些,不知道在printRange方法中要做什么。我相信我需要 int 一些东西,这样我才能使 for 循环工作。我也不知道如何使数字在 [] 中带有 , 和空格。如果您能帮助我的代码工作,那将非常有帮助...

导入 java.util.*; // 用于扫描仪

公共类 PrintRange

{ //开始类 PrintRange

public static void main(String[] args)

  { //begin main method

    Scanner console = new Scanner(System.in);

    System.out.println("This program prints the sequence of numbers between the two numbers that you give");

    //obtain values
    System.out.println("Enter two numbers (x,y)");
    System.out.print("Number x: ");
    int x = console.nextInt();
    System.out.print("Number y: ");
    int y = console.nextInt();      

    int sequence;
    sequence = printRange(x,y);

    System.out.println("The sequence of your number is " + sequence);

  } //end main method

public static int printRange(int x, int y)
  { //begin printRange method   
    //this method accepts two parameters and return an integer
int 
    if (x > y)
    { //begin if statement
      //x is larger than y
        for (int i = x; i <= y; i++)
        {//begin for loop i
        System.out.print("[" + i + "]");
        range = range + i;
        }//end for loop i

    } //end if method

    else if (x < y)
    { //begin else if method
      //x is smaller than y
        for (int j = x; j >= y; j--)
        {//begin for loop j
        System.out.print("[" + j + "]");
        range = range + j;
        }//end for loop j
    } //end else if method

    else if (x == y)
    { //begin else if method
      //x is equal to y
        System.out.print(x);
        range = range + x;
    } //end else if method
return range;
  } //end printRange method

} //结束类PrintRange

提前致谢!

【问题讨论】:

  • 您的问题是什么?现在这似乎是一个家庭作业转储,根本行不通。
  • "写一个方法",谁来写?

标签: java if-statement for-loop integer


【解决方案1】:

由于您正在学习此内容,因此我不会为您编写答案。我会给你提示。

1.import java.util.Scanner 如果你还没有的话。

2.for循环为:for(int i= 0;i&lt;parameter.length();i++)

3.在此处查找 .substring():http://www.tutorialspoint.com/java/java_string_substring.htm

4.Scanner 的工作原理是这样的:

System.out.println("HERE you put what you ask the user for input");
int input = input.nextInt(); //this stores the user's input in var input

5.祝你好运!我建议你运行 for 循环并使用计数器 i 上的子字符串。

【讨论】:

    【解决方案2】:

    解决方案:

    public static void printRange( int x, int y) {
      if(y  > x) {
        for (int  j = x; j <= y; j++) {
            int i = 1*j + (x - 1);
            System.out.print(j + " ");
        }
      } 
      else if (x > y){
        for (int j = x; j>= y; j--) {
            int i = -1*y + (x -y +1) * 2  +1;
            System.out.print(j + " ");
        }
      } 
      else {
        System.out.print(x);
      }
    }
    

    【讨论】:

      【解决方案3】:
      public void printRange(int int1, int int2) { 
      if (int1<=int2){
          for (int i = int1; i <=int2; i++) { 
              System.out.print(i + " ");
          }
      } else {
          for (int i = int1; i >=int2; i--) { 
              System.out.print(i + " ");
          }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-02-20
        • 1970-01-01
        • 2021-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多