Java基本计算器
我需要建立这个程序,将发现一个三角形丢失的一面,但我不断得到一个错误消息。这是我的代码:Java基本计算器,java,Java,我需要建立这个程序,将发现一个三角形丢失的一面,但我不断得到一个错误消息。这是我的代码: 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 s
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");
}
}
它一直告诉我,“斜边给定”和“第一边给定”不能分解成一个变量。这是私人用品,不是学校用品。谢谢。斜边切割和
firstsideGiven
的范围仅限于代码中的if(){…}
语句
您不能在该范围之外使用它们。如果您希望这样做,请在
If(){…}
块之外声明它们。斜边插入和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))));
}
}
变量的范围仅限于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))));
}
}
您在if语句中声明了变量,这意味着它的作用域仅限于if语句。此外,如果用户没有键入数字,您可以打印
并输入可接受的或真的?
。您不认为在这种情况下应该再次循环询问吗?您在if语句中声明了变量,这意味着它的作用域仅限于if语句。此外,如果用户没有键入数字,您可以打印并输入可接受的或真的?
。你不觉得在那种情况下你应该再回过头来问一次吗?