これが以前に尋ねられた場合は、質問欄にリンクするか、なんとなく私の方向に送信してください。私はこのサイトで似たような質問をしてみましたが、まだ見つけていませんでした。whileループでswitch文を使用し、意図した通りに正しくループしない
私は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:文字列インデックスが範囲外にあります。 これをどのように修正すればよいのでしょうか?これがなぜ発生しているのか、非常に感謝します。
編集:コードのすべて
秒編集投稿は:誤った情報を修正/より多くを与えました。
あなたはエラーが発生する直前に何を入力していますか? –
別の問題:while(!input.equals( "x")) 'はwhile(!operation.equals(" x "))' –
でなければなりません。入力とスタックトレースを投稿できますか?何も入力しないとエラーになりますが、それ以外の場合はうまくいくはずです。 – Zarwan