如果之前已经询问过,请将问题链接或以某种方式发送给我。我试着在这个网站上搜索类似的问题,但还没找到。
我正在为我的Java lvl 1在线课做一个迟到的作业(是的,我是Java的新手),我认为我的while循环和switch语句很好但是我无法弄清楚如何再循环它。循环应该回到要求用户输入。在用户可以输入下一个功能之前,这发生在控制台输出中:
This calcuclator requires you to enter a function and a number.
The functions are as follows:
S - Sine
C - Cosine
T - Tangent
R - Square Root
N - Natural Log
X - Exit the program
Enter a function:
s
Enter your value
40
The sine of your number is : 0.7451131604793488
S - Sine
C - Cosine
T - Tangent
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
R - Square Root
N - Natural Log
X - Exit the program
Enter a function
at java.lang.String.charAt(String.java:658)
at assigment9.Assigment9.main(Assigment9.java:110)
C:\Users\r3ds1\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 5 seconds)
这是我的代码示例:
import java.util.Scanner;
//whileSwitch
public class Assigment9 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("This calcuclator requires you to enter a function and a number.");
System.out.println("The functions are as follows: ");
//options
System.out.println("S - Sine");
System.out.println("C - Cosine");
System.out.println("T - Tangent");
System.out.println("R - Square Root");
System.out.println("N - Natural Log");
System.out.println("X - Exit the program");
//user input
System.out.println("Enter a function: ");
String input = in.nextLine();
char operation = input.charAt(0);
//supposed to stop when user inputs 'x'
while(!input.equals("x"))
{
switch(Character.toUpperCase(operation))
{
//Sine
case 'S':
System.out.println("Enter your value ");
double s;
double theSine;
s = in.nextDouble();
theSine = Math.sin(s);
System.out.println("The sine of your number is : " + theSine );
break;
//Cosine
case 'C':
System.out.println("Enter your value ");
double c;
c = in.nextDouble();
double theCosine;
theCosine = Math.cos(c);
System.out.println("The Cosine of your number is : " + theCosine );
break;
//tangent
case 'T':
System.out.println("Enter your value ");
double t;
t = in.nextDouble();
double theTangent;
theTangent = Math.cos(t);
System.out.println("The Tangent of your number is : " + theTangent );
break;
//Square root
case 'R':
System.out.println("Enter your value ");
double r;
r = in.nextDouble();
double theSqrt;
theSqrt = Math.cos(r);
System.out.println("The Square Root of your number is : " + theSqrt );
break;
//Natural Log
case 'N':
System.out.println("Enter your value ");
double n;
n=in.nextDouble();
double theLog;
theLog = Math.cos(n);
System.out.println("The Natural Log of your number is : " + theLog );
break;
//Exit
case 'X':
System.out.println("Thanks for using this calculator. ");
break;
}
//options
System.out.println("S - Sine");
System.out.println("C - Cosine");
System.out.println("T - Tangent");
System.out.println("R - Square Root");
System.out.println("N - Natural Log");
System.out.println("X - Exit the program");
System.out.println("Enter a function");
input = in.nextLine();
operation = input.charAt(0);
}
}
}`
因此,在用户进入下一个函数之前,程序会吐出一个StringIndexOutOfBoundsException:String index超出范围:0。 我将如何解决这个问题,以及对其发生原因的解释将非常感激!
编辑:发布所有代码
第二次编辑:纠正错误信息/给予更多。
答案 0 :(得分:0)
在while条件中有小写“x”:
while (!input.equals("x"))
因此,循环永远不会因为“X”输入而退出,并且在输入结束时它会读取空字符串,这会在从空字符串读取第一个字符时导致异常。
通常,为了简化代码,最好使用无限for
循环:
LOOP: for (;;) {
String input = in.nextLine();
if (input.isEmpty()) {
if (!in.hasNextLine())
break;
continue;
}
switch (input.characterAt(0)) {
case 'S': ...
...
case 'x':
case 'X':
break LOOP;
}
}
答案 1 :(得分:0)
那么你的代码对我来说很好。
我认为您在行System.out.println("Enter a function: ");
按ENTER键将生成一个长度为0的字符串,类似于此
String s="";
因此
input.charAt(0);
会产生StringIndexOutOfBoundsException
答案 2 :(得分:0)
基于@Adit A. Pillai提到的内容,您可能需要考虑在交换机中添加默认值,例如:
default:
System.out.println("Invalid input!");
break;
然后,您可以在进入交换机之前检查有效输入:
if (!input.equals(""))
{
operation = input.charAt(0);
}
此外,如果您将菜单和输入代码移动到while循环的开头,则可以避免重复这些行:
boolean repeat = true;
while(repeat)
{
System.out.println("This calcuclator requires you to enter a function and a number.");
System.out.println("The functions are as follows: ");
//options
System.out.println("S - Sine");
System.out.println("C - Cosine");
System.out.println("T - Tangent");
System.out.println("R - Square Root");
System.out.println("N - Natural Log");
System.out.println("X - Exit the program");
//user input
System.out.println("Enter a function: ");
String input = in.nextLine();
...
}
注意:在测试你的代码时,我添加了一个布尔标志来控制你的while循环,boolean repeat = true;
我在“X”的情况下切换了:
case 'X':
System.out.println("Thanks for using this calculator. ");
repeat = false;
break;
希望这有帮助,保持编码!
答案 3 :(得分:0)
我不清楚你究竟在问什么以及你想做什么。但是,而不是使用,
String input = in.nextLine();
char operation = input.charAt(0);
使用此,
char operation=in.next().charAt(0);