By GokiSoft.com|
20:16 17/06/2022|
Java Basic
Java Basic- Giải phương trình bậc 2 trong java
Giải phương trình bậc nhất ax + b = 0;
Giái phương trình bậc 2 : ax2 + bx + c = 0
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Lê Xuân Dũng [JavaFree]](https://www.gravatar.com/avatar/af3e93d6e9bd94db12e2b8a1069aef68.jpg?s=80&d=mm&r=g)
Lê Xuân Dũng
2020-03-19 09:49:22
package giai_pt_bac_2;
import java.util.Scanner;
public class Giai_PT_bac_2 {
public static void main(String[] args) {
float a, b, c;
float delta;
float x, x1, x2;
Scanner sc = new Scanner(System.in);
System.out.println("Nhap a:");
a = sc.nextFloat();
System.out.println("Nhap b:");
b = sc.nextFloat();
System.out.println("Nhap c:");
c = sc.nextFloat();
if (a==0){
if(b!=0){
x = -c/b;
System.out.println("PT co 1 nghiem x= "+x);
}
else {
if(c!=0){
System.out.println("PT vo nghiem!");
}
else {
System.out.println("PT co vo so nghiem!");
}
}
} else{
delta = b*b - 4*a*c;
if(delta<0) {
System.out.println("PT vo nghiem!");
} else if (delta==0){
x= -b/(2*a);
System.out.println("PT co nghiem kep x1 = x2 = "+x);
} else if(delta>0){
x1 = (float) ((-b-Math.sqrt(delta))/(2*a));
x2 = (float) ((-b+Math.sqrt(delta))/(2*a));
System.out.println("PT co 2 nghiem phan biet x1 = "+x1+", x2 = "+x2);
}
}
}
}
![Trần Mạnh Dũng [T1907A]](https://www.gravatar.com/avatar/ee4661d1f9133c241d6c8287c7ea8ceb.jpg?s=80&d=mm&r=g)
Trần Mạnh Dũng
2020-03-16 13:03:43
package BT43;
import java.util.Scanner;
public class Bai4 {
public static void main(String[] args) {
int a,b;
float x;
Scanner scan = new Scanner(System.in);
System.out.print("Nhập a = ");
a = scan.nextInt();
System.out.print("Nhập b = ");
b = scan.nextInt();
if(a == 0) {
if(b == 0){
System.out.println("Phương trình có vô số nghiệm.");
} else {
System.out.println("Phương trình vô nghiệm.");
}
} else {
x = (-1*b)/a;
System.out.println("Phương trình có 1 nghiệm x = " + x);
}
}
}
![Lê Minh Bắc [T1907A]](https://www.gravatar.com/avatar/22abcac77d8ca5e01144e240abb48d22.jpg?s=80&d=mm&r=g)
Lê Minh Bắc
2020-03-16 12:24:46
package baitap30;
import java.util.Scanner;
/**
*
* @author lemin
*/
public class BT30 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
float a,b,x;
Scanner scan = new Scanner(System.in);
System.out.println("Giu1ea3i PT bu1eadc nhu1ea5t ax + b = 0
Nhu1eadp a:");
a = scan.nextFloat();
System.out.println("Nhu1eadp b:");
b = scan.nextFloat();
x = -b/a;
System.out.println(a + "x" + " + " + b + " = 0");
System.out.println("=> x = " + x);
}
}
![Hoang Ngo [T1907A]](https://www.gravatar.com/avatar/9f7d962f002d4b5c555b1ee25b3622ff.jpg?s=80&d=mm&r=g)
Hoang Ngo
2020-03-16 06:58:13
package baitap;
import java.util.Scanner;
public class Bai1{
public static void main(String args[]) {
int a;
int b;
Scanner sc = new Scanner(System.in);
System.out.print("a = ");
a = sc.nextInt();
if(a == 0){
System.out.format("Mời nhập lại a: ");
a = sc.nextInt();
}
System.out.print("b = ");
b = sc.nextInt();
int x;
x = -b/a;
System.out.format("Nghiệm của phương trình là: %d", x);
}
}
![Đỗ Văn Huấn [T1907A]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ Văn Huấn
2020-03-16 06:44:04
package BaiTapNgay24_2_2020;
import java.util.Scanner;
public class BAI30 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Bài 1.
System.out.println("bai1: giai pt bac nhat ax+b=0");
System.out.println("Nhập vào số a: ");
float a = scanner.nextFloat();
System.out.println("Nhập vào số b: ");
float b = scanner.nextFloat();
float x;
if(a==0){
if(b==0){
System.out.println("Phương trình vố số nghiệm.");
}
else{
System.out.println("Phương trình vô nghiệm.");
}
}
else{
System.out.println("Phương trình có 1 nghiệm x = "+ (-1*b)/a);
![Ngô Quang Huy [C1907L]](https://www.gravatar.com/avatar/8e54dbf5994077ce599f49278164ae79.jpg?s=80&d=mm&r=g)
Ngô Quang Huy
2020-03-16 05:52:37
package bt;
import java.util.Scanner;
public class Bt1 {
public static void main(String[] args) {
float a,b;
Scanner input = new Scanner(System.in);
System.out.println("insert a: ");
a = input.nextFloat();
System.out.println("insert b: ");
b = input.nextFloat();
System.out.println(a+"x + "+b+" = 0");
System.out.println("Giai Pt: x = "+ -1*b/a);
}
}
![Nguyễn Hữu Đạt [C1907L]](https://www.gravatar.com/avatar/343beaebafd55688b65478947cb718be.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Đạt
2020-03-16 05:11:35
package baitap;
import java.util.Scanner;
public class Bai1{
public static void main(String args[]) {
int a;
int b;
Scanner sc = new Scanner(System.in);
System.out.print("a = ");
a = sc.nextInt();
if(a == 0){
System.out.format("Mời nhập lại a: ");
a = sc.nextInt();
}
System.out.print("b = ");
b = sc.nextInt();
int x;
x = -b/a;
System.out.format("Nghiệm của phương trình là: %d", x);
}
}
![Nguyễn Hữu Đạt [C1907L]](https://www.gravatar.com/avatar/343beaebafd55688b65478947cb718be.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Đạt
2020-03-16 05:11:29
package baitap;
import java.util.Scanner;
public class Bai1{
public static void main(String args[]) {
int a;
int b;
Scanner sc = new Scanner(System.in);
System.out.print("a = ");
a = sc.nextInt();
if(a == 0){
System.out.format("Mời nhập lại a: ");
a = sc.nextInt();
}
System.out.print("b = ");
b = sc.nextInt();
int x;
x = -b/a;
System.out.format("Nghiệm của phương trình là: %d", x);
}
}
![hoangkhiem [C1907L]](https://www.gravatar.com/avatar/d3627ce786997fab24d1b790c91c6368.jpg?s=80&d=mm&r=g)
hoangkhiem
2020-03-16 02:31:11
/*
* 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 baitap;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Giaipt {
public static void main(String[] args) {
float a,b,x;
System.out.println("Giải phương trình bậc nhất ax + b = 0");
Scanner Nhap = new Scanner(System.in);
System.out.println("nhap a : ");
a = Nhap.nextFloat();
System.out.println("nhap b : ");
b = Nhap.nextFloat();
x = ((-b)/a);
System.out.println("X co duoc là %f" +x);
}
}
![trung [C1907L]](https://www.gravatar.com/avatar/67c23432e4710f33dd14e580d41b0379.jpg?s=80&d=mm&r=g)
trung
2020-03-15 15:18:38
import java.util.Scanner;
import java.lang.Math;
/**
*
* @author prdox
*/
//Viết chương trình hiển thị thông tin cá nhân của bạn bao gồm (tên, tuổi, địa chỉ, email, sđt)
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Giải phương trình bậc hai ax^2 + bx + c = 0;
System.out.println("a:= ");
float a = Float.parseFloat(input.nextLine());
System.out.println("b:= ");
float b = Float.parseFloat(input.nextLine());
System.out.println("c:= ");
float c = Float.parseFloat(input.nextLine());
float delta = b*b - 4*a*c;
if (a == 0){
if (b == 0){
if (c == 0){
System.out.println("Phuong trinh co vo so nghiem");
}else{
System.out.println("Phuong trinh vo nghiem");
}
}else{
System.out.format("Phuong trinh co nghiem duy nhat la: %f",-c/b);
}
return;
}
//a != 0
if (delta < 0){
System.out.println("Phuong trinh vo nghiem");
}else if (delta == 0){
System.out.format("Phuong trinh co nghiem duy nhat la: %f",-b/(2*a));
}else{
System.out.println("Phuong trinh co 2 nghiem la:");
System.out.format("x1:= %f\n",(-b+Math.sqrt(delta))/(4*a));
System.out.format("x1:= %f\n",(-b-Math.sqrt(delta))/(4*a));
}
}
}