【发布时间】:2020-01-19 19:19:46
【问题描述】:
我必须编写一个程序,该程序利用方法重载来计算圆柱体、球体、圆锥体和三棱柱的表面积和体积。我需要接受用户的输入,然后显示正确的表面积和体积。
程序应该做的一个例子是:
Input your shape’s “a” value: none
Input your shape’s “b” value: none
Input your shape’s “c” value: none
Input your shape’s “h” value: none
Input your shape’s “l” value: none
Input your shape’s “r” value: 5
Input your shape’s “s” value: none
Would you like your answer rounded?: yes
To what decimal place?: 0
---------------------------------------------------------------------
Your shape is a sphere!
It’s volume is 524 units cubed.
It’s surface area is 314 units squared.
我已经编写了程序的大部分代码,但是我现在遇到的问题是,每当我完成输入数据时,都会收到错误消息:“线程中的异常”main“java.util.NoSuchElementException:找不到行”。我怀疑问题出在我用来过滤用户给我的内容的 if 语句上,因为每个语句都可以多次出现相同的异常。因此,如果我想计算球体的体积,我需要的唯一输入是半径 r 的输入,而其他所有输入都没有。但是对于圆柱体,除了半径 r 和高度 h 之外,所有其他的都不是。任何帮助将不胜感激。
这是我目前的代码:
import java.util.*;
class Main {
String a = "none"; String b = "none"; String c = "none"; String h = "none"; String r = "none"; String s = "none"; String l = "none";
String round;
double aP; double bP; double cP; double hP; double rP; double sP; double lP;
public static void main(String[] args) throws Exception{
new Main();
}//end main
public Main() throws Exception{
input();
surfaceAreaPrint();
volumePrint();
}//end Main
/**
*
*/
public void input() throws Exception{
Scanner sc = new Scanner(System.in);
System.out.print("");
System.out.print("Input your shape’s “a” value: ");
a = sc.nextLine();
System.out.print("Input your shape’s “b” value: ");
b = sc.nextLine();
System.out.print("Input your shape’s “c” value: ");
c = sc.nextLine();
System.out.print("Input your shape’s “h” value: ");
h = sc.nextLine();
System.out.print("Input your shape’s “r” value: ");
r = sc.nextLine();
System.out.print("Input your shape’s “s” value: ");
s = sc.nextLine();
System.out.print("Input your shape’s “l” value: ");
l = sc.nextLine();
System.out.print("Would you like your answer rounded?: ");
round = sc.nextLine();
System.out.print("");
System.out.print("");
sc.close();
parse();
}//end input
public void parse() throws Exception{
System.out.println(a);
aP = Double.parseDouble(a);
bP = Double.parseDouble(b);
cP = Double.parseDouble(c);
hP = Double.parseDouble(h);
rP = Double.parseDouble(r);
sP = Double.parseDouble(s);
lP = Double.parseDouble(l);
//cylinder
if(a == "none" && b == "none" && c == "none" && s == "none" && l != "none") {
surfaceAreaPrint();
return;
}
//sphere
else if (a.equalsIgnoreCase("none") && b.equalsIgnoreCase("none") && c.equalsIgnoreCase("none") && s.equalsIgnoreCase("none") && h.equalsIgnoreCase("none") && s.equalsIgnoreCase("none") && l.equalsIgnoreCase("none")){
surfaceAreaPrint();
return;
}
//cone
else if (a.equalsIgnoreCase("none") && b.equalsIgnoreCase("none") && c.equalsIgnoreCase("none") && s.equalsIgnoreCase("none") && h.equalsIgnoreCase("none") && l.equalsIgnoreCase("none")){
surfaceAreaPrint();
return;
}
//traingular prism
else if (r.equalsIgnoreCase("none") && s.equalsIgnoreCase("none")){
surfaceAreaPrint();
return;
}
}//end parse
public void surfaceAreaPrint() throws Exception{
Main s = new Main();
System.out.println(s.surfaceArea(hP, rP));
System.out.println(s.surfaceArea(rP));
System.out.println(s.surfaceArea(hP, rP, sP));
System.out.println(s.surfaceArea(aP, bP, cP, lP, hP));
}//end surfaceAreaPrint
public void volumePrint() throws Exception{
Main s = new Main();
System.out.println(s.surfaceArea(hP, rP));
System.out.println(s.surfaceArea(rP));
System.out.println(s.surfaceArea(hP, rP));
System.out.println(s.surfaceArea(bP, lP, hP));
}//end volumePrint
//surface area for cylinder
public double surfaceArea(double hP, double rP){
return (2.0 * Math.PI * Math.pow(rP, 2.0)) + (2.0 * Math.PI * rP * hP);
}//end surfaceArea
//surface area for sphere
public double surfaceArea(double rP){
return (4.0 * Math.PI * Math.pow(rP, 2.0));
}//end surfaceArea
//surface area for cone
public double surfaceArea(double hP, double rP, double sP){
return (Math.PI * Math.pow(rP, 2.0)) + (Math.PI * rP * sP);
}//end surfaceArea
//surface area for traingular prism
public double surfaceArea(double aP, double bP, double cP, double lP, double hp){
return (bP * lP) + (aP * hP) + (bP * hP) + (cP* hP);
}//end surfaceArea
//volume for cylinder
public double volume(double hP, double rP){
return (Math.PI * Math.pow(rP, 2.0) * hP);
}//end volume
//volume for sphere
public double volume(double rP){
return ( 4.0 / 3.0 ) * Math.PI * Math.pow(rP, 3.0);
}//end volume
//volume for cone
public double volume(double hP, double rP, double sP){
return (1.0 / 3.0 ) * Math.PI * Math.pow(rP, 2.0) * hP;
}//end volume
//volume for traingular prism
/**
* calculates volume for traingular prism
* @param times The number of times the die is to be rolled.
* @return The results of rolling a 6-sided die.
*/
public double volume(double aP, double bP, double cP, double lP, double hp){
return (1.0 / 2.0 ) * bP * lP * hP;
}//end volume
}//end Main
【问题讨论】:
标签: java if-statement exception overloading calculator