2017-12-04 33 views
0

"END"がコンソールに入力されたときに私のコードを終了しようとしていますが、私のコードは公平に動かず、どこが間違っているのか分からないようです。しかし、これは主に私がそれを理解できない理由と主に関係しています。できるなら助けてください。私はENDを入力すると私のプログラムが終了しないのですか?

import java.util.*; 
class Main 
{ 
public static void main(String args[]) 
{ 
    int j = 0; 
    while (j++ <= 3) {  
     // Create Scanner object to take input from command prompt 
     Scanner s=new Scanner(System.in); 
     // Take input from the user and store it in st 
     String st = ""; 
     while (!st.equals("END")) { 
      { 
       st=s.nextLine(); 
       // Initialize the variable count to 0 
       int count=0; 
       // Convert String st to char array 
       char[] c=st.toCharArray(); 
       // Loop till end of string 
       for(int i=0;i<st.length();i++) 

       // If character at index 'i' is not a space, then increment count 
        if(c[i]!=' ') count++; 
       // Print no.of spaces 
       System.out.printf("[%4d] spaces in ", +(st.length()-count)); 
       // Print no.of spaces 
       System.out.println('"' + st + '"'); 

      } 
      j++; 
     } 
    } 
} 
+0

まだデバッグを学んでいない場合は、この資料が役立ちます。[小さなプログラムをデバッグする方法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs /) – EJoshuaS

+0

[デバッガとは何ですか?また、どのようにして問題を診断するのに役立ちますか?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-) it-help-me-diagnose-problems) – EJoshuaS

+0

あなたはそれがないと思いますか? –

答えて

3

あなたが「END」を2回入力した場合は終了しますwhile (j++ <= 3) { ... }あなたのプログラムを持っているので。

+0

ああ、私はコードを3回ループすると仮定しているだけです。 – Jonny

0

2つのwhileループがネストされているためです。 2番目のwhileループに "END"を入力した後、2番目のループが終了します。しかし、表示されているように、終了後、最初のループはwhile (j++ <= 3) {で始まり、このループではユーザからの入力を3回待ってからScanner s=new Scanner(System.in);に対応します。したがって、プログラムが終了しないのは正常です。入力後にプログラムを終了させたい場合は、System.exit(-1);コマンドを使用します。私はあなたのコメントに応じてコードを追加しました。

import java.util.*; 

class Main { 

public static void main(String args[]) { 
int j = 0; 
while (j++ <= 3) { 
    // Create Scanner object to take input from command prompt 
    Scanner s = new Scanner(System.in); 
    // Take input from the user and store it in st 
    String st = s.nextLine(); 
    if (!st.equals("END")) { 
    // Initialize the variable count to 0 
    int count = 0; 
    // Convert String st to char array 
    char[] c = st.toCharArray(); 
    // Loop till end of string 
    for (int i = 0; i < st.length(); i++) 

    // If character at index 'i' is not a space, then increment count 
    { 
     if (c[i] != ' ') { 
     count++; 
     } 
    } 
    // Print no.of spaces 
    System.out.printf("[%4d] spaces in ", +(st.length() - count)); 
    // Print no.of spaces 
    System.out.println('"' + st + '"'); 
    } else { 
    System.exit(-1); 
    } 
} 
} 
} 
+0

彼はまた余分な 'j ++'を持っているので、2度しかないと思います。 – EJoshuaS

+0

ええ、あなたは正しいです –

+0

こんにちは、あなたはどこで、私はSystem.exit(-1)を統合する方法を知っていますか?コマンド。 「END」と入力すると – Jonny

0

ループを使用せずにif条件を追加するだけです。

public static void main(String args[]) { 
    int j = 0; 
    while (j++ <= 3) { 
     // Create Scanner object to take input from command prompt 
     Scanner s = new Scanner(System.in); 
     // Take input from the user and store it in st 
     String st = ""; 

     st = s.nextLine(); 

     if (st.equalsIgnoreCase("END")) { 
      j = 5; 
     } 
     // Initialize the variable count to 0 
     int count = 0; 
     // Convert String st to char array 
     char[] c = st.toCharArray(); 
     // Loop till end of string 
     for (int i = 0; i < st.length(); i++) // If character at index 'i' is not a space, then increment count 
     { 
      if (c[i] != ' ') { 
       count++; 
      } 
     } 
     // Print no.of spaces 
     System.out.printf("[%4d] spaces in ", +(st.length() - count)); 
     // Print no.of spaces 
     System.out.println('"' + st + '"'); 
     j++; 
    } 
} 

私は基本的に入力の終了を得ると、最初のループが終了するようにjの値を5に変更します。インプを止めてください。

このタイプのソリューションが必要なのか、入力を3回取る必要がありますか?

関連する問題