开发者

Give an assignment until it reaches a number, after that another assignment, and so on

I want to make an algorithm that will print out for exemple, "that is ok" until the number 0.25. And after that it says "warning", so when it reaches 0.3 it warns one person (one variable, let's call it person A); When the value raises up to 0.4 it warns two people (person A and person B); And finally, when it reaches 0.5 it warns three people (person A, person B, person C).

So, I don't know which variables to use, but going one step at a time, I came up with the following;

import java.util.Scanner;

public class apple {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner mc = new Scanner(System.in);
        double ind;
        ind = mc.nextDouble();

        switch (ind){
        case 0.25:
            System.out.println("Ok");
            break;
        case 0.3:
            System.out.println("Warning");
            break;
        case 0.4:
            System.out.println("danger");
        }
    }

}

now, i know switch is only usable to int variables. But I guess my logic was 开发者_StackOverflowat the frakking least, in the right place XD. Help anyone?


So here, is What I came up with, but now I got a new problem, I want that the value start from 0 and move up to 0.25 without anyone typing, i guess just like "for". I donno, maybe i'm too noob to make sense.

public static void main(String[] args) {

Scanner mc = new Scanner(System.in);
    System.out.println("Level of radiation: ");
    double ind = mc.nextDouble();
    if (ind <= 0.25) {
           System.out.println("Ok");
        } else if (ind <= 0.3) {
           System.out.println("Warning");
        } else if (ind <= 0.4) {
           System.out.println("danger");
        }
}

}


Maybe you will be less confused when you split the things to two variables: The text and the recipients:

private void sendAllMessages(double ind) {
  String text;
  if (ind < 0.25) {
    text = "Ok";
  } else if (ind < 0.30) {
    text = "Warning";
  } else {
    text = "Danger";
  }

  // A always gets a message
  sendMessage(text, A);

  // B was annoyed of the many messages
  if (ind >= 0.40) {
    sendMessage(text, B);
  }

  // C is really important and has lots of other emails, so keep him quiet
  if (ind >= 0.50) {
    sendMessage(text, C);
  }
}

To test this code, you should write a very simple sendMessage method, and then run the following:

@Test
public void testSendMessages() {
  for (int level = 0; level < 100; level++) {
    double ind = level / 100.0; /* note the ".0" at the end; that's important. */
    sendAllMessages(ind);
  }
}


how about something like this:

Map<Float, Person> notificationMap = new TreeMap<Float,Person>();
notificationMap.put(0.1, A);
notificationMap.put(0.3, B);
notificationMap.put(0.4, C);

...

ind = mc.nextDouble();
Collection<Person> persons = notificationMap.headMap(ind).values();

for (Person p : persons) {
  notify(p);
}

I probably wouldn't implement this way for efficiency reasons, but I think it is very readable.

TreeMap javadoc for more info.


You should not use switch in this case since you're not testing a "sharp" value but a range:

if (ind <= 0.25) {
   System.out.println("Ok");
} else if (ind <= 0.3) {
   System.out.println("Warning");
} else if (ind <= 0.4) {
   System.out.println("danger");
}

From the comments I assume that you need something like this:

public class Person {

   private double ind;
   private String name;

   public Person(double ind, String name) {
      this.ind = ind;
      this.name = name;
   }

   public void printWarning() {
      if (ind <= 0.25) {
         System.out.println("Ok");
      } else if (ind <= 0.3) {
         System.out.println("Warning");
      } else if (ind <= 0.4) {
        System.out.println("danger");
      }
   }

}

Then you can invoke it like that

public static void main(String[] args) {
   Person a = new Person(0.25, a);
   Person b = new Person(0.3, b);
   Person c = new Person(0.4, c);

   ArrayList<Person> persons = new ArrayList<Person>();
   persons.add(a);
   persons.add(b);
   persons.add(c);

   foreach (Person p: persons) {
      p.printWarning();
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜