2021. 2. 18. 16:56ㆍJava programming
MegaBytes Converter
Write a mothod called printMegaBytesAndKiloBytes that has 1 paramter of type int with the name kiloBytes.
The method should nt return anything (void) and it needs to calculate the megabytes and remaining kilobytes from the kilobytes parameter.
Then it needs to print a message in the format "XX KB = YY MB and ZZ KB".
XX represents the orignial value kiloBytes.
YY represents the calculated megabytes.
ZZ represents the calculated remaining kilobytes.
For example, when the parameter kiloBytes is 2500 it needs to print "2500 KB = 2 MB and 452 KB"
If the parameter kiloBytes is less than 0 then print the text "Invalid Value".
public class MegaBytesConverter {
public static void printMegaBytesAndKiloBytes (int kiloBytes){
if (kiloBytes < 0) {
System.out.println("Invalid Value");
} else {
int megaBytes = (kiloBytes / 1024);
int remainingKilobytes = kiloBytes % 1024;
System.out.println(kiloBytes + " KB = " + megaBytes + " MB and " + remainingKilobytes + " KB");
}
}
}
Above is my answer...
점점 문제가 응용력을 요구한다.
KiloBytes에서 Megabytes로 변환하는 과정에서 나눗셈 나머지를 구할 수 있도록 하는 % 기능을 사용해보았다.
'Java programming' 카테고리의 다른 글
Coding Exercise 4: Leap Year Calculator (0) | 2021.02.20 |
---|---|
Coding Exercise 3: Barking Dog (0) | 2021.02.18 |
Coding Exercise 1: Speed Converter (0) | 2021.02.18 |
Operators, Operands and Expressions (0) | 2021.02.17 |
float and double Primitive Types (0) | 2021.02.17 |