java打印 a b c,创建一个java程序,按顺序给出3个术语作为输入(a,b,c)打印它们的根...
Wrie a method printRoots that given 3 terms as input(a,b,c) in that order prints their roots
We have the following given information
If b²-4ac is a positive number, your program should print “The two roots are X and Y” where X is the larger root and Y is the smaller root
If b²-4ac *equals 0*, the program should print. “The equation has one X” where X is the only root
If b²-4ac is a negative number, the program should print.” The equation has two roots(-X1 + Y1i) and (-X2 and Y2i)
The term can be determined based on:
If b^2 - 4ac is a negative number, then the quadratic equation becomes: (-b+/- √C)/2a
-This means the equation can be simplified to become (-b+/- √Ci)/2a where the square root is not a positive number
Calculate the coefficient and print that(i.e X1 is -b/2a and Y1 is sqrt(-C)/2i
Note: Not allowed to use Scanners for this question
Is it possible for someone to review my program and tell me where I have gone wrong and do i just remove my scanners to make it a program without scanners?
import java.util.Scanner;//delete this part after
public class findingRoots {
public static void main(String[] args)
{
}
public static double printRoots (){ //should it be double here or int?
//read in the coefficients a,b,and c
Scanner reader = new Scanner(System.in);
int a=reader.nextInt();
System.out.println("Enter the value of a");
int b=reader.nextInt();
System.out.println("Enter the value of b");
int c=reader.nextInt();
System.out.println("Enter the value of c");
//now compte the discrimintat d
double discrimintant = d;
double X,Y; //root 1 & root 2, respectively
// is the step double X,Y necessary?
double d = (b*b)-(4.0*a*c);
if (d > 0.0){
d = Math.sqrt(d);
System.out.println("The two roots are X and Y");
double X = (-b + d)/(2.0 * a ); //X= root 1, which is larger
double Y = (-b - d)/(2.0 *a); //Y= root 2, which is the smaller root
System.out.println("Root 1" = X "and" "Root 2" "=" Y);
}
else{
if (d==0.0) //then...how to write?
System.out.println("The equation has one root X")//where X is the only root
double X = (-b + 0.0)/(2.0 * a);//repeated root
System.out.println("Root" "=" X);
}
else{
if(d < 0.0)
System.out.println("The equation has two roots (-X1 + Y1i) and (-X2 +Y2i)");
// where i represents the square root of negative 1
double X1 = -b/(2*a);
double Y1 = (Math.sqrt(-C))/(2*a);
double X2 = -b/(2*a);
double Y2 = (-(Math.sqrt(-C)))/(2*a);
double Y2 = (-(Math.sqrt(-C)))/(2*a);
System.out.println("Root 1" "=" (-X1 + Y1i) "and" "Root 2" "=" (-X2 +Y2i)");
}
}
}
解决方案
you can pass input from command lines. You will get the data at args array
in public static void main(String[] args) here args refers to command line arguements
when you run a java program using
java MyApp arg1 arg2
in your main args[0] is arg1 and args[1] is arg2
So in your case run the app like following command
java findingRoots 1 2 3
and in main
int a= Integer.parseInt(args[0])
N.B I think you would like to validate the command line parameters. check both the args.length and if they are int or not
java打印 a b c,创建一个java程序,按顺序给出3个术语作为输入(a,b,c)打印它们的根...相关推荐
- Java web 开发填坑记 2 -如何正确的创建一个Java Web 项目
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/72566261 本文出自[赵彦军的博客] Java web 开发填坑记 1-如何正确 ...
- 如何使用eclipse软件创建一个Java项目?
同学们在参加Java的时候老师肯定会教给你们如何去创建一个项目,这里怕有些同学没记住,所以单独为大家分享一篇如何使用eclipse软件创建一个Java项目教程,感觉有用的话收藏转发一下~ eclips ...
- 创建一个java项目
新建一个java项目 创建一个java项目 使用MyEclipse 新建项目 文件>新建>java项目 项目名称与存放路径 如果弹出此项,选择yes 项目创建成功 界面的效果 项目目录中的 ...
- idea怎样创建一个java项目?
idea创建java项目的方法:首先在电脑上打开idea,并依次点击"File->New->Project";然后选择JDK安装的路径,并点击New;接着创建一个简单的 ...
- java全景图片生成_[Java教程]使用Javascript来创建一个响应式的超酷360度全景图片查看幻灯效果...
[Java教程]使用Javascript来创建一个响应式的超酷360度全景图片查看幻灯效果 0 2015-07-23 18:00:14 360度的全景图片效果常常可以用到给客户做产品展示,今天这里我们 ...
- Docker创建一个java程序镜像
Docker创建一个java程序镜像 什么是Dockerfile 指令及其说明 个人镜像创建流程 Dockerfile文件创建 使用docker build命令构建镜像 运行镜像 附文件目录结构 什么 ...
- 《快乐编程大本营》java语言训练班 1课:第一个java程序:你好,范冰冰;
<快乐编程大本营>java语言训练班 1课:第一个java程序:你好,范冰冰: 1Java介绍 2安装java环境JDK 3安装web环境tomcat 4安装开发工具Idea2017 5编 ...
- 创建一个动物类,具有name.age.sex,从键盘输入动物的信息的同时使用io流保存这些信息到d:\\1.txt,并且将这些信息存放入动物的对象,存放入一个TreeSet集合(要求根据年龄进行排序)
创建一个动物类,具有name.age.sex,从键盘输入动物的信息的同时使用io流保存这些信息到d:\1.txt,并且将这些信息存放入动物的对象,存放入一个TreeSet集合(要求根据年龄进行排序)只 ...
- 代码实现从键盘接收一个字符串, 程序对其中所有字符进行排序,例如键盘输入: helloitcast程序打印:acehillostt...
package com.loaderman.test;import java.util.Comparator; import java.util.Scanner; import java.util.T ...
最新文章
- cookie和session的代码实现
- Kaggle Tabular Playground Series - Jan 2022 的baseline和日期特征处理
- 25 linux ndk 头文件_正点原子Linux第二十五章RTC实时时钟实验
- Android开发之Socket通信的方法
- 【LeetCode笔记】406. 根据身高重建队列(Java、偏数学)
- UVA - 227 Puzzle
- python执行批处理文件_如何从Python中执行批处理文件,从而改变调用进程的环境?...
- java p8级别_JAVA程序员月入5000+很迷茫,如何能在一年内改变达到月入过万?
- 《Spring技术内幕》——2.3节IoC容器的初始化过程
- 使用IDE宏遍历代码中的非ASCII字符
- HDU2048 神、上帝以及老天爷【递推】
- layui中全选反选_jQuery实现全选,反选,全不选
- 常用的laplace变换公式表
- 如何导出久其报表所有数据_久其报表不能传输怎么办?
- 数据结构c语言描述 试题,数据结构c语言版试题大全(含答案)
- 协同过滤推荐算法概括总结
- 音频信号输入itc服务器,ITC公共广播系统
- 计算机科学的顶级会议期刊,一些计算机领域的顶级会议和期刊
- 汕尾市海陆丰民间开胃小食摘录
- OSG三维渲染引擎编程学习之一:“第一章:OSG介绍” 之 “1.1 OSG简介”
热门文章
- Nosql数据库之mongodb c++使用实例
- 文档扫描识别——基于M-LSD线段检测的拍照文档校正
- python3输入密码显示*_python在控制台输入密码但是不显示
- jq 比较两个时间是否在同一天_.NET CORE下最快比较两个文件内容是否相同的方法...
- python字符集_PYTHON 中的字符集
- 学python的总结_为什么那么多自学Python的后来都放弃了,总结起来就这些原因
- linux性能分析资源推荐(重要)
- 输出EXCEL文件的通用函数
- pip安装python包出错:ctype = ctype.encode(default_encoding) # omit in 3.x!
- XMLSpy编写DTD出错:Character 'D' is grammatically unexpected