我一直在研究这个程序,并将向用户显示数组内容,并询问他希望从阵列右侧移出多少个位置,并替换到左侧。
这应该是这样的:
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
我得到了这个:
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
这是主要课程:
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();
}
}
}
这是附加课程:
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);
} }
答案 0 :(得分:0)
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;
}
在shift
方法中,您应该使用x
for循环变量。无论如何,cnt
和temp
始终相同。所以你在这个循环中并没有真正做任何事情。
如果您允许使用Array
以外的其他内容,请查看使用List
。它有一个独特的方法,如
http://docs.oracle.com/javase/7/docs/api/java/util/List.html#add(int,%20E)
允许您在0索引处添加数字,它会自动将现有值右移。因此,最后弹出一个值并将其移至前size
次。
答案 1 :(得分:0)
main()
方法中引入循环。q
。Shifter.shift()
。Shifter.display()
。更正后的代码:
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);
}
虽然还有更多要点需要改进,但正如你所说的那样。