编译错误变量未初始化

时间:2017-09-16 23:03:10

标签: java

我在这里编写的程序提示用户输入3个整数变量来比较它们,找出哪一个是最大的中间和最小的,那部分工作正常。我的问题是,如果你看看我的第一个if语句,我试图将每个值保存到内存中的变量,我不断得到变量而不是初始化错误。任何帮助都会很棒!我基本上想要存储这些值以继续编写更多代码来比较它们,并找出它们是否创建了一个三角形。

import java.util.Scanner;
    public class TriangleProgram
    {
       public static void main(String [] args)
       {
          Scanner input = new Scanner(System.in);

          int x;
          System.out.print("Enter X: ");
          x = input.nextInt();

          int y;
          System.out.print("Enter Y: ");
          y = input.nextInt();

          int z;
          System.out.print("Enter Z: ");
          z = input.nextInt();

          System.out.println();

          int largest;
          int middle;
          int smallest;


          if(x > y && x > z && y > z)
          {
             System.out.println("Largeset Number: " + x);
             System.out.println("Middle Number: " + y);
             System.out.println("Smallest Number: " + z);

             x = largest;
             y = middle;
             z = smallest;

          }

          else if(x > y && x > z && z > y)
          {
            System.out.println("Largeset Number: " + x);
            System.out.println("Middle Number: " + z);
            System.out.println("Smallest Number: " + y);
          }

          else if(y > x && y > z && x > z)
          {
            System.out.println("Largeset Number: " + y);
            System.out.println("Middle Number: " + x);
            System.out.println("Smallest Number: " + z);
          }

          else if(y > x && y > z && z > x)
          {
            System.out.println("Largeset Number: " + y);
            System.out.println("Middle Number: " + z);
            System.out.println("Smallest Number: " + x); 
          }

          else if(z > x && z > y && x > y)
          {
            System.out.println("Largeset Number: " + z);
            System.out.println("Middle Number: " + x);
            System.out.println("Smallest Number: " + y);
          }

          else
          {
            System.out.println("Largeset Number: " + z);
            System.out.println("Middle Number: " + y);
            System.out.println("Smallest Number: " + x);
          }


       }
    }

3 个答案:

答案 0 :(得分:2)

翻转作业。他们倒退了。

largest  = x;
middle   = y;
smallest = z;

答案 1 :(得分:2)

<button type="button" onclick="dealCards()">Deal New Hand</button>
<hr/>


<dt id = "hand"></dt>

您将最大值(您没有分配任何内容)的值分配给x(同样适用于中间到y和最小值到z)。这就是为什么你得到的变量没有初始化错误,而且可能不是你想要做的。尝试:

         x = largest;
         y = middle;
         z = smallest;

答案 2 :(得分:1)

你要做的if部分

if(
  player['stats'] &&
  player['stats']['guild'] &&
  player['stats']['guild']['master'] &&
  player['stats']['guild']['master']['since'] != undefined    // only check the last one as it is probably not an object but another value such as 0 (depending on what your data looks like, if you have it as an object then just remove the != undefined check)
)