mardi 3 octobre 2017

My Java if-else statement doesn't seems working

Hi I am student in Computer Science and new to Java. Thank you for dropping by for my question. My tutorial codes with problem are:

/*
 * To change this license header, choose License Headers in Project 
Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tutorial2.pkg1;
import java.util.Random;
import java.util.Scanner;
/**
 *
 * @author User10
 */
 public class Tutorial21 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
Random rand = new Random();
Scanner sc = new Scanner(System.in);

int m = rand.nextInt(9)+0;
int n = rand.nextInt(m)+0;

int answer = m - n;

System.out.println("What is " + m + " - " + n + "?");
sc.nextInt();

if (sc.equals(answer) == true){
    System.out.println("The answer is correct!");
}
else {
    System.out.println("The answer is incorrect!");
    System.out.println("The correct answer is " + answer);
}


}
}

However, the if-else part doesn't work as the program print the answer incorrect part no matter the answer is correct or not.

Aucun commentaire:

Enregistrer un commentaire