Java While使用switch语句循环,但未按预期正确循环
如果以前有人问过这个问题,请链接问题帖子或以某种方式发送给我。我试图在这个网站上搜索一个类似的问题,但还没有找到 我正在为我的Java lvl 1在线类做一个后期作业(是的,我是Java新手),我认为我的while-loop和switch语句很好,但我很难弄清楚如何再次循环它。循环应该返回到请求用户输入。在用户可以输入下一个功能之前,控制台输出中会出现以下情况:Java While使用switch语句循环,但未按预期正确循环,java,while-loop,switch-statement,Java,While Loop,Switch Statement,如果以前有人问过这个问题,请链接问题帖子或以某种方式发送给我。我试图在这个网站上搜索一个类似的问题,但还没有找到 我正在为我的Java lvl 1在线类做一个后期作业(是的,我是Java新手),我认为我的while-loop和switch语句很好,但我很难弄清楚如何再次循环它。循环应该返回到请求用户输入。在用户可以输入下一个功能之前,控制台输出中会出现以下情况: 此计算器要求您输入函数和数字。 功能如下: S-正弦 C余弦 T切线 R平方根 自然对数 退出程序 输入一个函数: s 输入您的值 4
此计算器要求您输入函数和数字。
功能如下:
S-正弦
C余弦
T切线
R平方根
自然对数
退出程序
输入一个函数:
s
输入您的值
40
你的号码的正弦是:0.7451131604793488
S-正弦
C余弦
T切线
线程“main”java.lang.StringIndexOutOfBoundsException中出现异常:字符串索引超出范围:0
R平方根
自然对数
退出程序
输入函数
位于java.lang.String.charAt(String.java:658)
在assigment9.assigment9.main(assigment9.java:110)
C:\Users\r3ds1\AppData\Local\NetBeans\Cache\8.1\executor snippets\run.xml:53:Java返回:1
生成失败(总时间:5秒)
以下是我的代码示例:
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:字符串索引超出范围:0。
我将如何解决这个问题,并解释为什么会发生这种情况将非常感谢
编辑:发布所有代码
第二次编辑:更正错误信息/提供更多信息。while条件中有小写“x”:
while (!input.equals("x"))
因此,由于“X”输入,循环永远不会真正退出,并且在输入结束时,它读取空字符串,这导致从空字符串中读取第一个字符时出现异常
通常,为了简化代码,最好使用infinitefor
循环:
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;
}
}
你的代码对我来说很好。 我认为您正在按行
System.out.println(“输入函数:”)后的ENTER键代码>
按ENTER键将生成一个长度为0的字符串,类似这样的字符串
String s=”“代码>
因此
input.charAt(0);
会产生一个<代码> StrutixOutOfFunsExcExabor
< p> > 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;
希望这有帮助,继续编码 我不清楚你到底在问什么,想做什么。但是,不是使用,
String input = in.nextLine();
char operation = input.charAt(0);
用这个,
char operation=in.next().charAt(0);
在错误发生之前,您正在输入什么?另一个问题:while(!input.equals(“x”)
应该是while(!operation.equals(“x”))
您的代码对我来说运行良好。你能发布你的输入和堆栈跟踪吗?如果您不输入任何内容,您将得到该错误,但我认为它应该可以工作。我认为您将实际问题缩短到代码之外。你是如何得到价值的?你没有回答我的问题。第二次输入的字母是什么?你刚才按ENTER键了吗?啊,我甚至没有考虑使用默认的大小写或检查有效的输入:o尽管我的问题仍然存在,即使这里的其他人在运行我的代码时没有收到错误。我想我会把你提供的附加信息暂时放在这里,谢谢你,李c:@chieftwopinels正在查看它,谢谢你发布这个问题链接