簡體   English   中英

由於一種方法有參數,主函數不起作用? 方法 Height(int, int ) 不適用於參數 ()

[英]Main function not working due to one method having parameters ? The method Height(int, int ) is not applicable for arguments ()

// Patrik Maco
// 18/10/21
// VERSION 1 
// Write a program that allows the user to input the length, width and height of a room (cm) - volme of balloon (m^3) prints the volume of the room in m^3 and the amount of balloons needed to fill the room.

import java.util.Scanner; 

class volume 
{      
    public static void main (String [] a)
    {     
             Run();
             System.exit(0);
    }

            public static int Length()
{              Scanner scanner  = new Scanner(System.in);
               System.out.println("Enter Values as whole numbers- not as decimals. Do not include measurement. Good example : 5,7,9 Bad example: 3.2, 5cm");
               System.out.println("Length of room (in cm) ? ");
               int length = scanner.nextInt(); 
               return length;
} 

           public static int Width()
{             Scanner scanner = new Scanner(System.in);
              System.out.println("Enter Values as whole numbers- not as decimals. Do not include measurement. Good example : 5,7,9 Bad example: 3.2, 5cm");
              System.out.println("Width of room (in cm) ? ");
              int width = scanner.nextInt();
              return width;
} 

           public static int Height(length, width)
{            Scanner scanner = new Scanner(System.in);
             System.out.println("Enter Values as whole numbers- not as decimals. Do not include measurement. Good example : 5,7,9 Bad example: 3.2, 5cm");
             System.out.println("Height of room (in cm) ? ");
             int height = scanner.nextInt();
             return height;
             System.out.println("Enter the volume of the baloon in M cubed");
             float volume_b;
             volume_b = scanner.nextFloat();
             float RoomVolume;
             RoomVolume = ( height * length * width);
             System.out.println("The volume of the room is : " + RoomVolume);
             float balloon3;
             balloon3 = RoomVolume/1000000;
             float BalloonAmmount;
             BalloonAmmount = balloon3/volume_b;
             int n;
             n = (int)BalloonAmmount;
             System.out.println("The ammount of balloons needed is : " + n);
}
          public static void Run()
{            int length = Length();
             int width =  Width();
             int height = Height();
}

}

**方法 Run() 返回錯誤消息 方法 Height(int, int ) 不適用於參數 ()。 我對編碼很陌生,如果可能的話,我喜歡解釋我如何解決這個問題。

高度在它自己的方法中包含兩個參數,但是如果我在 Run() 中輸入相同的參數,我的程序就不會運行**

您的方法Height存在多個問題。

  1. 在Java中,所有參數都需要指定其類型,所以應該聲明為
    public static int Height(int length, int width)
  1. 無條件返回語句之后的所有代碼都是不可訪問的
       return height;
       // any code after this point is unreachable
  1. 如果一個方法需要參數,你需要明確地提供它們:
    public static void Run()
    {
        int length = Length();
        int width =  Width();
        int height = Height(length, width); // you need to pass length and width!
    }

其他觀察:

  • 不需要在main的末尾顯式調用System.exit(0) ,程序在該點自動結束。
  • 在 Java 中,將字段和類視為名詞,將方法視為動詞,並相應地命名它們是一種非常普遍的命名約定。 此外,類名通常以大寫字母SomeClassName開頭,而方法名通常以小寫字母doSomething()開頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM