Java 어디가 잘못됐는지 못찾겠습니다.. 도와주세요.
조회수 434회
public class Duplicate{
public static void main(String[] args){
double dishes = 0.0;
int studentdiscount = 50;
int workerdiscount = 25;
int guestdiscount = 0;
int discountreal = 0;
double discountprice = 0.0;
int isxmas = 0;
dishes = Terminal.askFloat("how much is the dishes? ");
isxmas = Terminal.askInt("is today xmas? (y: 1, n: 0)");
discountreal = isxmas * studentdiscount; //instance1
discountprice = dishes * (1 - discountreal/100.0); //instance1
System.out.printf("\nit costs: %.2f dollar for student.\n", discountprice);
discountreal = isxmas * workerdiscount; //instance2
discountprice = dishes * (1 - discountreal/100.0); //instance2
System.out.printf("\nit costs: %.2f dollar for workers.\n", discountprice);
discountreal = isxmas * guestdiscount; //instance3
discountprice = dishes * (1 - discountreal/100.0); //instance3
System.out.printf("\nit costs: %.2f it costs: %.2f dollar for guests.\n", discountprice);
}
}
위의 코드에서 반복되는 instance 부분들을 메소드를 사용해 간단하게 해야하는게 문제입니다. 아래가 제가 나름대로 해 본 부분인데, 실행하면 자꾸 0 만 나옵니다... 변수선언 위치가 잘못되었는지, 어쨌는지 도대체가 모르겠습니다. 제발 도와주세요..ㅠㅠ
public class Test{
public static double discountprice(double dishes, int xmas, int studentdiscount){
return dishes * (1 - (xmas * studentdiscount/100.0));
}
public static void main(String[] args){
double dishes = 0.0;
int studentdiscount = 50;
int discountreal = 0;
double discountprice = 0.0;
int isxmas = 0;
dishes = Terminal.askFloat("how much is the dishes? ");
isxmas = Terminal.askInt("is today xmas? (y: 1, n: 0)");
System.out.printf("\nit costs: %.2f dollar for student.\n", discountprice);
System.out.printf("\nit costs: %.2f dollar for workers.\n", discountprice*1.5);
System.out.printf("\nit costs: %.2f it costs: %.2f dollar for guests.\n", discountprice*2);
}
}
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
아래와 같이 메소드를 호출해야겠죠.
... ... System.out.printf("\nit costs: %.2f dollar for student.\n", discountprice(dishes, isxmas, studentdiscount)); System.out.printf("\nit costs: %.2f dollar for workers.\n", discountprice(dishes, isxmas, studentdiscount)*1.5); System.out.printf("\nit costs: %.2f dollar for guests.\n", discountprice(dishes, isxmas, studentdiscount)*2.0);
댓글 입력