尽管 Java 中没有错误,但在输入 Char 之前代码崩溃
Code crashing before enter Char despite no errors at the side in Java
我正在为一项作业编写代码,要求我将华氏温度转换为摄氏温度,反之亦然,并将它们存储在数组中。在完成它并对其显示在侧面的错误进行排序之后,它在到达循环结束后仍然设法崩溃。我已经尝试做一些我在网上看到的事情,但到目前为止似乎没有任何效果。我正在使用 NetBeans IDE 8.0.2
我得到的错误是:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0
完整代码为:
package assignment.pkg1;
import java.util.Scanner;
public class Assignment1 {
public static void main(String[] args)
{
controller();
}
//----------------------------controller()--------------------------------------
public static void controller()
{
Scanner kb = new Scanner(System.in);
double num[]=new double[10], cel, fah, tem;
char ch;
for(int x=0; x<10; x++) //This loops ten times to get ten tempretures
{
System.out.print("Please enter your tempreture :");
num[x]=kb.nextDouble();
}
System.out.println();
System.out.println("Is the data you are entering in Fahrenheit or Celcius?");
System.out.println("Please enter C for Celcius or F for Fahrenheit : ");
ch = kb.nextLine().charAt(0);
if (ch !='C' || ch !='c')
{
for(int x =0;x<10;x++)
{
fah=ctof(num[x]);
System.out.println(num[x]+" degrees C = "+fah+" degrees F");
}
}
if (ch =='F' || ch =='f')
{
for(int x =0;x<10;x++)
{
cel=ftoc(num[x]);
System.out.println(num[x]+" degrees F = "+cel+" degrees C");
}
}
}
//----------------------------ctog()--------------------------------------------
public static double ctof(double cel)
{
double tem;
//fah = cel / 5 + 32;
tem = (cel / 5) + 32;
return tem;
}
//----------------------------ftoc()--------------------------------------------
public static double ftoc(double fah)
{
double tem;
//cel = (fah - 32)/9 * 5
tem = (fah - 32) /9 * 5;
return tem;
}
}
在调用 .nextDouble();
后在 for 循环中添加 kb.nextLine();
,因为 nextDouble() 不使用换行符 (\n)。
如果不这样做,您将对没有第一个索引的空字符串执行 charAt(0) \ first index
。
这个问题是由于kb.nextDouble();
没有使用换行符。要解决此问题,您可以在 ch = kb.nextLine().charAt(0);
之前调用 kb.nextLine();
我正在为一项作业编写代码,要求我将华氏温度转换为摄氏温度,反之亦然,并将它们存储在数组中。在完成它并对其显示在侧面的错误进行排序之后,它在到达循环结束后仍然设法崩溃。我已经尝试做一些我在网上看到的事情,但到目前为止似乎没有任何效果。我正在使用 NetBeans IDE 8.0.2
我得到的错误是:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0
完整代码为:
package assignment.pkg1;
import java.util.Scanner;
public class Assignment1 {
public static void main(String[] args)
{
controller();
}
//----------------------------controller()--------------------------------------
public static void controller()
{
Scanner kb = new Scanner(System.in);
double num[]=new double[10], cel, fah, tem;
char ch;
for(int x=0; x<10; x++) //This loops ten times to get ten tempretures
{
System.out.print("Please enter your tempreture :");
num[x]=kb.nextDouble();
}
System.out.println();
System.out.println("Is the data you are entering in Fahrenheit or Celcius?");
System.out.println("Please enter C for Celcius or F for Fahrenheit : ");
ch = kb.nextLine().charAt(0);
if (ch !='C' || ch !='c')
{
for(int x =0;x<10;x++)
{
fah=ctof(num[x]);
System.out.println(num[x]+" degrees C = "+fah+" degrees F");
}
}
if (ch =='F' || ch =='f')
{
for(int x =0;x<10;x++)
{
cel=ftoc(num[x]);
System.out.println(num[x]+" degrees F = "+cel+" degrees C");
}
}
}
//----------------------------ctog()--------------------------------------------
public static double ctof(double cel)
{
double tem;
//fah = cel / 5 + 32;
tem = (cel / 5) + 32;
return tem;
}
//----------------------------ftoc()--------------------------------------------
public static double ftoc(double fah)
{
double tem;
//cel = (fah - 32)/9 * 5
tem = (fah - 32) /9 * 5;
return tem;
}
}
在调用 .nextDouble();
后在 for 循环中添加 kb.nextLine();
,因为 nextDouble() 不使用换行符 (\n)。
如果不这样做,您将对没有第一个索引的空字符串执行 charAt(0) \ first index
。
这个问题是由于kb.nextDouble();
没有使用换行符。要解决此问题,您可以在 ch = kb.nextLine().charAt(0);
kb.nextLine();