输出不正确或无法编译

时间:2013-10-06 18:31:45

标签: java

我遇到了几个程序的问题 - 一个是我要调试的,另一个是我需要编写的。

第一个我要用户输入行星名称,将其转换为大写,然后通过枚举运行。我做错了什么? import java.util。*;

public class DebugNine4 {
enum Planet {
    MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE
};

public static void main(String[] args) {
    Planet planet;
    String userEntry;
    int position;
    int comparison;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a planet in our solar system >> ");
    planet = input.nextLine().toUpperCase();
    planet = Planet.valueOf(planet);
    System.out.println("You entered " + planet);
    position = planet.ordinal();
    System.out.println(planet + " is " + (position + 1)
            + " planet(s) from the sun");
}
}

第二个是输出没有像我想要的那样经过。它回来了:

Cut Shampoo

而不是我需要它做的信息 - 可以按服务名称,成本和时间进行排序。我在这个代码上哪里出错?

  1. 首先,您需要创建一个包含三个数据字段的Service对象。
  2. 接下来,您需要创建一个包含6个服务对象的数组,并使用表中的表9.6,第420页中的数据填充它们,如:

    service[0] = new Service("Cut",8.00,15);
    
  3. 使用扫描程序对象,从问题中收集回复:"Sort services by (S)ervice, (P)rice, or (T)ime"

  4. 根据以下输入返回按正确顺序列出的3个格式列中的所有信息:

    按时间排序:

    Trim          $6.00    5 minutes
    Shampoo        $4.00    10 minutes
    
  5. 代码:

    public class SalonReport {
    
    public static void main(String[] args) {
    
        // services listing with time and cost
        Service[] myService = new Service[6];
        myService[0] = new Service("Cut", 8.00, 15);
        myService[1] = new Service("Shampoo", 4.00, 10);
        myService[2] = new Service("Manicure", 18.00, 30);
        myService[3] = new Service("Style", 48.00, 55);
        myService[4] = new Service("Permanent", 18.00, 35);
        myService[5] = new Service("Trim", 6.00, 5);
        SortDescription(myService, myService.length);
        System.out.println(myService[0].getServiceType() + " "
                + myService[1].getServiceType());
    }
    
    public static void SortDescription(Service[] array, int len) {
        int a;
        int b;
        Service temp;
    
        for (a = 0; a < len; ++a)
            for (b = 0; b < len - 1; ++b) {
    
                if (array[b].getServiceType().compareTo(
                        array[b + 1].getServiceType()) > 0)
                    ;
                {
                    temp = array[b];
                    array[b] = array[b + 1];
                    array[b + 1] = temp;
    
                }
    
            }
    
    }
    }
    
    class Service {
    
    // declaring parameters
    String servDescript;
    double price;
    int avgMin;
    
    public Service(String s, double p, int m) { // constructor
        servDescript = s;
        price = p;
        avgMin = m;
    
    }
    
    // method returning requested item -
    
    public String getServiceType() {
        return servDescript;
    }
    
    public double getPrice() {
        return price;
    }
    
    public int getMinutes() {
        return avgMin;
    }
    }
    

3 个答案:

答案 0 :(得分:0)

首先似乎是:

planet = Planet.valueOf(input.nextLine().toUpperCase());

删除此行,因为您无法将String放入枚举类型变量:

    planet = input.nextLine().toUpperCase();

编辑:

public class DebugNine4 {
enum Planet {
    MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE
};

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a planet in our solar system >> ");

    Planet planet = Planet.valueOf(input.nextLine().trim().toUpperCase());
    System.out.println("You entered " + planet);

    int position = planet.ordinal();
    System.out.println(planet + " is " + (position + 1)
            + " planet(s) from the sun");
}
}

以上代码有效,请留意您的意见。

尝试过如下组合:mArS,火星,火星等。

答案 1 :(得分:0)

第二项任务: 它可以准确打印您的数组中的代码,第一和第二服务。 要打印所有项目,请循环显示所有项目并打印所需的所有字段。 如果排序不是任务本身,请考虑使用Arrays.sort(array, comparator)

答案 2 :(得分:-1)

import java.util.Scanner;

public class DebugNine4 {
    enum Planet {MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE};

    public static void main(String[] args) {

              Planet planet;
              String userEntry;
              int position;
              int pos;
              int comparison;
              Scanner input = new Scanner(System.in);
              System.out.print("Enter a planet in our solar system >> ");
              userEntry = input.nextLine().toUpperCase();
              planet = Planet.valueOf(userEntry);
              System.out.println("You entered " + planet);
              pos =  planet.ordinal();
              System.out.println(planet + " is " + (pos + 1) + " planet(s) from the sun");
           }
        }