Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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 如何在连续的for循环之间传递变量值?_Java_Variables_For Loop - Fatal编程技术网

Java 如何在连续的for循环之间传递变量值?

Java 如何在连续的for循环之间传递变量值?,java,variables,for-loop,Java,Variables,For Loop,我有两个连续的for循环,我需要将其中一个变量的值传递给另一个for循环内的实例 for(int x=0; x< sentence.length(); x++) { int i; if (!Character.isWhitespace(sentence.charAt(x))) i = x ; break; } for (int i ; i < sentence.length(); i++) { if (Character.isWh

我有两个连续的for循环,我需要将其中一个变量的值传递给另一个for循环内的实例

for(int x=0; x< sentence.length(); x++)  {

  int i;
  if (!Character.isWhitespace(sentence.charAt(x)))
      i = x ;
      break;    
}

for (int  i  ; i < sentence.length(); i++) {
  if (Character.isWhitespace(sentence.charAt(i)))
     if (!Character.isWhitespace(sentence.charAt(i + 1)))
}
for(int x=0;x

这只是我程序的一部分,我的目的是将x的值(从fírst for循环)分配给i变量(从第二个for循环),这样我就不会从0开始,而是从x的值开始(在断开第一个for循环之前)。

您需要了解Java块范围:

在for循环之外声明变量,如下所示

// Declare what you want to access outside here.
...
for(int x = 0; x< sentence.length(); x++)  {
//在此处声明要在外部访问的内容。
...
对于(int x=0;x
它看起来像Java,是吗

您必须在循环块外声明“i”变量。顺便说一句,如果“i”不是一个循环计数器,则最好为该变量指定一个有意义的名称(x与循环计数器无关)

另外,您可能有一个bug,因为中断超出了条件表达式块(第一个循环)

int currentCharPosition=0;//为变量指定一个完整的名称(保留i作为循环计数器)
for(int i=0;i<句子长度();i++){
如果(!Character.isWhitespace(句子.charAt(x)){
currentCharPosition=x;
break;//将中断放在if块中
}
}
while(currentCharPosition<句子长度()){
...
currentCharPosition++;
}
intx;
对于(x=0;x
int-sentenceLength=句子.length();
int[]firstLoopData=newint[sentenceLength-1];
for(int x=0,index=0;x
i是第一个循环的本地变量,无法在第二个for循环内使其在本地可访问。为什么不尝试使用全局变量(在第一个for循环外),并在中断第一个for循环之前更新变量的值。然后,您可以在第二个for循环中访问相同的值。为什么不使用
数组
而不是
int
,在该数组中,您可以添加第一个循环中的所有值,并在第二个循环中使用该数组变量!
int currentCharPosition = 0; //give a maningful name to your variable (keep i for loop counter)

for(int i=0; i< sentence.length(); i++) {

            if (!Character.isWhitespace(sentence.charAt(x))){  
                currentCharPosition  = x ;
                break;  //put the break in the if block
            }

}

while( currentCharPosition < sentence.length()) {
            ...
            currentCharPosition++;
}
int x;
for(x = 0; x < sentence.length; x++)
   if(!Character.isWhitespace(sentence.charAt(x)))
      break;

for(int i = x; i < //And so on and so fourth
int sentenceLength = sentence.length();
int[] firstLoopData = new int[sentenceLength -1];
for(int x=0, index=0; x < sentenceLength; x++)  {
    if (!Character.isWhitespace(sentence.charAt(x))){
        firstLoopData[index] = x;
        index++; 
        break; 
    }   
}

for(int tempInt: firstLoopData){
    //your code...
}