我试图用Java编写一个程序,该程序将接受用户输入并转换为摄氏或华氏。因此用户输入一些数字,一个空格,然后是C或F.祢程序编译罚款,但是当我尝试测试它,我得到以下信息:MSDOS窗口中的Java程序错误:线程“main”中的异常java.lang.NumberFormatException
Exception in thread "main" java.lang.NumberFormatException: For input string: (whatever number, space, F/C I put in to test it0
at java.lang.Integer.parseInt<Integer.java:492>
at java.lang.Integer.parseInt<Integer.java:527>
at Temp.Conv.main<TempConv.java:14>
我猜的Java不像我试图使用Parse搜索字符串中的整数。 有关如何完成它的任何建议?
下面是代码:(只要你知道,我知道的支架和空间是关闭的,但这网站不会让我来解决它)
public class TempConv
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String reply = "y";
while(reply.equalsIgnoreCase("y"))
{
System.out.println("Enter temp (ex. 70 F or 23 C): ");
String CF = input.nextLine(); // inputs string
int temp = Integer.parseInt(CF); // to get integers from string
for (int i = 0; i < CF.length(); ++i)
{
char aChar = CF.charAt(i);
if (aChar == 'F') // looking in string for F
// go to f2c()
{
f2c(temp);
}
else if (aChar == 'C') // looking for C
// go to c2f()
{
c2f(temp);
}
}
System.out.println("Would you like to covert another temp? <y/n> ");
reply = input.next();
}
}
static void f2c(int j)
{
int c = (j - 32)*(5/9);
System.out.println(j + "F = " + c + "C");
}
static void c2f(int k)
{
int f = (k*(5/9))+32;
System.out.println(k + "C = " + f + "F");
}
}