Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
statment的Java代码格式化_Java_For Loop_Formatting - Fatal编程技术网

statment的Java代码格式化

statment的Java代码格式化,java,for-loop,formatting,Java,For Loop,Formatting,对于循环,他们是否有更好的格式化方法 用户输入两个初始化为x和y的整数,程序将第一个数字的值打印为+,第二个数字的值打印为- 例如,9和5将导致 +++++++++----- 我尝试使用嵌套的for循环,但结果是 +-----+-----+-----+-----+-----+-----+-----+-----+----- Scanner sc=新扫描仪(System.in); 整数x,y,总计=0; System.out.println(“输入两个大于0且不大于20的数字!”); x=sc.ne

对于循环,他们是否有更好的格式化方法

用户输入两个初始化为x和y的整数,程序将第一个数字的值打印为+,第二个数字的值打印为-

例如,9和5将导致

+++++++++-----

我尝试使用嵌套的
for循环
,但结果是

+-----+-----+-----+-----+-----+-----+-----+-----+-----

Scanner sc=新扫描仪(System.in);
整数x,y,总计=0;
System.out.println(“输入两个大于0且不大于20的数字!”);
x=sc.nextInt();
y=sc.nextInt();
绘制(x,y);
专用静态空白绘制(整数x,整数y){

对于(int i=1;i如果要使其变小:)

私有静态无效绘制(int x,int y){
对于(int i=-x;iSystem.out.print(i我会一起去掉循环,并使用


你到底想达到什么目的?这是为了一个大学作业,输入的第一个数字被打印成加号,第二个数字被打印成负号。到目前为止,代码按我的意愿运行。我只是想看看如何改进它,因为没有人的代码是完美的,对吧!?这和格雷厄姆·沃伦德的代码不一样吗?替换“+”,“-”与之相反:)x=5,y=4的结果是------+++Java中实际的
。@GrahamWarrender它是三元运算符
?:
您忘记打印它了:
System.out.print(StringUtils.repeat(“+”,x)+StringUtils.repeat(“y”,x));
    Scanner sc = new Scanner(System.in);
    int x,y,total = 0;

    System.out.println("Enter two numbers greater than 0 and no greater than 20!");
    x = sc.nextInt();
    y = sc.nextInt();

    draw(x,y);  

    private static void draw(int x, int y) {
    for(int i=1; i<=x; i++)
    {
        System.out.print("+");

    }
    for(int j=1; j<=y; j++)
    {
        System.out.print("-");
    }
}
public class MainClass {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int x, y, total = 0;

        System.out.println("Enter two numbers greater than 0 and no greater than 20!");
        x = sc.nextInt();
        y = sc.nextInt();

        draw(x, y);
    }

    private static void draw(int x, int y) {
        for (int i = 1; i <= x; i++) {
            System.out.print("+");

        }
        for (int j = 1; j <= y; j++) {
            System.out.print("-");
        }
    }

}
Enter two numbers greater than 0 and no greater than 20!

9   --> Hit enter.
5   --> Hit enter again!
+++++++++-----
private static void draw(int x, int y) {
    for (int i = -x; i < y; i++) {
        System.out.print(i<0 ? "+" : "-");
    }
}
private static void draw(int x, int y) {
    System.out.print(Stringutils.repeat("+", x));
    System.out.println(StringUtils.repeat("-", y));
}