[英]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.