帮忙做几道JAVA题目

2024-12-04 21:35:53
推荐回答(1个)
回答1:

都是接受的命令行参数

// 1. Delta.java
public class Delta
{
public static void main(String[] args)
{
try
{
int n = Integer.parseInt(args[0]); // 接受命令行参数
int[][] a = new int[n][n];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
a[i][j] = 0;
if(j == 0 || i == j)
{
a[i][j] = 1;
}
else if (i > j)
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if (a[i][j] != 0)
{
System.out.printf("%4d ", a[i][j]);
}
if(j == n - 1)
{
System.out.println();
}
}
}
}
catch (Exception e)
{
System.out.println("请输入整数");
}
}
}
----------------------------------------------------------------
// 2. Sort.java
import java.util.*;

public class Sort
{
public static void main(String[] args)
{
try
{
int[] a = new int[args.length];
for (int i = 0; i < args.length; i++)
{
a[i] = Integer.parseInt(args[i]);
}
// Arrays.sort(a); // 这里用了jdk自带的排序方法
// 你也可以用下面的冒泡升序排序
for (int i = 0; i < args.length - 1; i++)
{
for (int j = i + 1; j < args.length; j++)
{
if (a[i] > a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("最小数 " + a[0]);
System.out.println("最大数 " + a[a.length - 1]);
}
catch (Exception e)
{
System.out.println("请输入一组整数");
}
}
}
---------------------------------------------------------------
// 3. Args.java
public class Args
{
public static void main(String[] args)
{
for (int i = 0; i < args.length; i++)
{
System.out.println(args[i]);
}
}
}
----------------------------------------------------------
// 4. NewString.java
public class NewString
{
public static void main(String[] args)
{
StringBuffer str = new StringBuffer();
char ch = args[1].charAt(0);
for (int i = 0; i < args[0].length(); i++)
{
if (args[0].charAt(i) != ch)
{
str.append(args[0].charAt(i));
}
}
System.out.println(str);
}
}