Chào mọi người.
Mình có thằng em đang học java và nó hỏi 1 câu hỏi tưởng dễ mà lại khó. (Với một vài người)
Mình có 1 hàm kiểm tra tam giác vuông thế này.
import java.util.Scanner;
public class Works7 {
public boolean RightTriangle(int a, int b, int c){
if((a*a + b*b == c*c) || (a*a + c*c == b*b) || (c*c + b*b == a*a) ){
return true;
}
return false;
}
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
// Scanner input2 = new Scanner(System.in);
String d;
do{
System.out.print("Enter a : ");
int a = input.nextInt(); //String a = input.nextLine();
System.out.print("Enter b : ");
int b = input.nextInt(); //String b = input.nextLine();
System.out.print("Enter c : ");
int c = input.nextInt(); //String c = input.nextLine();
//
// int e = Integer.parseInt(a);
// int f = Integer.parseInt(b);
// int g = Integer.parseInt(c);
//
Works7 Ws = new Works7();
if(Ws.RightTriangle(a, b, c) == true){
// if(Ws.RightTriangle(e, f, g) == true){
System.out.println("This is a right triangle!");
}else{
System.out.println("This is not a right triangle!");
}
System.out.print("Continue? (Y/N): ");
d = input.nextLine();
// d = input2.nextLine();
}while(d.equals("Y") || d.equals("y"));
}
}
Nếu mình để code như này chương trình sẽ chạy mà bỏ qua câu lệnh nhập d = Yes or No;
Còn nếu mình khai báo 1 cái Scanner = input2 mới : d = input2.nextLint() ;
Hoặc ban đầu khai báo 3 biến String a,b,c rồi convert a,b,c sang integer thì sẽ chạy bình thường.
Muốn hỏi mọi người. Vì sao lại như vậy?