简体   繁体   English

我如何使我的答案看起来像[z1,z2…]

[英]How do i make my answer look like [z1, z2 …]

I am working on a program. 我正在做一个程序。 I have gotten to my end result but not the format that is required. 我已经达到了最终结果,但不是必需的格式。 My requirement is that the output should be formatted like [z1, z2 ...] 我的要求是输出的格式应为[z1,z2 ...]

public class Range {

    public static void main(String[] args) {

        Scanner console = new Scanner(System.in);
        System.out.println("Please input your frist number.");
        int input1 = red(console);

        System.out.println("Please input your second number.");
        int input2 = red(console);
        printRange(input1, input2);
    }

    public static int red(Scanner console) {
        int x = console.nextInt();
        return x;
    }

    public static void printRange(int input1, int input2) {

        if (input1 > input2) {
            for (int Z = input1; Z >= input2; Z--) {
                System.out.print("[" + Z + "]");
            }
        } 
        else if (input1 < input2) {
            for (int Z = input1; Z <= input2; Z++) {
                System.out.print("[" + Z + "]");
            }
        } 
        else {
            System.out.print("[" + input1 + "]");
        }
    }
}
public static void printRange(int e, int r){
    int [] result = new int[1];
    if(e<r){
        result = new int[r-e];
        for(int i=0;i<(r-e);i++)
            result[i]=e+i+1;
    }else if(r<e){
        result = new int[e-r];
        for(int j=0;j<(e-r);j++)
            result[j]=r+j+1;
    }else
        result[0]=e;
    System.out.println(Arrays.toString(result));
}

Try the below code, hope that's what you need: 尝试以下代码,希望这是您需要的:

import java.util.Scanner;

public class Range {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println("Please input your frist number.");
        int input1 = red(console);
        System.out.println("Please input your second number.");
        int input2 = red(console);
        printRange(input1, input2);
    }

    public static int red(Scanner console) {
        int x = console.nextInt();
        return x;
    }

    public static void printRange(int input1, int input2) {
        System.out.print("[");

        if (input1 > input2) {
            for (int Z = input1; Z >= input2; Z--) {
                if(Z != input1){
                    System.out.print( ", " );
                }
                System.out.print( Z );
            }
        } else if (input1 < input2) {
            for (int Z = input1; Z <= input2; Z++) {
                if(Z != input1){
                    System.out.print( ", " );
                }
                System.out.print( Z );
            }
        } else {
            System.out.print( input1 );
        }
        System.out.print( "]" );
    }
}

Edit: Explanation: Here is what it takes to get you the desired output. 编辑:说明:这是获取所需输出所需要的。

  1. When you said that you want all your numbers enclosed in [], all you need to do is get the brackets out of the loop so they are printed only once. 当您说希望将所有数字括在[]中时,您要做的就是将方括号移出循环,以便仅打印一次。 Opening bracket before adding the numbers and closing bracket after the numbers are added. 在添加数字之前用大括号括住,在数字添加之后用括号括住。
  2. When you want to add the list of string in the brackets, you will need the separator (,) between two numbers. 如果要在方括号中添加字符串列表,则需要在两个数字之间使用分隔符(,)。 Hence I have added a print statement before adding each number. 因此,我在添加每个数字之前添加了一条打印语句。 The if condition restricts the adding of separator before the first number. if条件限制在第一个数字之前添加分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何快速((A ^ z1 * y ^ z2)mod P)mod Q - how can i make fast ((A^z1 * y^z2) mod P) mod Q 如何使用Quartz库每天在XX:XX处安排从日期X1 / Y1 / Z1到日期X2 / Y2 / Z2的某些任务? - How can I use Quartz library to schedule some task from date X1/Y1/Z1 to date X2/Y2/Z2 at XX:XX every day? 两点之间的随机位置(x1,z1,x2,z2) - Random location between two points (x1, z1, x2, z2) 为什么 x *= (y1*y2*y3)/z1 没有给出与 JAVA 中的 x = x * (y1*y2*y3)/z1 相同的答案 - Why is x *= (y1*y2*y3)/z1 not giving the same answer as x = x * (y1*y2*y3)/z1 in JAVA 如何让我的应用看起来像一个标准的Android应用程序? - How do I make my app look like a standard Android app? 如何使ListViewItem看起来像Twitter应用程序? - How can I make my ListViewItem look like the Twitter app? 在Sony z2上从图库中选取图像时发生IllegalArgumentException - IllegalArgumentException while picking image from gallery on Sony z2 如何在 java 中正确打印我的答案 - How do i make my answer print correctly in java 我如何做才能让我的测验让我回答? - How do I make it so my quiz lets me answer? FileWriter false将文件从字符串A覆盖到Z,但是仅保留字符串Z; 如何保持字符串AZ? - FileWriter false overwrites file from string A to Z BUT leaves only string Z; how do I keep string A-Z?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM