简体   繁体   English

我一直在编写此代码,并调用了方法replaceLessThan(x,e),但我无法正确编写此方法

[英]I have been working on this code and I called my method replaceLessThan(x,e), and I can't properly write this method

public class ArrayExamples {

    public static void main(String[] args) {
    int c = 3;
    int d =2;
    System.out.println("c is " + c + " d is " + d);
    swapInts(3,2);
        int [] a = {1,2,3};
        int [] b = {2,2,3};
        int [] x = {3,45,17,2,-1,44,9,23,67,2,-6,-23,-100,12,5,1212};
        int e = 12;
        System.out.println();
        for ( int z: a){
            System.out.print( z + " ");

        }
        System.out.println();
        for ( int y: b){
            System.out.print( y + " ");
        }
        swapIntArrays (a,b);
        System.out.println();
        for ( int z: x){
            System.out.print( z + " ");
        }

        replaceLessThan(x,e);
    }


    public static void replaceLessThan(int[] x, int e) {    
        System.out.println();
        for (int counter = 0 ; counter<x.length; counter++){
            if ( x[counter] < e){

                System.out.print (x[counter] + " ");
            }

        }

    }


    public static void swapInts(int c, int d){
        int temp = c;
        c=d;
        d=temp;
        System.out.println("c is " + c + " c is " + d);


    }

    public static void swapIntArrays (int []a, int []b){
        System.out.println();
        for(int i1=0; i1 < a.length; i1++){
            int temp = a[i1];
            a[i1] = b[i1];
            b[i1]= temp;

            System.out.print(a[i1] + " ");

        }
        System.out.println();

        for(int i1=0; i1 < b.length; i1++){
            System.out.print(b[i1] + " ");
        }

        System.out.println();
    }

}

my other methods are working fine but I can't figure out how to manipulate the int[]x array to get 12 to replace all numbers less than 12. I can get numbers less than 12 from int[]x to print but I can't get 12 to replace all the numbers less than 12 我的其他方法工作正常,但是我无法弄清楚如何操作int [] x数组以获取12来替换小于12的所有数字。我可以从int [] x中获取小于12的数字以进行打印,但是我可以不能得到12来替换少于12的所有数字

Modify your replaceLessThan method as given below to replace all the elements less than e . 如下所示修改您的replaceLessThan方法,以替换所有小于e的元素。

    public static void replaceLessThan(int[] x, int e) {    
        System.out.println();
        for (int counter = 0 ; counter<x.length; counter++){
            if ( x[counter] < e){
                x[counter] = e; // Add this line to replace elements.
            }
            System.out.print (x[counter] + " "); // Move this statement out of if condition             
        }

暂无
暂无

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

相关问题 如果没有其他方法被调用,我将如何调用该方法? - How would I have a method called if no other method has been called? 我有一段代码,即带有try和catch块的方法。 catch块捕获异常,我该如何为它编写失败的junit? - I have a piece of code i.e a method with try and catch block. The catch block catches the exception how can I write the failure junit for it? 我可以判断是否调用了抽象方法吗? - Can I tell if an abstract method has been called? 如何使用Temple方法,泛型方法或其他方法优化代码?现在我必须编写三遍类似的代码 - How to optimize my code using temple method or generic method or other method?Now I have to write the similar code three times 我的代码有什么问题,我不能使用KeyPressed方法? - What wrong with my code , I can't use KeyPressed method? 我无法在我的代码中使用 findOne() 方法 - I can't use findOne() method in my code 为什么我的servlet代码中不能使用doGet方法? - Why can't i use doGet method in my servlet code? 我该如何使用反射对私有方法进行存根,然后在调用return时说出测试代码 <X> 对象? - how can I stub the private method with reflection and then say the test code whenever it is called return `stubbed <X>` object? 我对I / O中的write方法不了解的代码 - A code I don't understand about the write method in I/O 我在双向链接列表中查找节点的方法无法正常工作,我也不知道为什么? - My method for finding a node in a double linked list isn't working properly and I don't know why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM