方法声明中的类型错误的非法启动

时间:2014-02-28 05:38:40

标签: java

有人可以向我解释为什么我仍然会收到以下错误?我试图计算佣金。

  illegal start of type public static void GalleryCommission("Price * .20"

这是我的代码:

            import java.util.Scanner;
            import javax.swing.JOptionPane;

            public class Painting {

            private static Object paintingTitle;
        private static Object artist;
        private static Object medium;
        private static Object price;
        private static Oblect GalleryCommission;

        public static void main(String[] args){

    String result;
    result = JOptionPane.showInputDialog(null, "What is Paint Title?"); 
    JOptionPane.showMessageDialog(null, "Paint Title is " + result + "!");
    result = JOptionPane.showInputDialog(null, "What is Artist name?");
    JOptionPane.showMessageDialog(null, "Artist is " + result + "!");
    result = JOptionPane.showInputDialog(null, "What is Medium?");
    JOptionPane.showMessageDialog(null, "Medium is " + result + "!");
    result = JOptionPane.showInputDialog(null, "What is Price?");
    JOptionPane.showMessageDialog(null, "Price is $" + result + "!");
    result = JOptionPane.showInputDialog(null, "To Calculate Gallery Commission Price is?");
   JOptionPane.showMessageDialog(null, "GalleryCommission is $" + "result *.20");
}

public static Object getPrice() {
    return price;
}
public static void setPrice(Object price) {
   return ObjectPrice;
  }
    public static void GalleryCommission("Price * .20") {
    return GalleryCommission;
}  
 }

3 个答案:

答案 0 :(得分:4)

您的方法存在一些问题。

public static void GalleryCommission("Price * .20") {
    return GalleryCommission;
}

这是void方法,但有return语句。


错误地指定了参数。它一定是这样的

public static void GalleryCommission(DataType argName) {

前:

public static void GalleryCommission(Object price) {

此外,如果您想返回GalleryCommission,请将您的方法签名更改为此类

public static Object GalleryCommission() { // Note that I did not give any argument.
    return GalleryCommission;
}

此行中存在拼写错误

private static Oblect GalleryCommission; // Oblect is not a valid data type

必须是

private static Object GalleryCommission; // Object

答案 1 :(得分:0)

删除"Price * .20"您的方法包含return语句,删除void并更改为

对象类型

 public static Object GalleryCommission() {
        return GalleryCommission;
    } 

答案 2 :(得分:0)

您的方法GalleryCommission

有很多问题
public static void GalleryCommission("Price * .20") {
  return GalleryCommission;
}  

首先,您无法从void方法返回值。其次,你不能将String字面值作为参数传递。最后,您应该使用Java命名约定。考虑到这一点,我想你想要,

private static double COMMISSION = 0.2;
public static double getGalleryCommission(double price) {
  return price * COMMISSION;
}