如果最后一个数字为0,则为Java True

如果最后一个数字为0,则为Java True,java,arrays,loops,input,Java,Arrays,Loops,Input,我正在尝试制作一个java程序,允许用户输入任意数量的数字。如果他们输入0,程序会说树为false。我得到了工作,但我希望它,所以如果最后两个数字输入为0,程序将打印为真。0表示空值 例如,如果他们输入5个数字: 2 4 5 0 0=正确 2 0 5 0 0=错误 2 4 5 6 7=正确 所有其他的东西都很混乱,所以我把它简化了 System.out.println("Enter a number or enter 0 to represent null: "); for(in

我正在尝试制作一个java程序,允许用户输入任意数量的数字。如果他们输入0,程序会说树为false。我得到了工作,但我希望它,所以如果最后两个数字输入为0,程序将打印为真。0表示空值

例如,如果他们输入5个数字:

2 4 5 0 0=正确

2 0 5 0 0=错误

2 4 5 6 7=正确

所有其他的东西都很混乱,所以我把它简化了

    System.out.println("Enter a number or enter 0 to represent null: ");
    for(int i=0;i<num;i++)
    {
        arr[i] = input.nextInt();
        if(arr[i] == 0)
        {
            isTree = false;
        }
    }

    System.out.println("Is this a tree: " + isTree);
System.out.println(“输入一个数字或输入0表示空:”);

对于(int i=0;i执行您正在执行的操作,但还要检查末尾是否有两个零:

System.out.println("Enter a number or enter 0 to represent null: ");
for(int i=0;i<num;i++)
{
    arr[i] = input.nextInt();
    if(arr[i] == 0 && i < arr.length-2) //ignore if last two nodes
    {
        isTree = false;
    }
}
boolean isTree2 = (arr[arr.length-1] == 0 && arr[arr.length-2] == 0)
               || (arr[arr.length-1] != 0 && arr[arr.length-2] != 0);
System.out.println("Is this a tree: " + (isTree && isTree2));
System.out.println(“输入一个数字或输入0表示空:”);
对于(inti=0;i您可以使用regex“[1-9]+0{2}”=>一个或多个介于1到9之间的数字,以2 0结尾

代码示例:

System.out.println("Enter a number or enter 0 to represent null: ");
java.util.regex.Pattern pattern = Pattern.compile("[1-9]+0{2}");
java.util.regex.Matcher matcher = pattern.matcher("20500");
boolean isTree = false;
while(matcher.find()) {
    isTree = true;
}
System.out.println("Is this a tree: " + isTree);

你可以试试这个工具来测试你的正则表达式:

我想你想要的是,如果最后两位是零,那么你的程序会说它是真的

int length = arr.length;
boolean isTree = false;
if(length >= 2) {
    if(arr[length-1] == 0 && arr[length-2] == 0) {
        isTree = true;
    }
}
System.out.println(isTrue);
System.out.println(“输入一个数字或输入0表示空:”);
布尔cond=false;

对于(int i=0;i我不会在您输入输入时进行检查,而是在所有输入完成后进行检查。我会将您的所有输入连接到一个字符串中,然后计算该字符串以确定它是否满足树条件

public static void main(String[] args) throws Exception {
    int num = 5;
    int[] arr = new int[num];

    System.out.println("Enter a number or enter 0 to represent null: ");
    for (int count = 0; count < 5; count++) {
        for (int i = 0; i < num; i++) {
            arr[i] = input.nextInt();
        }

        // Arrays.toString(arr) results in [2, 4, 5, 0, 0]
        // Remove all brackets, commas, and spaces
        String inputs = Arrays.toString(arr).replaceAll("\\[", "").replaceAll("\\]", "").replaceAll(", ", "");

        boolean isTree = true;
        // Only have to check for ending 00 if you know a 0 is in your inputs
        if (inputs.contains("0")) {
            isTree = inputs.endsWith("00") && !inputs.substring(0, inputs.length() - 2).contains("0");
        }

        System.out.println("Is this a tree: " + isTree);
        System.out.println("");
    }
}
publicstaticvoidmain(字符串[]args)引发异常{
int num=5;
int[]arr=新int[num];
System.out.println(“输入一个数字或输入0表示空:”);
对于(int count=0;count<5;count++){
for(int i=0;i
结果:


请解释更多关于零的含义。前两个示例之间的行为差异不清楚。你说的树是假的是什么意思?@Sasha Binary search tree我猜是这样做的,但当用户输入2 4 5 0 0时,它仍然打印为false
public static void main(String[] args) throws Exception {
    int num = 5;
    int[] arr = new int[num];

    System.out.println("Enter a number or enter 0 to represent null: ");
    for (int count = 0; count < 5; count++) {
        for (int i = 0; i < num; i++) {
            arr[i] = input.nextInt();
        }

        // Arrays.toString(arr) results in [2, 4, 5, 0, 0]
        // Remove all brackets, commas, and spaces
        String inputs = Arrays.toString(arr).replaceAll("\\[", "").replaceAll("\\]", "").replaceAll(", ", "");

        boolean isTree = true;
        // Only have to check for ending 00 if you know a 0 is in your inputs
        if (inputs.contains("0")) {
            isTree = inputs.endsWith("00") && !inputs.substring(0, inputs.length() - 2).contains("0");
        }

        System.out.println("Is this a tree: " + isTree);
        System.out.println("");
    }
}