2016-09-15 99 views
-1

我有这样一个任务:Java输入,扫描仪和if语句

写一个完整的程序称为ExamResult与行为如下所示。 阅读用户输入内容,了解学生的内部考试分数和期末考试分数。

内部测试分数低于20会导致学生失败; 40以下的最终考试成绩也会导致学生失败。

否则学生已通过。

学期考试成绩!

你的内部测试分数是多少? 21

你的期末考试分数是多少? 69

你已经通过了!

到目前为止,我已经发现使用if语句和扫描器进行输入是合适的。 现在这是我的代码(我有课,进口的Java UTIL及以上主梅索德。我选择不包括在这里)

System.out.println("Semester Examination Results!"); 

    System.out.println("What is your internal test score?"); 

    //input 
    //calculate input 
    //does input pass the if test? 
    //output passed/failed 

    int score1 = scanner.nextLine(); 
    int score2 = scanner.nextLine(); 

    if (int score1 < 20; || score2 < 40;) 
    { 
    System.out.println("You have failed"); 
    } 
    else 
    { 
    System.out.println("You have passed"); 
    } 

我不确定什么是如何使用输入计算?

+2

'if(int score1 <20; || score2 <40;)'不会很好地结束。删除';'和'int'。 – Bathsheba

+0

我强烈建议阅读初学者的java教程,然后在Stack Overflow上发布问题。提问“经过深入研究”的基本问题是可以的:) – TheLostMind

+0

我一遍又一遍地阅读我的书,并试图研究,它只是因为它太新了,所以我很容易发作:P –

回答

2

使用scanner.nextInt()而不是nextLine()。你也不需要if语句中的分号。

Edit1

您不需要在if语句中重新初始化score1和2。它ahould这个样子

Scanner scanner = new Scanner(System.in); 
//init and read in values here 
if(score1 < 20 || score2 < 40){ 
    //do stuff 
} 
else{ 
    //do other stuff 
} 
+0

错误:找不到符号 int score2 = scanner.nextInt(); 找不到符号扫描仪,我该怎么办? –

+0

@TinaDitte - 你还没有声明/初始化一个'Scanner',在'int score1'之前添加'Scanner sc = new Scanner(System.in)'' – TheLostMind

+0

已更新我的回答以反映这一点。 –

1
  • 删除;从条件,这些都不是任何声明。也删除int

    if (int score1 < 20; || score2 < 40;) 
    

    应该是:

    if (score1 < 20 || score2 < 40) 
    
  • 此外,使用nextInt()Integer.parseInt(scanner.nextLine())用于取整数输入。