Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在数组末尾停止递增和递减?_Java_Android_Arrays - Fatal编程技术网

Java 如何在数组末尾停止递增和递减?

Java 如何在数组末尾停止递增和递减?,java,android,arrays,Java,Android,Arrays,我是编程的初学者。我想制作一个具有3个视图的android应用程序 文本视图(显示文本),以及 按钮(前进和后退) 我制作了一个要显示的单词数组1,2,3,4,5。 我把one放在xml上 当用户再次单击FORWARD时,显示two;当用户再次单击FORWARD时,显示two;当用户单击BACK时,显示two。我可以做到这一点 问题是当它到达5时,用户单击FORWARD;当它到达1时,用户单击BACK时,它崩溃 我希望按钮不起任何作用,甚至不返回到one。我希望用户知道它是列表的末尾。BACK按

我是编程的初学者。我想制作一个具有3个视图的android应用程序

  • 文本视图(显示文本),以及
  • 按钮(前进和后退) 我制作了一个要显示的单词数组
    1,2,3,4,5
    。 我把
    one
    放在
    xml

    当用户再次单击
    FORWARD
    时,显示
    two
    ;当用户再次单击
    FORWARD
    时,显示
    two
    ;当用户单击
    BACK
    时,显示
    two
    。我可以做到这一点

    问题是当它到达
    5
    时,用户单击
    FORWARD
    ;当它到达
    1
    时,用户单击
    BACK
    时,它崩溃

    我希望按钮不起任何作用,甚至不返回到
    one
    。我希望用户知道它是列表的末尾。
    BACK
    按钮也存在同样的问题。我希望它保持在
    one
    上。这是我的密码。请帮忙

    public class aba extends AppCompatActivity {
    
        int i = 0;
        String[] myList = {
            "one",
            "two",
            "three",
            "four",
            "five"
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_aba);
    
            }
            //Incrementing the value by l on forwardButton click
        public void forwardButton(View view) {
                TextView textDisplay = (TextView) findViewById(R.id.textDisplay);
                i = i + 1;
                textDisplay.setText(myList[i]);
                if (i == myList.length) {
                    i = i + 0;
                }
            }
            //decrementing the value by l on backButton click
        public void backButton(View view) {
            TextView textDisplay = (TextView) findViewById(R.id.textDisplay);
            i = i - 1;
            textDisplay.setText(myList[i]);
            if (i == 0) {
                i = i - 0;
            }
    
        }
    }
    

    数组的索引从0开始。 由于数组包含5个元素,
    length
    的值为5,但索引为0、1、2、3、4

    这意味着您不想低于0而高于4

    (此外,没有理由在每次单击按钮时初始化
    TextView

    像这样的方法应该会奏效:

    int i = 0;
    String[] myList={"one", "two", "three", "four", "five"};
    TextView textDisplay;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aba);
    
        textDisplay = (TextView) findViewById(R.id.textDisplay);
    }
    
    public void forwardButton(View view)  {
        if(i < myList.length - 1) {
            i++;
            textDisplay.setText(myList[i]);
        }
    
    }
    
    public void backButton(View view)  {
        if(i > 0) {
            i--;
            textDisplay.setText(myList[i]);
        }
    }
    
    inti=0;
    字符串[]myList={“一”、“二”、“三”、“四”、“五”};
    文本视图文本显示;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aba);
    textDisplay=(TextView)findViewById(R.id.textDisplay);
    }
    公共无效转发按钮(视图){
    如果(i0){
    我--;
    textDisplay.setText(myList[i]);
    }
    }
    
    数组的索引从0开始。 由于数组包含5个元素,
    length
    的值为5,但索引为0、1、2、3、4

    这意味着您不想低于0而高于4

    (此外,没有理由在每次单击按钮时初始化
    TextView

    像这样的方法应该会奏效:

    int i = 0;
    String[] myList={"one", "two", "three", "four", "five"};
    TextView textDisplay;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aba);
    
        textDisplay = (TextView) findViewById(R.id.textDisplay);
    }
    
    public void forwardButton(View view)  {
        if(i < myList.length - 1) {
            i++;
            textDisplay.setText(myList[i]);
        }
    
    }
    
    public void backButton(View view)  {
        if(i > 0) {
            i--;
            textDisplay.setText(myList[i]);
        }
    }
    
    inti=0;
    字符串[]myList={“一”、“二”、“三”、“四”、“五”};
    文本视图文本显示;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aba);
    textDisplay=(TextView)findViewById(R.id.textDisplay);
    }
    公共无效转发按钮(视图){
    如果(i0){
    我--;
    textDisplay.setText(myList[i]);
    }
    }
    
    试图从越界数组元素设置文本是导致崩溃的原因,因此在尝试访问
    myList
    数组之前,您需要定位
    if
    s以确保
    i
    myList
    数组的范围内

    实际上,您的
    if
    语句并没有起到多大的帮助作用。除了在有问题的代码行之后,加上或减去0根本不会改变变量,因此它不会有帮助

    //Incrementing the value by l on forwardButton click
    public void forwardButton(View view) {
        TextView textDisplay = (TextView) findViewById(R.id.textDisplay);
        i = i + 1;
        if (i >= myList.length) {
            // myList.length is past the end of the array, so set to length - 1
            i = myList.length - 1;
        }
        textDisplay.setText(myList[i]);
    }
    //decrementing the value by l on backButton click
    public void backButton(View view) {
        TextView textDisplay = (TextView) findViewById(R.id.textDisplay);
        i = i - 1;
        if (i < 0) {
            // -1 is before the beginning of the array, so set to 0 (beginning of the array)
            i = 0;
        }
        textDisplay.setText(myList[i]);
    }
    
    //按“前进”按钮上的l递增值单击
    公共无效转发按钮(视图){
    TextView textDisplay=(TextView)findViewById(R.id.textDisplay);
    i=i+1;
    如果(i>=myList.length){
    //myList.length超过数组的结尾,因此设置为length-1
    i=myList.length-1;
    }
    textDisplay.setText(myList[i]);
    }
    //在backButton上按l递减该值单击
    公共无效backButton(视图){
    TextView textDisplay=(TextView)findViewById(R.id.textDisplay);
    i=i-1;
    if(i<0){
    //-1在数组开头之前,因此设置为0(数组开头)
    i=0;
    }
    textDisplay.setText(myList[i]);
    }
    
    试图从越界数组元素设置文本是导致崩溃的原因,因此在尝试访问
    myList
    数组之前,您需要定位
    if
    s以确保
    i
    myList
    数组的范围内

    实际上,您的
    if
    语句并没有起到多大的帮助作用。除了在有问题的代码行之后,加上或减去0根本不会改变变量,因此它不会有帮助

    //Incrementing the value by l on forwardButton click
    public void forwardButton(View view) {
        TextView textDisplay = (TextView) findViewById(R.id.textDisplay);
        i = i + 1;
        if (i >= myList.length) {
            // myList.length is past the end of the array, so set to length - 1
            i = myList.length - 1;
        }
        textDisplay.setText(myList[i]);
    }
    //decrementing the value by l on backButton click
    public void backButton(View view) {
        TextView textDisplay = (TextView) findViewById(R.id.textDisplay);
        i = i - 1;
        if (i < 0) {
            // -1 is before the beginning of the array, so set to 0 (beginning of the array)
            i = 0;
        }
        textDisplay.setText(myList[i]);
    }
    
    //按“前进”按钮上的l递增值单击
    公共无效转发按钮(视图){
    TextView textDisplay=(TextView)findViewById(R.id.textDisplay);
    i=i+1;
    如果(i>=myList.length){
    //myList.length超过数组的结尾,因此设置为length-1
    i=myList.length-1;
    }
    textDisplay.setText(myList[i]);
    }
    //在backButton上按l递减该值单击
    公共无效backButton(视图){
    TextView textDisplay=(TextView)findViewById(R.id.textDisplay);
    i=i-1;
    if(i<0){
    //-1在数组开头之前,因此设置为0(数组开头)
    i=0;
    }
    textDisplay.setText(myList[i]);
    }
    
    在实际更改索引值之前,需要执行数组结尾的检查。此外,由于Java从0索引数组,因此需要检查索引是否小于数组长度

    //Incrementing the value by l on forwardButton click
    public void forwardButton(View view)  {
        TextView textDisplay = (TextView) findViewById(R.id.textDisplay)
    
        if (i < myList.length - 1) {
            i=i+1;
        }
    
        textDisplay.setText(myList[i]);
    }
    //decrementing the value by l on backButton click
    public void backButton(View view)  {
        TextView textDisplay = (TextView) findViewById(R.id.textDisplay);
    
        if(i > 0) {
            i=i-1;
        }
    
        textDisplay.setText(myList[i]);
        if(i==0){
            i=i-0;
        }
    
    }
    
    //按“前进”按钮上的l递增值单击
    公共无效转发按钮(视图){
    TextView textDisplay=(TextView)findViewById(R.id.textDisplay)
    如果(i0){
    i=i-1;
    }
    textDisplay.setText(myList[i]);
    如果(i==0){
    i=i-0;
    }
    }
    
    Y