Java 如何定义此变量以使其不';你没有读到错误吗?

Java 如何定义此变量以使其不';你没有读到错误吗?,java,Java,前几天开始学习java,我可能只是错误地定义了变量。在下面的脚本中,变量x在编译过程中显示“未找到符号错误”。我尝试过更改变量名,在代码的开头定义它,并检查所有大括号。谁能告诉我怎么了 守则: public class covidSim { static void main(String[] args) { int currentDay=0; int currentCases=1; int targetDay=100; wh

前几天开始学习java,我可能只是错误地定义了变量。在下面的脚本中,变量x在编译过程中显示“未找到符号错误”。我尝试过更改变量名,在代码的开头定义它,并检查所有大括号。谁能告诉我怎么了

守则:

public class covidSim {
    static void main(String[] args) {
        int currentDay=0;
        int currentCases=1;
        int targetDay=100;
        while(currentDay < targetDay) {
            int exeNum = currentCases;
            System.out.println("Day|");
            while(exeNum > 0) {
                int randomSpread = (int)(Math.random() * 9);
                if (randomSpread > 0 && randomSpread < 5) {
                    int x = 0;
                }
                if (randomSpread > 4) {
                    int x = (int)(Math.random() * 5);
                }
                exeNum--; 
                currentCases = currentCases + x;
            }
            System.out.println(currentCases);
            System.out.println(currentDay);
            currentDay++;
        }
    }
}
公共类covidSim{
静态void main(字符串[]参数){
int currentDay=0;
int currentCases=1;
int targetDay=100;
而(当前日<目标日){
int exeNum=当前案例;
System.out.println(“日”);
while(exeNum>0){
int randomSpread=(int)(Math.random()*9);
if(randomSpread>0&&randomSpread<5){
int x=0;
}
如果(随机排列>4){
int x=(int)(Math.random()*5);
}
埃克塞努姆--;
currentCases=currentCases+x;
}
系统输出打印项次(当前案例);
系统输出打印项次(当前日期);
currentDay++;
}
}
}

您只需要从两个
if
语句中提取
x
的声明,并将其放在它们上面,以便为引用它的所有位置定义它。下面是代码位的外观:

int x = 0;
int randomSpread = (int)(Math.random() * 9);
if (randomSpread > 0 && randomSpread < 5) {
    x = 0; }
if (randomSpread > 4) {
    x = (int)(Math.random() * 5); }
exeNum--;
currentCases = currentCases + x; }
intx=0;
int randomSpread=(int)(Math.random()*9);
if(randomSpread>0&&randomSpread<5){
x=0;}
如果(随机排列>4){
x=(int)(Math.random()*5);}
埃克塞努姆--;
currentCases=currentCases+x;}

您只需要从两个
if
语句中提取
x
的声明,并将其放在它们上面,以便为引用它的所有位置定义它。下面是代码位的外观:

int x = 0;
int randomSpread = (int)(Math.random() * 9);
if (randomSpread > 0 && randomSpread < 5) {
    x = 0; }
if (randomSpread > 4) {
    x = (int)(Math.random() * 5); }
exeNum--;
currentCases = currentCases + x; }
intx=0;
int randomSpread=(int)(Math.random()*9);
if(randomSpread>0&&randomSpread<5){
x=0;}
如果(随机排列>4){
x=(int)(Math.random()*5);}
埃克塞努姆--;
currentCases=currentCases+x;}

您正在if语句内部定义
x
,然后尝试在外部访问它。在第一条if语句上方定义
x
。只能使用定义范围内的变量

while(exeNum > 0) {
   int randomSpread = (int)(Math.random() * 9);
   int x = 0;
   if (randomSpread > 0 && randomSpread < 5) {
       x = 0;
   }
   if (randomSpread > 4) {
       x = (int)(Math.random() * 5);
   }
   exeNum--; 
   currentCases = currentCases + x;
}
while(exeNum>0){
int randomSpread=(int)(Math.random()*9);
int x=0;
if(randomSpread>0&&randomSpread<5){
x=0;
}
如果(随机排列>4){
x=(int)(Math.random()*5);
}
埃克塞努姆--;
currentCases=currentCases+x;
}

您正在if语句内部定义
x
,然后尝试在外部访问它。在第一条if语句上方定义
x
。只能使用定义范围内的变量

while(exeNum > 0) {
   int randomSpread = (int)(Math.random() * 9);
   int x = 0;
   if (randomSpread > 0 && randomSpread < 5) {
       x = 0;
   }
   if (randomSpread > 4) {
       x = (int)(Math.random() * 5);
   }
   exeNum--; 
   currentCases = currentCases + x;
}
while(exeNum>0){
int randomSpread=(int)(Math.random()*9);
int x=0;
if(randomSpread>0&&randomSpread<5){
x=0;
}
如果(随机排列>4){
x=(int)(Math.random()*5);
}
埃克塞努姆--;
currentCases=currentCases+x;
}
您可以使用以下代码:

int exeNum = currentCases;
while(exeNum > 0) {
  int randomSpread = (int)(Math.random() * 9);
  int x = 0;
  if (randomSpread > 4) {
    x = (int)(Math.random() * 5); 
  }
  exeNum--; 
  currentCases = currentCases + x; 
}
您不再需要指定此条件,因为x仅在
randomSpread>4
时更改为不同的值:

if (randomSpread > 0 && randomSpread < 5) {
    int x = 0; }
if(randomSpread>0&&randomSpread<5){
int x=0;}
您可以使用以下代码:

int exeNum = currentCases;
while(exeNum > 0) {
  int randomSpread = (int)(Math.random() * 9);
  int x = 0;
  if (randomSpread > 4) {
    x = (int)(Math.random() * 5); 
  }
  exeNum--; 
  currentCases = currentCases + x; 
}
您不再需要指定此条件,因为x仅在
randomSpread>4
时更改为不同的值:

if (randomSpread > 0 && randomSpread < 5) {
    int x = 0; }
if(randomSpread>0&&randomSpread<5){
int x=0;}

与您的问题无关,但您应该坚持使用更常见的缩进和大括号定位样式(例如,请参见我如何修改您的代码)。对于你的问题,你应该在那些
if
s之前定义
intx
,这样之后就可以访问它,只需执行
x=0
x=(int)(Math.random()*5)
x
只在那些if语句中定义。从纯逻辑的角度考虑这个问题,它与语法无关。如果这两个If语句的计算结果都不是true,那么
x
的值应该是什么?与您的问题无关,但您应该坚持使用更常见的缩进和大括号定位样式(例如,请参见我如何修改您的代码)。对于你的问题,你应该在那些
if
s之前定义
intx
,这样之后就可以访问它,只需执行
x=0
x=(int)(Math.random()*5)
x
只在那些if语句中定义。从纯逻辑的角度考虑这个问题,它与语法无关。如果这两条If语句的计算结果都不是true,那么
x
的值应该是多少?