非法类型字母表的开头

时间:2015-10-28 18:58:03

标签: java

是的,我看了其他代码,但我有一个独特的情况,这里是:好吧所以我的高中老师让我们做一个项目,我们必须使用IF和ELSE声明来找出我们的首字母来自YES和NO输入(0 =不,1 =是)并且它必须与他选择的每个字母一起工作,但是在第45行它表示非法的类型开始,但唯一的东西还有其他...反正这是代码,并提前感谢您的帮助



/* Objective: practice completing if, if-else,block statements.
 *            and relational operators.
 */
import java.io.*;
import java.util.*;
public class Alphabet2 { 
  public static void main(String args[]) {
  	final int YES = 1;
  	final int NO = 0;
  	int answer = 0;
  	Scanner kbReader = new Scanner(System.in);	
  	
    System.out.println("Think of a letter from A to Z\n\n");
    
    System.out.println("0 = A-M");
    System.out.println("1 = N-Z");
    System.out.print("Enter your choice\t");
    answer = kbReader.nextInt();
    if (answer == NO){
    	System.out.println("\nOK, A thru M\n");
        System.out.println("0 = A-G");
        System.out.println("1 = H-M");
        System.out.print("Enter your choice\t");
        answer = kbReader.nextInt();
        if (answer == YES){
           System.out.print("\nOK, H thru M \n");
           System.out.print("\n0 = H-J \n");
           System.out.print("\n1 = K-M \n");
           System.out.print("Enter your choice \t");
           answer = kbReader.nextInt();
             if(answer == YES){
               System.out.print("lol");
             }
             
             else {
             }
           }
        //else
          // System.out.print("");
        }
    else {
        System.out.println("OK, A thru G\n");
        }
    }
    else {
    	System.out.println("\nOK, N thru Z\n");
        System.out.println("0 = N-S");
        System.out.println("1 = T-Z");
        System.out.print("Enter your choice\t");
        answer = kbReader.nextInt();
        if (answer == NO){
    	   System.out.println("OK, N thru S\n");
         }
         else {	
            System.out.println("OK, T thru Z\n");
         }      
      }   
   }




2 个答案:

答案 0 :(得分:1)

在底部是您的代码正确缩进。

请注意

  else {
    System.out.println("\nOK, N thru Z\n");
关闭main方法的大括号后会出现

您收到该特定错误消息的原因是解析器认为else方法之后出现的main是另一个方法或字段声明的类型,因为它不是关键字修饰符可能是会员声明的一部分。

考虑这个问题的一种方法是在提交内容之前将其完全分解:

// A-Z
if (...) {
  // A-M
} else {
  // N-Z
}

然后再多一层

// A-Z
if (...) {
  // A-M
  if (...) {
    // A-F
  } else {
    // G-M
  }
} else {
  // N-Z
  if (...) {
    // N-S
  } else {
    // T-Z
  }
}

/* Objective: practice completing if, if-else,block statements.
 *            and relational operators.
 */
import java.io.*;
import java.util.*;
public class Alphabet2 { 
  public static void main(String args[]) {
    final int YES = 1;
    final int NO = 0;
    int answer = 0;
    Scanner kbReader = new Scanner(System.in);  

    System.out.println("Think of a letter from A to Z\n\n");

    System.out.println("0 = A-M");
    System.out.println("1 = N-Z");
    System.out.print("Enter your choice\t");
    answer = kbReader.nextInt();
    if (answer == NO){
      System.out.println("\nOK, A thru M\n");
      System.out.println("0 = A-G");
      System.out.println("1 = H-M");
      System.out.print("Enter your choice\t");
      answer = kbReader.nextInt();
      if (answer == YES){
        System.out.print("\nOK, H thru M \n");
        System.out.print("\n0 = H-J \n");
        System.out.print("\n1 = K-M \n");
        System.out.print("Enter your choice \t");
        answer = kbReader.nextInt();
        if(answer == YES){
          System.out.print("lol");
        }

        else {
        }
      }
      //else
      // System.out.print("");
    }
    else {
      System.out.println("OK, A thru G\n");
    }
  }
  else {
    System.out.println("\nOK, N thru Z\n");
    System.out.println("0 = N-S");
    System.out.println("1 = T-Z");
    System.out.print("Enter your choice\t");
    answer = kbReader.nextInt();
    if (answer == NO){
      System.out.println("OK, N thru S\n");
    }
    else {  
      System.out.println("OK, T thru Z\n");
    }      
  }   
}

答案 1 :(得分:0)

/* Objective: practice completing if, if-else,block statements.
 *            and relational operators.
 */
import java.io.*;
import java.util.*;
public class Alphabet2 { 
  public static void main(String args[]) {
    final int YES = 1;
    final int NO = 0;
    int answer = 0;
    Scanner kbReader = new Scanner(System.in);  

    System.out.println("Think of a letter from A to Z\n\n");

    System.out.println("0 = A-M");
    System.out.println("1 = N-Z");
    System.out.print("Enter your choice\t");
    answer = kbReader.nextInt();
    if (answer == NO){
        System.out.println("\nOK, A thru M\n");
        System.out.println("0 = A-G");
        System.out.println("1 = H-M");
        System.out.print("Enter your choice\t");
        answer = kbReader.nextInt();
        if (answer == YES){
           System.out.print("\nOK, H thru M \n");
           System.out.print("\n0 = H-J \n");
           System.out.print("\n1 = K-M \n");
           System.out.print("Enter your choice \t");
           answer = kbReader.nextInt();
             if(answer == YES){
               System.out.print("lol");
             }

        else{
          System.out.print("");
        }
    }
    else {
        System.out.println("OK, A thru G\n");
        }
    }
    else {
        System.out.println("\nOK, N thru Z\n");
        System.out.println("0 = N-S");
        System.out.println("1 = T-Z");
        System.out.print("Enter your choice\t");
        answer = kbReader.nextInt();
        if (answer == NO){
           System.out.println("OK, N thru S\n");
         }
         else { 
            System.out.println("OK, T thru Z\n");
         }      
      }

} }