자바 if else 질문이요!

자바 if else 질문이요!

작성일 2008.10.18댓글 2건
    게시물 수정 , 삭제는 로그인 필요

import java.util.Scanner;
import java.text.NumberFormat;

public class Salary
{
   public static void main (String[] args)
   {
       double currentSalary;  // employee's current  salary
       double raise;          // amount of the raise
       double newSalary;      // new salary for the employee
       String rating;         // performance rating

       Scanner scan = new Scanner(System.in);

       System.out.print ("Enter the current salary: ");
       currentSalary = scan.nextDouble();
       System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
       rating = scan.next();

       // Compute the raise using if ...
    if (rating == 1)
          raise = currentSalary * .06;
        else if(rating == 2)
          raise = currentSalary * .04;
        else if(rating == 3)
          raise = currentSalary * .015;
      

       
       newSalary = currentSalary + raise;

       // Print the results
       NumberFormat money = NumberFormat.getCurrencyInstance();
       System.out.println();
       System.out.println("Current Salary:       " + money.format(currentSalary));
       System.out.println("Amount of your raise: " + money.format(raise));
       System.out.println("Your new salary:      " + money.format(newSalary));
       System.out.println();
    }
}

 

저기서 1은 excellent 2 는 good 3은 poor

인데요

 

컴파일이 안되네요.. 도와주세요.


#자바 if문 #자바 if else #자바 if 조건 여러개 #자바 if문 탈출 #자바 if문 예제 #자바 if문 조건 여러개 #자바 if문 return #자바 if문 break #자바 ifpresent #자바 if문 한줄

profile_image 익명 작성일 -

먼저, rating은 String입니다.

String객체를 비교할땐 .equals("문자열") 형식이죠.

== 구문으로 비교할수 없습니다.

 

그리고, raise는 초기값이 없기때문에, if~else문에서 어긋날경우 값이 없습니다.

그래서, 최초 변수선언시 초기값을 주거나, else raise = 0; 라고 값을 넣어야죠.

안그러면 raise는 null이기때문에, money.format(raise)가 성립될수 없어 에러가납니다.

 

 

import java.util.Scanner;
import java.text.NumberFormat;

public class Salary
{
   public static void main (String[] args)
   {
       double currentSalary;  // employee's current  salary
       double raise;          // amount of the raise
       double newSalary;      // new salary for the employee
       String rating;         // performance rating

       Scanner scan = new Scanner(System.in);

       System.out.print ("Enter the current salary: ");
       currentSalary = scan.nextDouble();
       System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
       rating = scan.next();

       // Compute the raise using if ...
    if (rating.equals("1"))
          raise = currentSalary * .06;
        else if(rating.equals("2"))
          raise = currentSalary * .04;
        else if(rating.equals("3"))
          raise = currentSalary * .015;
       else
          raise = 0;
     

      
       newSalary = currentSalary + raise;

       // Print the results
       NumberFormat money = NumberFormat.getCurrencyInstance();
       System.out.println();
       System.out.println("Current Salary:       " + money.format(currentSalary));
       System.out.println("Amount of your raise: " + money.format(raise));
       System.out.println("Your new salary:      " + money.format(newSalary));
       System.out.println();
    }
}

profile_image 익명 작성일 -

rating 변수를 String 으로 선언하셨네요.

스트링 값을 비교하기 위해서는 == 이런 연산자를 사용할수 없습니다.

 

equals() 메소드를 사용하시면 됩니다.

 

if (rating.equals("1"))

~~~

else if (rating.equals("2"))

~~~

 

이런 형식으로 사용하세요..


         

자바 if-else 질문이요

void speedUp(int x) { if(velocity >= 90) System.out.println("90 Km 를 초과하지 못합니다. 현재속도는 " + velocity); else { velocity = velocity + x; System.out.println("속도를 " +x...

자바 if else 질문이요!

... else if(rating == 2) raise = currentSalary * .04; else if(rating == 3) raise = currentSalary * .015; newSalary = currentSalary + raise; // Print the results...

java 기초 질문! 자바 if else 문...

... if (num1 > num2) { int max=num1; } else { int max=num2; } System.out.println(max); } } 자바입문자입니다 이렇게 프로그램은 잘못된 부분이 있다고 하는데요 어떤...

자바 if else질문드립니다.

if-else문 function pwChk(){ if(document.frm1.pw.value=="1234") alert("비밀번호가 정확합니다.") else alert("비밀번호를 다시 입력해 주세요!") } 비밀번호...

자바 if else문 - 링크클릭시...

... if(t==1) { window.open("http://address.com", "name1", "width=450, height=600, scrollbars=no"); }else if(t==2){ window.open("http://address2.com", "name2", "width...

else if 괄호 문제 질문입니다

안녕하세요 자바스크립트 예제 풀다가 연습하는데 아래처럼 짜고 거래처명... else if(frm.address.value == "") alert("주소를 입력하세요"); else if(frm.tel.value...