Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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 初学者做while循环错误_Java_Loops_While Loop - Fatal编程技术网

Java 初学者做while循环错误

Java 初学者做while循环错误,java,loops,while-loop,Java,Loops,While Loop,我不明白为什么我的代码的最后一行抛出错误“cont无法解析为变量”,基本上我需要在循环中将华氏温度转换为摄氏温度,以防他们想要转换多个温度 我还需要让程序要求他们重新输入温度类型,如果他们说的不是c或f,而没有要求他们重新输入数字 这是我的两个问题,这是我的代码,谢谢 //Jonathan Towell //This program will convert temperatures from celsius to Fahrenheit import javax.swing.JOptionPa

我不明白为什么我的代码的最后一行抛出错误“cont无法解析为变量”,基本上我需要在循环中将华氏温度转换为摄氏温度,以防他们想要转换多个温度

我还需要让程序要求他们重新输入温度类型,如果他们说的不是c或f,而没有要求他们重新输入数字

这是我的两个问题,这是我的代码,谢谢

 //Jonathan Towell
//This program will convert temperatures from celsius to Fahrenheit
import javax.swing.JOptionPane;
public class FarenheitOrCelsius
{

    public static void main(String[] args) 
{
do
{
 double temp = Double.parseDouble(JOptionPane.showInputDialog("Enter the temperature to be converted"));
 String ver = JOptionPane.showInputDialog("Is that temperature in celsius or fahreneheit?\n" + "Enter C or F");

 if (ver.toLowerCase().contains("c")) //If C is entered convert to fahrenheit
 {
     double result = Math.round(((9 * (temp)/5) + 32));
     JOptionPane.showMessageDialog(null, "You entered " + temp + " degrees celsius.\n" + "That temperature in fahrenheit is " + result);
     int cont = Integer.parseInt(JOptionPane.showInputDialog("Do you want to convert another temperature?", JOptionPane.YES_NO_OPTION));
 }
 else if (ver.toLowerCase().contains("f")) //If f is entered covert to celsius
 {
     double result =  Math.round((5 *(temp - 32)/9)); //Math equation for coversion from F to C
     JOptionPane.showMessageDialog(null,  "You entered " + temp + " degrees fahrenheit.\n" + "That temperature in celsius is " + result);
     int cont = Integer.parseInt(JOptionPane.showInputDialog("Do you want to convert another temperatuer?", JOptionPane.YES_NO_OPTION));
 }
 else JOptionPane.showMessageDialog(null, "Invalid Option.\n Only enter C or F.");
 {
     System.exit(0);
 } 

} while (cont = JOptionPane.YES_OPTION);
}
}

cont
是在内部作用域中声明的,因此
while
循环不可用。您需要在
do
语句之前声明它:

int cont = JOptionPane.NO_OPTION;
do {
  ...
  cont = Integer.parseInt( ...

  ...
  cont = Integer.parseInt( ...
} while (cont == JOptionPane.YES_OPTION);
注意,您还需要使用double
=
测试中进行比较。

就像局部方法变量仅在该方法中可用一样,在一对花括号内声明的变量的范围仅限于该代码块。但是,在块外部声明的变量在块内部和外部都可用

因此,在下面的代码中

do {
   int localToDoBlock = 1;
   ...
   localToDoBlock++;
} while (localToDoBlock < 10) ; // ERROR!
编译器不再抱怨,因为
outsideTheDoBlock
在循环内外都可用。

查找变量范围。
while(cont=JOptionPane.YES\u选项)看看那个操作员。
int outsideTheDoBlock = 1;
do {
       outsideTheDoBlock++;
} while (outsideTheDoBlock < 10) ; // Works now