简体   繁体   English

移位数组。 输出不正确。

[英]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. 在您的shift方法中,您应该将x用于循环变量。 cnt and temp are always the same no matter what. 无论如何, cnttemp始终相同。 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 . 如果允许使用Array以外的其他东西,也许可以看看使用List It has a unique method such as 它具有独特的方法,例如

http://docs.oracle.com/javase/7/docs/api/java/util/List.html#add(int,%20E) 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. 这将允许您在0索引处添加一个数字,并将自动将现有值右移。 So pop a value off the end and move it to the front size amount of times. 所以弹出关闭端的值,并且将其移动到前size的倍量。

  • Introduce a loop in main() method. main()方法中引入一个循环。
  • Firstly read as string in order to recognize q . 首先以字符串形式读取以识别q
  • Implement Shifter.shift() properly. 正确实现Shifter.shift()
  • Add a space after the prompt. 在提示后添加一个空格。
  • Initialize the array properly. 正确初始化阵列。
  • Implement Shifter.display() properly. 正确实现Shifter.display()

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. 尽管还有很多要改进的地方,但是您可以这样做。

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

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