带阵列字符串的缓冲读卡器

时间:2014-02-22 14:01:36

标签: java arrays if-statement bufferedreader

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

    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”这个词将是输出,但如果你不是会员,那么它将是“拉巴斯”。我不知道在“拉巴斯”的其他声明中该怎么做。因为它继续说“否则没有”错误。我搜索了问题为什么和所有答案都没有花括号。我试图把它但仍然存在错误。而在“你不是这里的学生”中的其他声明。正常运作。有什么问题呢?

3 个答案:

答案 0 :(得分:2)

如上面的评论所述,在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.");
}

答案 1 :(得分:0)

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(...)

测试数组是否包含特定值

答案 2 :(得分:0)

包裹测试;

import java.io。*;

class itlog {     public static void main(String args [])抛出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.");

} } 像这样的东西