简体   繁体   中英

Shifting an array. Output is not correct.

I have been working on this program and will display the array contents to the user and ask how many positions he would like to shift off the right side of the array, and replace onto the left side.

This is what is supposed to look like:

Array contents:   1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
Shift how many positions? 5

Array contents:  11 12 13 14 15  1  2  3  4  5  6  7  8  9 10
Shift how many positions? 2

Array contents:   9 10 11 12 13 14 15  1  2  3  4  5  6  7  8
Shift how many positions? 0

Array contents:   9 10 11 12 13 14 15  1  2  3  4  5  6  7  8
Shift how many positions? -8

Array contents:   2  3  4  5  6  7  8  9 10 11 12 13 14 15  1
Shift how many positions? 15

Array contents:   2  3  4  5  6  7  8  9 10 11 12 13 14 15  1
Shift how many positions? 17

Array contents:  15  1  2  3  4  5  6  7  8  9 10 11 12 13 14
Shift how many positions? q

And im getting this:

Array Contents: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 

Shift how many positions?5
Array Contents: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 

Here is the main class:

public class Shift1 {

     /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

            Scanner kp = new Scanner(System.in);

                final int size = 15;
        char q = 'y';
        boolean flag = false;

        Shifter test = new Shifter(size);

        test.display();
        System.out.println();


            Scanner input = new Scanner(System.in);
            System.out.print("Shift how many positions?");{

            int value1 = input.nextInt();
            test.shift(value1);
            test.display();
        }
    }
}

And here is additional class:

public class Shifter 
{

public int [] data=new int[15];
    public Shifter()
    {
        int size=0;
}

public Shifter(int size){

    for (int i = 0; i < data.length; i++)
    {
        Random r = new Random(15);
        int second = r.nextInt(15) + 1;
        int temp = data[i];
        data[i] = data[second];
        data[second] = temp;
    }
}

public void shift(int pos){ 
    for(int x=0; x<pos; x++)
    {
        int cnt = data.length-1;
        int temp = data[cnt];
        for(cnt=data.length-1; cnt>0; cnt--)
    { 
        data[cnt] = data[cnt]-1;
    }
        data[0] =temp;
    }
}
public void display(){
    String values = "";
        for (int i = 0; i < data.length; i++)
        {
          if (i < 15)
        {
          values += (i + 1);
           if (i < 14)
        {
          values += ", ";
        }
    }
}
        System.out.printf("Array Contents: %s \n", values);
    } }
for(int x=0; x<pos; x++) {
    int cnt = data.length-1;
    int temp = data[cnt];

    for(cnt=data.length-1; cnt>0; cnt--) { 
        data[cnt] = data[cnt]-1;
    }
    data[0] =temp;
}

In your shift method you should be utilizing your x for loop variable. cnt and temp are always the same no matter what. So your not really doing anything in this loop.

If your allowed to use something other than an Array , maybe take a look at using a List . It has a unique method such as

http://docs.oracle.com/javase/7/docs/api/java/util/List.html#add(int,%20E)

which will allow you to add a number at the 0 index and it will auto shift the existing values right. So pop a value off the end and move it to the front size amount of times.

  • Introduce a loop in main() method.
  • Firstly read as string in order to recognize q .
  • Implement Shifter.shift() properly.
  • Add a space after the prompt.
  • Initialize the array properly.
  • Implement Shifter.display() properly.

Corrected codes:

public static void main(String[] args) {

    Scanner kp = new Scanner(System.in);

    final int size = 15;
    char q = 'y';
    boolean flag = false;

    Shifter test = new Shifter(size);
    Scanner input = new Scanner(System.in);

    for(;;) {
        test.display();
        System.out.println();
        System.out.print("Shift how many positions? ");

        String value = input.next();
        if (value.equals("q")) break;
        int value1 = Integer.parseInt(value);
        test.shift(value1);
    }
}

public Shifter(int size){

    for (int i = 0; i < data.length; i++)
    {
        data[i] = i + 1;
    }
}

public void shift(int pos){
    int max = pos % data.length;
    if (max < 0) max += data.length;
    for (int x=0; x<max; x++)
    {
        int cnt = data.length-1;
        int temp = data[cnt];
        for(cnt=data.length-1; cnt>0; cnt--)
        { 
            data[cnt] = data[cnt-1];
        }
        data[0] =temp;
    }
}
public void display(){
    String values = "";
    for (int i = 0; i < data.length; i++)
    {
        values += " ";
        if (data[i] < 10) values += " ";
        values += data[i];
    }
    System.out.printf("Array Contents: %s \n", values);
}

Though there are more points to be improved, this works as you said.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM