2016-10-07 5 views
-1

これが以前に尋ねられた場合は、質問欄にリンクするか、なんとなく私の方向に送信してください。私はこのサイトで似たような質問をしてみましたが、まだ見つけていませんでした。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:文字列インデックスが範囲外にあります。 これをどのように修正すればよいのでしょうか?これがなぜ発生しているのか、非常に感謝します。

編集:コードのすべて

秒編集投稿は:誤った情報を修正/より多くを与えました。

+0

あなたはエラーが発生する直前に何を入力していますか? –

+0

別の問題:while(!input.equals( "x")) 'はwhile(!operation.equals(" x "))' –

+0

でなければなりません。入力とスタックトレースを投稿できますか?何も入力しないとエラーになりますが、それ以外の場合はうまくいくはずです。 – Zarwan

答えて

0

while条件で小文字の「x」があります:

while (!input.equals("x")) 

したがってループは本当に理由は、「X」の入力を終了したことがないし、入力の終わりに、それは例外になり、空の文字列を読み込み、空のStringから最初の文字を読み込みます。

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; 
    } 
} 
0

さて、あなたのコードは私のために正常に動作します:

一般

、コードを単純化するために、それは無限forループを使用することをお勧めします。 は、私はあなたがラインSystem.out.println("Enter a function: ");

この
String s="";

ので、

input.charAt(0); 

のような長さ= 0の文字列、何かを生成するEnterキーを押した後、ENTERキーを押していると思うだろうaを作りなさいStringIndexOutOfBoundsException

0

何の@Adit A.の建物あなたはその後、前にあなたのスイッチを入力し、有効な入力のためにチェックすることができ

 default: 
     System.out.println("Invalid input!"); 
     break; 

 if (!input.equals("")) 
    { 
     operation = input.charAt(0); 
    } 

また、あなたが移動する場合Pillaiさんは、あなたのスイッチにデフォルトを追加することを検討することをお勧めします、のようなものを述べましたあなたの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ループを制御するブールのフラグを追加し、私は「X」の場合にはオフに切り替えboolean repeat = true;

 case 'X': 
     System.out.println("Thanks for using this calculator. "); 
     repeat = false; 
     break; 

が、これは、コーディングを維持、お役に立てば幸い!

+0

ああ、デフォルトのケースを使用したり、有効な入力をチェックしたりすることは考えていませんでした:o私の問題を引き続き解決します。私はちょうどあなたが与えてくれた追加情報で今はそれを残しておくと思います。Andlee c: –

+0

@ChiefTwoPencilsは現在、それを見て、その質問のリンクを投稿してくれてありがとう –

0

私は、あなたが何を求めているのか、何をしようとしているのか明確ではありません。しかし、その代わりに、

String input = in.nextLine(); 
char operation = input.charAt(0); 

使用これを使用するのではなく、

char operation=in.next().charAt(0); 
関連する問題