2016-06-17 4 views
-5

私はideone.comの非常に簡単なプログラムを使って私の友人と分かち合っていますが、 「Bye!」と入力すると「ランタイムエラー」です。それは "さようなら"を返すはずでした。私に戻って。コードは次のとおりです。私の単純なJavaプロジェクトで「ランタイムエラー」 - どこから来ているのか分かりません

/* package whatever; // don't place package name! */ 

import java.util.*; 
import java.lang.*; 
import java.io.*; 

/* Name of the class has to be "Main" only if the class is public. */ 
class Main 
{ 
    public static void main (String[] args) throws java.lang.Exception 
    { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("Hi!"); 
     String nxt = scan.nextLine(); 
     while (nxt!="Bye" || nxt!= "Bye!" || nxt!="bye" || nxt!="bye!"){ 
      reply("How are you?", "I'm fine, thanks.", nxt); 
      reply("What is your favorite color?", "Light green! It is so pretty!", nxt); 
      reply("What do you like to eat?", "Oh, I don't. Computers don't eat.", nxt); 
      reply("Do you like me?", "Yes, but you're way too inquisitive!", nxt); 
      reply("What's your favorite memory?", "Um...Something about my CPU having an extremely - okay...I don't think....Um...", nxt); 
      reply("Where did you learn how to talk?", "This program.", nxt); 
      reply("So...Are you alive?", "No. I don't regulate my body temperature. \n !!!COMPUTER OVERHEATED!!!", nxt); 
      reply("Do you know any jokes?", "Why don't you program some...?", nxt); 
      nxt = scan.nextLine(); 
     } 
     System.out.println("Bye!"); 

    } 
    public static void reply (String input, String output, String compare){ 
     boolean lia = compare.contains(input); 
     if(lia == true){ 
      System.out.println(output); 
     } 
    } 
} 

誰でも見たい場合は、hereが私のコードです。

+3

これは実行時エラーではありません。 IDEONEはコンパイルエラー*であるため、 "コンパイル情報"にエラーを表示します。これは、 'else'節に' if'文がないことが原因です。 **** Main.java:25:エラー: 'else' if 'なし' *** – Andreas

+1

実際のIDEをダウンロードするだけで、本当に良い無料のものがいくつかあります。 IntelliJのように –

+0

Pythonの背景から来ていますか? Javaはしばらくの間にelse文を使用しません –

答えて

1

私はあなたが他の
を省略すべきであるあなたが

while(){ 
    //your code 
} else{ 
    //your code 
} 

を置くことができるとは思わない使用方法を参照してくださいここでdocs using while

0

これはコンパイルエラーです。あなたのwhileループでelseを削除してください。エルス

public static void main (String[] args) throws java.lang.Exception 
{ 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Hi!"); 
    String nxt = scan.nextLine(); 
    while (nxt!="Bye" || nxt!= "Bye!" || nxt!="bye" || nxt!="bye!"){ 
     reply("How are you?", "I'm fine, thanks.", nxt); 
     reply("What is your favorite color?", "Light green! It is so pretty!", nxt); 
     reply("What do you like to eat?", "Oh, I don't. Computers don't eat.", nxt); 
     reply("Do you like me?", "Yes, but you're way too inquisitive!", nxt); 
     reply("What's your favorite memory?", "Um...Something about my CPU having an extremely - okay...I don't think....Um...", nxt); 
     reply("Where did you learn how to talk?", "This program.", nxt); 
     reply("So...Are you alive?", "No. I don't regulate my body temperature. \n !!!COMPUTER OVERHEATED!!!", nxt); 
     reply("Do you know any jokes?", "Why don't you program some...?", nxt); 
     nxt = scan.nextLine(); 
    } 
0

、IF

if(condition){ 
//code if condition is true 
} 
else { 
//code if condition is false 
} 

は{}の部分他を除去した後、正常に動作し、あなたがふり何だろう、それはしばらくの後である行きます一方、 "else"はできません。 "else"は "if"の後に来るはずです

関連する問題