简体   繁体   English

在For循环(Java)中初始化变量

[英]Initializing variables in For Loops (Java)

I need to create a program that takes in 4 integers from the user and makes sure if the input is between 0 and 255 exclusive. 我需要创建一个程序,该程序需要用户输入4个整数,并确保输入是否介于0和255之间。 Everything is working except for my final output, the IP address which is pretty much all of the inputs in one string. 除了我的最终输出(几乎所有输入都在一个字符串中)的IP地址之外,一切都正常。 It keeps printing out 0 because I had to initialize my variables before using them in an array, so I assigned them the value of 0. However, the value was supposed to change in the for loop, but it still prints out the incorrect value. 它一直输出0,因为在数组中使用变量之前必须先初始化变量,因此我为它们分配了0值。但是,该值在for循环中应该已更改,但是它仍然输出不正确的值。 I can only print out the IP address once and it has to be at the end. 我只能打印一次IP地址,并且必须在末尾。 I know that there is an easier way of doing this, but I still want to know how fix this issue for future reference. 我知道有一个更简单的方法可以执行此操作,但是我仍然想知道如何解决此问题以供将来参考。 The following is my code: 以下是我的代码:

import java.util.Scanner; 导入java.util.Scanner;

class Main { 主类{

public static void main(String[] args) {
    Scanner run = new Scanner(System.in);
    String per = ".";
    int firstInput = 0;
    int secondInput = 0;
    int thirdInput = 0;
    int fourthInput = 0;
    boolean firstMeetsParameters = true;
    boolean secondMeetsParameters = true;
    boolean thirdMeetsParameters = true;
    boolean fourthMeetsParameters = true;
    int[] inputs = new int[] {firstInput,secondInput,thirdInput,fourthInput};
    boolean[] condition = new boolean[] {firstMeetsParameters,secondMeetsParameters,thirdMeetsParameters,fourthMeetsParameters};
    String[] num = new String[] {"first", "second", "third", "fourth"};
    for(int x = 0; x < inputs.length; x++) {
        System.out.println("Please enter the " + num[x] + " octet:");
        inputs[x] = run.nextInt();
        if(inputs[x] < 0 || inputs[x] > 255) {
            condition[x] = false;
        }
    }
    for(int i = 0; i < inputs.length; i++){
        if(condition[i] == false) {
            System.out.println("Octet " + (i+1) + " is incorrect.");
        }        
    }
    System.out.println("IP Address: " + firstInput + per + secondInput + per + thirdInput + per + fourthInput);    
}

} }

run this code and be enlightened: 运行以下代码并得到启发:

int x = 0;
int[] a = new int[] {x};
x = 1;
System.out.println(a[0]); // What do you think.. does this print 0 or 1?
a[0] = 2;
System.out.println(x); // What do you think.. does this print 1 or 2?

Once you understand that new int[] {x}; 一旦您了解了新的int [] {x}; does not 'link' x and the first slot of array a together, and that new int[] {x}; 不会将x和数组a的第一个插槽“链接”在一起,以及新的int [] {x}; in the above is no different than new int[] {0}, you should be able to figure out why it's not working: You're assigning the user inputs into the 4 slots of the inputs[] array, and you never touch the firstInput variable at all, until you print it, at which point it is obviously still what it was when you created it: 0. 上面的代码与new int [] {0}没什么不同,您应该能够弄清楚它为什么不起作用:您正在将用户输入分配到inputs []数组的4个插槽中,并且永远不要触摸完全是firstInput变量,直到您打印出来为止,这时它显然仍然是创建时的样子:0。

You never set the values of firstInput, secondInput etc. 您永远不会设置firstInput,secondInput等的值。

If you change your output statement to the following it works. 如果将输出语句更改为以下内容,则它可以工作。

System.out.println("IP Address: " + inputs[0] + per + inputs[1] + per + inputs[2] + per + inputs[3]);

The problem is on variable reference. 问题出在可变参考上。 You are setting the values at the inputs array, but print variables firstInput, secondInput... As they are native types there's no reference between them. 您正在输入数组中设置值,但是打印变量firstInput,secondInput...。因为它们是本机类型,所以它们之间没有引用。 You should print using the inputs array like this: 您应该使用输入数组进行打印,如下所示:

System.out.println("IP Address: " + inputs[0] + per + inputs[1] + per + inputs[2] + per + inputs[3]);

The value set on inputs[0] does not change the value on firstInput variable. 输入[0]上设置的值不会更改firstInput变量上的值。

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

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