如何在if-else语句中重写java中的variabe值

如何在if-else语句中重写java中的variabe值,java,eclipse,Java,Eclipse,嘿,伙计们, 简单的代码。我刚在java中输入,我想在else语句中再次显示输入消息并覆盖num1变量。但我面临的消息是“复制局部变量字符串a和num1”。请建议最佳解决方案。 这是我的密码 String a =JOptionPane.showInputDialog("Please enter the first Number :"); int num1=Integer.parseInt(a); int con1=JOptionPane.showConfirmDialog(null

嘿,伙计们, 简单的代码。我刚在java中输入,我想在else语句中再次显示输入消息并覆盖num1变量。但我面临的消息是“复制局部变量字符串a和num1”。请建议最佳解决方案。 这是我的密码

String a =JOptionPane.showInputDialog("Please enter  the first Number :");      
int num1=Integer.parseInt(a);
int con1=JOptionPane.showConfirmDialog(null,num1);
if(con1==JOptionPane.YES_OPTION)
 {
JOptionPane.showMessageDialog(null,num1);
}
else{

String a=JOptionPane.showInputDialog("Pleaee enter  the first Number :");   
int num1=Integer.parseInt(w);

}

您在代码中声明了两次变量“a”和num1, 您可以创建具有不同名称的变量,也可以尝试下面的代码来修复错误

String a =JOptionPane.showInputDialog("Please enter  the first Number :");      
int num1=Integer.parseInt(a);
int con1=JOptionPane.showConfirmDialog(null,num1);
  if(con1==JOptionPane.YES_OPTION)
   {
    JOptionPane.showMessageDialog(null,num1);
   }
  else{

    a=JOptionPane.showInputDialog("Pleaee enter  the first Number :");   
    num1=Integer.parseInt(w);
  }

由于{if/else}是条件语句,因此不能使用相同的变量名添加重复的值。因此,可以尝试删除重复的声明,如下所示:

String a =JOptionPane.showInputDialog("Please enter  the first Number :");      
int num1=Integer.parseInt(a);
int con1=JOptionPane.showConfirmDialog(null,num1);
if(con1==JOptionPane.YES_OPTION)
 {
JOptionPane.showMessageDialog(null,num1);
}
else{

 a=JOptionPane.showInputDialog("Pleaee enter  the first Number :");   
 num1=Integer.parseInt(w);

}

只需删除else块中的单词String和int