【问题标题】:Buffered reader with array string [closed]带有数组字符串的缓冲阅读器[关闭]
【发布时间】:2014-02-22 14:01:35
【问题描述】:

我正在制作一个程序,让您知道自己的成绩(如果您是班级成员,就像班级部分一样。)这只是数组的基础研究,我仍在学习它。很抱歉问这个问题(如果你认为这对你来说太基础了)

    import java.io.*;
    class itlog {
    public static void main (String args[]) throws IOException {

    int [] student_num = {100,56,75,70,90,50};
    int student_no;
    String [] studentName = {"janet, dada, dodo, dede, didi, hihi"};
    String student;     

    BufferedReader rb = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What is your name?: ");
    student = rb.readLine();

    if (student.equals("janet"));
    {System.out.println("Pasok.");


    if (student.equals("dada"));
    System.out.println("Pasok.");


    if (student.equals("dodo"));
    System.out.println("Pasok.");


    if (student.equals("dede"));
    System.out.println("Pasok.");


    if (student.equals("didi"));
    System.out.println("Pasok.");

    if (student.equals("hihi"));
    System.out.println("Pasok.");

    }

    else
    System.out.println("Labas.");



    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Type your Class Ranking: ");
    student_no = Integer.parseInt(br.readLine());

    if (student_no >= 1 && student_no < 6)
    System.out.println("You're School Grade is" + " " + student_num[student_no-1]);

    else
    System.out.println("You're not a student here.");

}
}

如您所见,我想制作一个程序,如果您在此处输入您的姓名(例如您是 janet),则输出“Pasok”一词,但如果您不是会员,则它将是“Labas”。我不知道在“Labas”的else语句中该怎么做。因为它一直在说“没有 if 的 else”错误。我搜索了为什么和所有答案都没有大括号的问题。我试图把它,但仍然有一个错误。而“你不是这里的学生”中的 else 语句。正常运行。它有什么问题?

【问题讨论】:

  • 您不应该在if 条件的末尾使用分号:if (student.equals("janet"));。也就是说,这不是这里唯一的错误。如果student.equals("janet") 为真,那么if (student.equals("dada")) 肯定不会为真..
  • 太棒了!谢谢!没注意到!我的程序正在运行!

标签: java arrays if-statement bufferedreader


【解决方案1】:

正如上面的评论所说,删除if 语句末尾的分号;你真正的if 声明是

if (student.equals("dede")) {
  ; // Empty instruction!
}
System.out.println("Pasok.");

更好的方法可能是使用Set 的学生而不是多个if;更少的代码,更少的错误!

Set<String> students = new HashSet<String>(Arrays.asList(new String [] {"janet, dada, dodo, dede, didi, hihi"}));
if(students.contains(student)) {
  System.out.println("Pasok.");
}
else {
  System.out.println("Labas.");
}

【讨论】:

  • 哇!谢谢你的信息!那会让我的一天更轻松!谢谢:D
  • +1 用于使用 HashSet 简化代码并回答 OP 的问题
【解决方案2】:
if (student.equals("janet"))
{System.out.println("Pasok.");


if (student.equals("dada"))
System.out.println("Pasok.");


if (student.equals("dodo"))
System.out.println("Pasok.");


if (student.equals("dede"))
System.out.println("Pasok.");


if (student.equals("didi"))
System.out.println("Pasok.");

if (student.equals("hihi"))
System.out.println("Pasok.");

}

else
System.out.println("Labas.");

在您的代码中不应有“;”每个 if 条件语句后的分号。您仍然可以使用以下功能

Arrays.asList(...).contains(...)

测试一个数组是否包含某个值

【讨论】:

    【解决方案3】:

    封装测试;

    导入 java.io.*;

    类 itlog { public static void main (String args[]) throws IOException {

    int [] student_num = {100,56,75,70,90,50};
    int student_no;
    String [] studentName = {"janet, dada, dodo, dede, didi, hihi"};
    String student;     
    
    BufferedReader rb = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What is your name?: ");
    student = rb.readLine();
    
    if (student.equals("janet"))
    {
    System.out.println("Pasok.");
    
    if (student.equals("dada"))
    System.out.println("Pasok.");
    
    
    if (student.equals("dodo"))
    System.out.println("Pasok.");
    
    
    if (student.equals("dede"))
    System.out.println("Pasok.");
    
    
    if (student.equals("didi"))
    System.out.println("Pasok.");
    
    if (student.equals("hihi"))
    System.out.println("Pasok.");
    
    }
    else
    System.out.println("Labas.");
    
    
    
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Type your Class Ranking: ");
    student_no = Integer.parseInt(br.readLine());
    
    if (student_no >= 1 && student_no < 6)
    System.out.println("You're School Grade is" + " " + student_num[student_no-1]);
    
    else
    System.out.println("You're not a student here.");
    

    } } 像这样的

    【讨论】:

      猜你喜欢
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2015-06-18
      • 2023-03-28
      • 2011-04-01
      • 1970-01-01
      • 2015-08-30
      相关资源
      最近更新 更多