Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java基本计算器_Java - Fatal编程技术网

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语句。此外,如果用户没有键入数字,您可以打印
并输入可接受的
真的?
。你不觉得在那种情况下你应该再回过头来问一次吗?