[英]Java Basic Calculator
我需要构建此程序,该程序将找到三角形的缺失边,但我不断收到错误消息。 这是我的代码:
import java.util.Scanner;
public class MissingSide {
static java.util.Scanner userInput = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("What is the first side, other than the hypotenuse?");
if (userInput.hasNextInt()) {
int firstsideGiven = userInput.nextInt();
} else {
System.out.println("Enter something acceptable");
}
System.out.println("What is the hypotenuse?");
if (userInput.hasNextInt()) {
int hypotenuseGiven = userInput.nextInt();
} else {
System.out.print("Really?");
}
System.out.print("Your missing side value is: " +
System.out.print((Math.pow(firstsideGiven, 2) - Math.pow(hypotenuseGiven, 2)) + "this");
}
}
它不断告诉我“ hypotenuseGiven”和“ firstsideGiven”不能解析为变量。 这是供个人使用,而不是学校的事情。 谢谢。
范围hypotenuseGiven
和firstsideGiven
仅限于if() {...}
代码中的语句。
您不能在该范围之外使用它们。 如果您愿意,可以在if() {...}
块之外声明它们。
变量的范围限于if块。
特别说明:
1)代码的System.out.print()部分也存在语法错误。
2)计算需要根据毕达哥拉斯公式的平方根。
调试代码示例如下:
import java.util.*;
import java.lang.Math;
public class MissingSide
{
public static void main(String[] args)
{
int firstsideGiven = 0;
int hypotenuseGiven = 0;
Scanner userInput = new Scanner(System.in);
System.out.print("What is the first side, other than the hypotenuse?");
if (userInput.hasNextInt()){
firstsideGiven = userInput.nextInt();
}else {
System.out.println("Enter something acceptable");
}
System.out.println("What is the hypotenuse?");
if (userInput.hasNextInt()){
hypotenuseGiven = userInput.nextInt();
}else{
System.out.print("Really?");
}
System.out.print("Your missing side value is: " +(Math.sqrt((hypotenuseGiven*hypotenuseGiven)-(firstsideGiven*firstsideGiven))));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.