2018携程笔试

本文介绍了一个使用Java编写的简单程序,该程序通过控制台输入坐标并打印特定的图形。主要实现了根据输入的坐标绘制等腰直角三角形,并考虑了不同情况下的图形绘制逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String[] num = sc.nextLine().split(" ");
		int x1 = Integer.parseInt(num[0]);
		int y1 = Integer.parseInt(num[1]);
		int x2 = Integer.parseInt(num[2]);
		int y2 = Integer.parseInt(num[3]);

		print(x1, y1, x2, y2);
	}
	public static void print(int x1, int y1, int x2, int y2) {
		// 首先判断输入的数据是否满足条件
		if (x1 == x2 && Math.abs(y1 - y2) > 1) {
			int line = (Math.abs(y1 - y2) - 1) / 2;
			int yushu = Math.abs(y1 - y2) % 2;
			int line1 = line;
			int y11 = y1 - line1;
			int y21 = y2 + line1;
			// 分成两种情况进行讨论,第一种是|y1-y2|为奇数
			if (yushu == 1) {
				int count = 0;
				for (int i = 1; i <= 10; i++) {
					if(i<x1){
						for (int j = 1; j <= 10; j++) {
							System.out.print("-" + " ");
						}
						System.out.println();
					}
					// 上面两个等腰直角
					if (i == x1 + count && line >= 0) {
						for (int j = 1; j <= 10; j++) {
							if (j >= y1 - count && j <= y1 + count || j >= y2 - count && j <= y2 + count) {
								System.out.print("#" + " ");
							} else {
								System.out.print("-" + " ");
							}
						}
						count++;
						System.out.println();
						line--;
						// 下面这个大的等腰直角
					} else if (line < 0) {
						for (int j = 1; j <= 10; j++) {
							if (j >= (y11) && j <= (y21)) {
								System.out.print("#" + " ");
							} else {
								System.out.print("-" + " ");
							}
						}
						y11++;
						y21--;
						System.out.println();
					} 
				}
			}
			// 第二种情况,|y1-y2|为偶数
			if (yushu == 0) {
				int count = 0;
				for (int i = 1; i <= 10; i++) {
					if(i<x1){
						for (int j = 1; j <= 10; j++) {
							System.out.print("-" + " ");
						}
						System.out.println();
					}
					// 上面两个等腰直角
					if (i == x1 + count && line >= 0) {
						for (int j = 1; j <= 10; j++) {
							if (j >= y1 - count && j <= y1 + count || j >= y2 - count && j <= y2 + count) {
								System.out.print("#" + " ");
							} else {
								System.out.print("-" + " ");
							}
						}
						count++;
						System.out.println();
						line--;
						// 下面这个大的等腰直角
					} else if (line < 0) {
						for (int j = 1; j <= 10; j++) {
							if (j >= (y11 - 1) && j <= (y21 + 1)) {
								System.out.print("#" + " ");
							} else {
								System.out.print("-" + " ");
							}
						}
						y11++;
						y21--;
						System.out.println();
					}
				}
			}
		}

	}
}

代码很潦草,能基本实现要求,但是还需要增加一个输出的判断,当输出到一行的末尾时,不应该带有空格。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值