0

I am having an issue with a couple of programs - one I am to debug and the other that I need to write.

The first one I am to take a user inputted planet name, convert it to uppercase and then run it through an enum. What did I do wrong? 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");
}
}

The 2nd one is the output is not going through like I want it. It is coming back with:

Cut Shampoo

and not with the info I need it to do - be sortable by name of service, cost and time. Where did I go wrong on this code?

  1. First you will need to create a Service object with three data fields.
  2. Next you will need to create an array of 6 Service objects, populating them with the data from table 9.6, page 420 in the book, like:

    service[0] = new Service("Cut",8.00,15);
    
  3. Using the Scanner object, collect the response from the question: "Sort services by (S)ervice, (P)rice, or (T)ime"

  4. Return all of the information in 3 formatted columns listed in the correct order based on the input like:

    Sorted by time:

    Trim          $6.00    5 minutes
    Shampoo        $4.00    10 minutes
    

Code:

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;
}
}
4

3 回答 3

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、mars、Mars 等。

于 2013-10-06T18:42:59.767 回答
0

对于第二个任务:它准确地打印您编码的内容,数组中的第一个和第二个服务。要打印所有项目,请循环浏览它们并打印出所有需要的字段。Arrays.sort(array, comparator)如果排序不是任务本身,请考虑使用。

于 2013-10-07T08:02:16.607 回答
-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");
           }
        }
于 2015-06-07T06:46:59.937 回答